sqlite列出所有表信息

前端之家收集整理的这篇文章主要介绍了sqlite列出所有表信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <sqlite3.h>
#include <stdio.h>
#include <string.h>

//回调函数
int ListTablesName_callback(void *pDB,int argc,char **argv,char **azColName)
{
    int ret;
    char psqlQuerySel[128] = "select * from ";
    char *pError = NULL;
    sqlite3_stmt *pStmt = NULL;

    strcat(psqlQuerySel,argv[0]);    
    fprintf(stdout,"%s\n",psqlQuerySel);

        //准备读取数据
    ret = sqlite3_prepare((sqlite3 *)pDB,psqlQuerySel,strlen(psqlQuerySel),&pStmt,(const char**)&pError);
    if (ret != sqlITE_OK)
    {
        fprintf(stderr,"sqlite3_prepare fail\n");
        return -1;
    }

    while (1)
    {
        ret = sqlite3_step(pStmt);        //移动记录集
        if (ret != sqlITE_ROW)
            break;

        fprintf(stdout,"%d ",sqlite3_column_int(pStmt,0));
        fprintf(stdout,"%s \n",sqlite3_column_text(pStmt,1));        
    }

    return 0;
}

int main()
{
    int ret = 0;
    sqlite3 *pDB = NULL;
    sqlite3_stmt *pStmt = NULL;
    char *pError = NULL;
    char *pListTablesNameSel = (char*)"SELECT name FROM sqlite_master";//列出所以表名语句

    //打开文件
    ret = sqlite3_open("test.db",&pDB);
    if (ret != sqlITE_OK)
    {
        fprintf(stderr,"can't open database : %s\n",sqlite3_errmsg(pDB));

        sqlite3_close(pDB);
        return -1;
    }

  //查找源文件中所有表名    
    ret = sqlite3_exec(pDB,pListTablesNameSel,ListTablesName_callback,pDB,&pError);
    if (ret != sqlITE_OK)
    {
        fprintf(stderr,"list table count error: %s",sqlite3_errmsg(pDB));

        sqlite3_close(pDB);
        return -1;
    }

    return 0;
}

原文链接

猜你在找的Sqlite相关文章