文件首頁
MySQL 9.0 C API 開發者指南
下載本手冊
PDF (US Ltr) - 1.4Mb
PDF (A4) - 1.4Mb


5.4.47 mysql_list_fields()

MYSQL_RES *
mysql_list_fields(MYSQL *mysql,
                  const char *table,
                  const char *wild)

說明

注意

mysql_list_fields() 已被棄用,並將在未來版本的 MySQL 中移除。請改用 mysql_real_query()mysql_query() 來執行 SHOW COLUMNS 語句。

傳回一個空的結果集,其元數據提供給定資料表中與 wild 參數指定的簡單正規表示式相符的欄位相關資訊。wild 可以包含萬用字元 %_,或者可以是 NULL 指標以匹配所有欄位。呼叫 mysql_list_fields() 類似於執行查詢 SHOW COLUMNS FROM tbl_name [LIKE wild]

取得的資訊大致等同於使用 mysql 用戶端執行此處顯示的語句所產生的資訊,如下所示:

$> mysql test --column-type-info -e "SELECT * FROM t LIMIT 0"
Field   1:  `c1`
Catalog:    `def`
Database:   `test`
Table:      `t`
Org_table:  `t`
Type:       LONG
Collation:  binary (63)
Length:     11
Max_length: 0
Decimals:   0
Flags:      NUM 

Field   2:  `c2`
Catalog:    `def`
Database:   `test`
Table:      `t`
Org_table:  `t`
Type:       LONG
Collation:  binary (63)
Length:     11
Max_length: 0
Decimals:   0
Flags:      NUM 

$>

最好使用 SHOW COLUMNS FROM tbl_name 而不是 mysql_list_fields()

您必須使用 mysql_free_result() 釋放結果集。

傳回值

成功時傳回 MYSQL_RES 結果集。如果發生錯誤,則傳回 NULL

錯誤

範例

int i;
MYSQL_RES *tbl_cols = mysql_list_fields(mysql, "mytbl", "f%");

unsigned int field_cnt = mysql_num_fields(tbl_cols);
printf("Number of columns: %d\n", field_cnt);

for (i=0; i < field_cnt; ++i)
{
  /* col describes i-th column of the table */
  MYSQL_FIELD *col = mysql_fetch_field_direct(tbl_cols, i);
  printf ("Column %d: %s\n", i, col->name);
}
mysql_free_result(tbl_cols);