sqlite3 sqlite_master结构

sqlite_master是一个表,属于系统表,存放在根页中,每一个数据库的.db文件都有一个sqlite_master表。
sqlite_master表存放了.db中所有表的相关信息,比如:表的名称、用于创建此表的sql语句、索引、索引所属的表、创建索引的sql语句等。
sqlite_master表是只读的,只能对他进行读操作,写的操作由系统自动执行,使用者没有写的执行权限。用户对自定义表的操作多会自动同步到这个表中。
在这里插入图片描述
TEXT——:text永远默认存储的的table,表类的字段
name——:name存放表名的字段,可以输入要查询的表名

使用示例:
1.查询表存在否:SELECT * from sqlite_master where name = ‘表名’
2.查询表中的字段存在否:SELECT * from sqlite_master where name = ‘表名’ and sql like ‘%字段%’(sql like与通配符作用相似,查询包含%%中间的字段)
3.查询表中的字段存在否(具体):SELECT * from sqlite_master where name = ‘表名’ and sql = ‘字段’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`sqlite3_get_table`是SQLite C API中的一个函数,用于执行一个SQL语句并返回结果集。它的用法如下: ``` int sqlite3_get_table( sqlite3 *db, // SQLite 数据库连接对象 const char *zSql, // 要执行的 SQL 语句 char ***pazResult, // 返回的结果集 int *pnRow, // 返回结果集中的行数 int *pnColumn, // 返回结果集中的列数 char **pzErrmsg // 如果出错,返回错误信息 ); ``` 该函数执行SQL语句,并将结果存储在 `pazResult` 中。结果以一个二维字符数组的形式返回,每行对应一个字符串数组,每个字符串数组中的元素对应一列数据。`pnRow` 和 `pnColumn` 分别返回结果集中的行数和列数。如果发生错误,`pzErrmsg` 返回错误信息。 以下是一些操作 `sqlite3_get_table` 函数的示例: 1. 查询所有表名 ```c char **result; int nRow, nColumn; char *zErrMsg = 0; const char *zSql = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"; int rc = sqlite3_get_table(db, zSql, &result, &nRow, &nColumn, &zErrMsg); if (rc == SQLITE_OK) { for (int i = 0; i <= nRow; i++) { for (int j = 0; j < nColumn; j++) { printf("%s\t", result[i * nColumn + j]); } printf("\n"); } sqlite3_free_table(result); } else { printf("SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } ``` 2. 查询表中所有列名和数据 ```c char **result; int nRow, nColumn; char *zErrMsg = 0; const char *zSql = "SELECT * FROM mytable;"; int rc = sqlite3_get_table(db, zSql, &result, &nRow, &nColumn, &zErrMsg); if (rc == SQLITE_OK) { for (int i = 0; i <= nRow; i++) { for (int j = 0; j < nColumn; j++) { printf("%s\t", result[i * nColumn + j]); } printf("\n"); } sqlite3_free_table(result); } else { printf("SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } ``` 3. 插入数据并查询 ```c char **result; int nRow, nColumn; char *zErrMsg = 0; const char *zSql = "INSERT INTO mytable VALUES(1, 'hello, world'); SELECT * FROM mytable;"; int rc = sqlite3_get_table(db, zSql, &result, &nRow, &nColumn, &zErrMsg); if (rc == SQLITE_OK) { for (int i = 0; i <= nRow; i++) { for (int j = 0; j < nColumn; j++) { printf("%s\t", result[i * nColumn + j]); } printf("\n"); } sqlite3_free_table(result); } else { printf("SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } ``` 注意,`sqlite3_get_table` 函数在查询语句中只支持一个语句。如果要执行多个语句,需要使用 `sqlite3_exec` 函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值