电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

提升SQLite数据插入效率低、速度慢的方法

7页
  • 卖家[上传人]:m****
  • 文档编号:47871367
  • 上传时间:2018-07-05
  • 文档格式:DOCX
  • 文档大小:27.90KB
  • / 7 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 1、提升提升 SQLite 数据插入效率低、速度慢的方法数据插入效率低、速度慢的方法SQLite 数据库由于其简单、灵活、轻量、开源,已经被越来越多的被应用到中小型应用中。甚至有人说,SQLite 完全可以用来取代 c 语言中的文件读写操作。因此我最近编写有关遥感数据处理的程序的时候,也将 SQLite 引入进来,以提高数据的结构化程度,并且提高大数据的处理能力(SQLite 最高支持 2PB 大小的数据)。但是最开始,我发现,直接使用 SQL 语句的插入效率简直低的令人发指的。后来不断查文档、查资料,才发现了一条快速的“数据插入”之路。本文就以插入数据为例,整合网上和资料书中的各种提高 SQLite 效率的方法,给出提高 SQLite 数据插入效率的完整方法。1 1 数据数据我使用的电脑是 Win7 64 位系统,使用 VC2010 编译,SQLIte 版本为 3.7.15.2 ,电脑 CPU 为二代 i3 处理器,内存 6G。实验之前,先建立要插入数据的表:sql view plaincopy1. create table t1 (id integer , x integer , y

      2、int eger, weight real) 2 2 慢速慢速最粗暴的方法最粗暴的方法SQLite 的 API 中直接执行 SQL 的函数是:cpp view plaincopy1. int sqlite3_exec( sqlite3*, const char *sql, int (*callback)(void*,int,char*,char*), v oid *, char *errmsg) 直接使用 INSERT 语句的字符串进行插入,程序部分代码(完整代码见后文),如下:cpp view plaincopy1. for(int i=0;i 2. #include 3. #include 4. #include 5. #include “sqlite3.h“ 6. 7. const int nCount = 500000; 8. 9. int main (int argc,char* argv) 10. 11. sqlite3* db; 12. sqlite3_open(“testdb.db“ , 13. sqlite3_exec(db,“PRAGMA synchronous =

      3、 OFF; “,0,0,0); 14. sqlite3_exec(db,“drop table if exists t1“,0,0 ,0); 15. sqlite3_exec(db,“create table t1(id integer,xinteger,y integer ,weight real)“,0,0,0); 16. clock_t t1 = clock(); 17. 18. sqlite3_exec(db,“begin;“,0,0,0); 19. sqlite3_stmt *stmt; 20. const char* sql = “insert into t1 values(?,?, ?,?)“; 21. sqlite3_prepare_v2(db,sql,strlen(sql), 22. 23. for(int i=0;inCount;+i) 24. 25. / std:stringstream ssm; 26. / ssm“insert into t1 values(“i“, “i*2“,“i/2“,“i*i“)“; 27. / sqlite3_exec(db,ssm.str().c_str(),0, 0,0); 天地棋牌 http:/28. sqlite3_reset(stmt); 29. sqlite3_bind_int(stmt,1,i); 30. sqlite3_bind_int(stmt,2,i*2); 31. sqlite3_bind_int(stmt,3,i/2); 32. sqlite3_bind_double(stmt,4,i*i); 33. sqlite3_step(stmt); 34. 35. sqlite3_finalize(stmt); 36. sqlite3_exec(db,“commit;“,0,0,0); 37. clock_t t2 = clock(); 38. 39. sqlite3_close(db); 40. 41. std:cout“cost tima: “(t2-t1)/1000.“s “std:endl; 42. 43. return 0; 44.

      《提升SQLite数据插入效率低、速度慢的方法》由会员m****分享,可在线阅读,更多相关《提升SQLite数据插入效率低、速度慢的方法》请在金锄头文库上搜索。

      点击阅读更多内容
    关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
    手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
    ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.