#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列出所有表信息
最新推荐文章于 2024-04-28 15:05:14 发布