簡體   English   中英

在sqlite3中獲取列名

[英]Get column names in sqlite3

我想打印我的sql表內容,因此,我想從表中檢索列名。 我遇到的一個解決方案是:

SELECT sql FROM sqlite_master WHERE tbl_name = 'table_name' AND type = 'table'

但看起來我將不得不解析結果。

另一個建議是使用:

PRAGMA table_info(table_name);

但是下面的sqlite頁面建議不要使用它: http//www.sqlite.org/pragma.html#pragma_full_column_names

有沒有辦法實現這一目標。 還有什么是使用的語法

PRAGMA table_info(table_name);

以上解決方案已從此處獲取

由於您的問題已標記為c我假設您可以訪問SQLite C API。 如果使用從您希望的表中選擇的prepare_v2函數之一創建准備語句,則可以使用sqlite3_column_name獲取每列的名稱。

您可以安全地使用PRAGMA table_info(table-name); 因為它沒有以任何方式棄用(你的鏈接發布到另一個pragma)。

int sqlite3_get_table(
    sqlite3 *db,          /* An open database */
    const char *zSql,     /* SQL to be evaluated */
    char ***pazResult,    /* Results of the query */
    int *pnRow,           /* Number of result rows written here */
    int *pnColumn,        /* Number of result columns written here */
    char **pzErrmsg       /* Error msg written here */
    );

如果您使用的是c / c ++,則可以使用函數sqlite3_get_table(db,query,result,nrow,ncol,errmsg);

將查詢作為select * from table;

前幾個結果[0],結果[1] ......將有列名。

此設置將切換顯示列名稱作為select語句的返回值的一部分:

.headers on

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM