测试在多线程下操作sqlite内存数据库(转)


硬件平台: Linux on vmware6
操作系统:Trustix Linux3
SQLITE: sqlite-3.5.7


以下摘自:http://www.sqlite.org/c3ref/open.html
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);

If the filename is ":memory:", then an private in-memory database is created for the connection. This in-memory database will vanish when the database connection is closed. Future version of SQLite might make use of additional special filenames that begin with the ":" character. It is recommended that when a database filename really does begin with ":" that you prefix the filename with a pathname like "./" to avoid ambiguity.


我在主线程中使用
int sqlitehandle = sqlite3_open(":memory:", &pdb);
创建内存数据库,并创建表:
int rs = sqlite3_exec(pdb, "CREATE TABLE testtbl(id int NOT NULL, name varchar(100))", 0, 0,0);


然后创建两个新的线程,将pdb传入线程中,主线程进入睡眠。
在两个线程中,均执行:
  while(1)
  {
    int rs = sqlite3_exec(pdb, "INSERT INTO testtbl values(25,'aaaaaaaaaaaa')", 0,0,0);
    printf("rs = %d/n", rs);
    if(rs != SQLITE_OK)
    {
        printf("err rs=%d/n", rs);
        break;
    }
    usleep(10);
 }

测试结果显示是可以两个线程操作同一个句柄的!!!
并且在程序运行时,我用lsof命令查看该进程的句柄状态,确实没有任何磁盘的操作!!!

但有个问题不知道是否是由于我是虚拟机引起,即该程序在执行(约1分钟后),两个线程像是进入死锁状态,再没有任何动作,没有退出,也没有显示insert失败!这暂时不理解。


 
文件:sqlite-3.5.7.tar.gz
大小:2506KB
下载:下载



2008.5.5增加:

根据sqlite3的描述,自3.3.1版本起才能在多个线程中操作同一个句柄.但未测试;

另外,上面的例子中,如果不是内存数据库,那么多个线程同时写同一个句柄,sqlite_exec会返回21的错误,这说明了需要在写前加锁.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用多线程访问SQLite内存数据库的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <sqlite3.h> #define THREAD_NUM 5 #define DB_NAME ":memory:" sqlite3 *db; void *thread_func(void *arg) { int thread_id = *(int *)arg; char sql[100]; printf("Thread %d: starting...\n", thread_id); // 执行一些数据库操作 sprintf(sql, "INSERT INTO test VALUES (%d)", thread_id); sqlite3_exec(db, sql, NULL, NULL, NULL); printf("Thread %d: done!\n", thread_id); pthread_exit(NULL); } int main() { int i, ret; pthread_t threads[THREAD_NUM]; int thread_ids[THREAD_NUM]; // 打开内存数据库 ret = sqlite3_open(DB_NAME, &db); if (ret != SQLITE_OK) { fprintf(stderr, "Failed to open database: %s\n", sqlite3_errmsg(db)); exit(1); } // 创建测试sqlite3_exec(db, "CREATE TABLE test (id INTEGER)", NULL, NULL, NULL); // 创建多个线程来访问数据库 for (i = 0; i < THREAD_NUM; i++) { thread_ids[i] = i; ret = pthread_create(&threads[i], NULL, thread_func, &thread_ids[i]); if (ret != 0) { fprintf(stderr, "Failed to create thread %d\n", i); exit(1); } } // 等待所有线程完成 for (i = 0; i < THREAD_NUM; i++) { pthread_join(threads[i], NULL); } // 查询测试表并输出结果 printf("Query results:\n"); sqlite3_exec(db, "SELECT * FROM test", [](void *data, int argc, char **argv, char **col_names) -> int { for (int i = 0; i < argc; i++) { printf("%s = %s\n", col_names[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; }, NULL, NULL); // 关闭数据库 sqlite3_close(db); return 0; } ``` 该程序会创建一个内存数据库,并创建一个名为“test”的表,然后创建多个线程来访问数据库,在每个线程中插入一个整数值。最后,程序会查询“test”表并输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值