PostgreSQL:如何讀取PostgreSQL表格架構
資訊組: 編程SQL > PostgreSQL:如何讀取PostgreSQL表格架構
PostgreSQL: 如何讀取PostgreSQL表格架構
以下範例能讀出 PostgreSQL 內表格的架構 Table Structure,包括字串長度:
pg_num_fields
pg_field_name
pg_field_type
附加檔案:0 | 讀取:155 | 留言:0
自用備註:function get_field_size($table,$field,$type) { // get field length
global $zz;
if (preg_match('/char/',$type)) {
$r=$zz->sqlexec("select a.atttypmod,a.attrelid from pg_attribute as a, pg_class as c where c.relname='$table' AND a.attrelid=c.oid AND a.attname='$field' ");
$r2=pg_fetch_object($r->info);
$result=$r2->atttypmod-4;
} else {
$result='-';
}
return $result;
} //get_field_size
$table='log';
//$s="select * from $table"; $r=$zz->sqlexec($s);
$r=pg_query($link,"select * from $table");
$fieldcount=pg_num_fields($r->info);
$data.="<table border=1>";
$data.="<tr> <td colspan=4> Table Structure: $table";
$data.="<tr><th>#<th>Field<th>Type<th>Length Char";
for ($i = 0; $i < $fieldcount; ++$i) {
$fieldname = pg_field_name($r->info, $i);
$fieldtype = pg_field_type($r->info, $i);
$data.="<tr>";
$data.="<td>".($i+1);
$data.="<td>$fieldname";
$data.="<td>$fieldtype";
$data.="<td>".get_field_size($table,$fieldname,$fieldtype);
}
$data.="</table>";
echo $data;
張貼: 菠蘿油王子 2011-10-13
我要推薦給朋友
文章留言
未有留言
我要留言
請先登入才可留言!