sqlite3 C封装

二次封装:https://blog.csdn.net/lifan1314521/article/details/51250350

  1. 回调函数
    sqlite3_exec()有回调函数
    sqlite3_step(), sqlite3_perpare(), sqlite3_finalize() 可以不用回调函数一步一步的去取数据去的字段。

  2. 速度效率上sqlite3_step快
    参考:https://blog.csdn.net/zongshiwujie/article/details/7394101
    关于不同插入方法对插入速度的影响参考:https://www.sqlite.org/faq.html#q19
    中的“(19) INSERT is really slow - I can only do few dozen INSERTs per second

Actually, SQLite will easily do 50,000 or more INSERT statements per second on an average desktop computer. But it will only do a few dozen transactions per second. Transaction speed is limited by the rotational speed of your disk drive. A transaction normally requires two complete rotations of the disk platter, which on a 7200RPM disk drive limits you to about 60 transactions per second.
Transaction speed is limited by disk drive speed because (by default) SQLite actually waits until the data really is safely stored on the disk surface before the transaction is complete. That way, if you suddenly lose power or if your OS crashes, your data is still safe. For details, read about atomic commit in SQLite…

By default, each INSERT statement is its own transaction. But if you surround multiple INSERT statements with BEGIN…COMMIT then all the inserts are grouped into a single transaction. The time needed to commit the transaction is amortized over all the enclosed insert statements and so the time per insert statement is greatly reduced.

Another option is to run PRAGMA synchronous=OFF. This command will cause SQLite to not wait on data to reach the disk surface, which will make write operations appear to be much faster. But if you lose power in the middle of a transaction, your database file might go corrupt.

  1. 对sql事务理解:
    https://blog.csdn.net/qyx0714/article/details/70157749
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLite3是一个开源的轻量级关系型数据库管理系统,适用于嵌入式设备和移动平台。在C语言中,可以使用SQLite3 API来操作数据库,包括创建表、插入数据、查询数据、更新数据等操作。为了方便使用,可以将SQLite3数据库封装成一个C语言库,提供更简单的接口供应用程序调用。 以下是一个简单的SQLite3封装库示例: ```c #include <stdio.h> #include <string.h> #include <sqlite3.h> /* 封装SQLite3数据库操作 */ typedef struct { sqlite3* db; char* errmsg; } Sqlite3; /* 打开数据库 */ int sqlite3_open_db(Sqlite3* sqlite, const char* db_path) { int ret = sqlite3_open(db_path, &sqlite->db); if (ret != SQLITE_OK) { sqlite->errmsg = sqlite3_errmsg(sqlite->db); return -1; } return 0; } /* 关闭数据库 */ void sqlite3_close_db(Sqlite3* sqlite) { sqlite3_close(sqlite->db); } /* 执行SQL语句 */ int sqlite3_exec_sql(Sqlite3* sqlite, const char* sql) { int ret = sqlite3_exec(sqlite->db, sql, NULL, NULL, &sqlite->errmsg); if (ret != SQLITE_OK) { return -1; } return 0; } /* 查询数据 */ int sqlite3_query_data(Sqlite3* sqlite, const char* sql, int (*callback)(void*, int, char**, char**), void* data) { int ret = sqlite3_exec(sqlite->db, sql, callback, data, &sqlite->errmsg); if (ret != SQLITE_OK) { return -1; } return 0; } /* 示例:创建表 */ int sqlite3_create_table(Sqlite3* sqlite, const char* table_name, const char* fields) { char sql[256]; sprintf(sql, "CREATE TABLE IF NOT EXISTS %s (%s)", table_name, fields); return sqlite3_exec_sql(sqlite, sql); } /* 示例:插入数据 */ int sqlite3_insert_data(Sqlite3* sqlite, const char* table_name, const char* values) { char sql[256]; sprintf(sql, "INSERT INTO %s VALUES (%s)", table_name, values); return sqlite3_exec_sql(sqlite, sql); } /* 示例:查询数据 */ int sqlite3_query_data(Sqlite3* sqlite, const char* table_name) { char sql[256]; sprintf(sql, "SELECT * FROM %s", table_name); return sqlite3_query_data(sqlite, sql, callback, NULL); } /* 示例:回调函数 */ int callback(void* data, int argc, char** argv, char** azColName) { int i; for (i = 0; i < argc; i++) { printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; } /* 示例:主函数 */ int main() { Sqlite3 sqlite; if (sqlite3_open_db(&sqlite, "test.db") != 0) { printf("Open database failed: %s\n", sqlite.errmsg); return -1; } if (sqlite3_create_table(&sqlite, "user", "id INT PRIMARY KEY, name TEXT, age INT") != 0) { printf("Create table failed: %s\n", sqlite.errmsg); sqlite3_close_db(&sqlite); return -1; } if (sqlite3_insert_data(&sqlite, "user", "1, 'Tom', 20") != 0) { printf("Insert data failed: %s\n", sqlite.errmsg); sqlite3_close_db(&sqlite); return -1; } if (sqlite3_query_data(&sqlite, "user") != 0) { printf("Query data failed: %s\n", sqlite.errmsg); sqlite3_close_db(&sqlite); return -1; } sqlite3_close_db(&sqlite); return 0; } ``` 以上代码演示了如何使用SQLite3 API封装一个简单的数据库操作库,其中包括打开数据库、关闭数据库、执行SQL语句、查询数据等操作,并提供了示例代码。在实际应用中,可以根据需要扩展更多的接口,以满足不同的业务需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值