SQLite学习手册(实例代码<二>)

前端之家收集整理的这篇文章主要介绍了SQLite学习手册(实例代码<二>)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

三、高效的批量数据插入:

在给出操作步骤之前先简单说明一下批量插入的概念,以帮助大家阅读其后的示例代码。事实上,批量插入并不是什么新的概念,在其它关系型数据库的C接口API中都提供了一定的支持,只是接口的实现方式不同而已。纵观众多流行的数据库接口,如OCI(Oracle API)、MysqL API和Postgresql API等,OCI提供的编程接口最为方便,实现方式也最为高效。sqlite作为一种简单灵活的嵌入式数据库也同样提供了该功能,但是实现方式并不像其他数据库那样方便明显,它只是通过一种隐含的技巧来达到批量插入的目的,其逻辑如下:
1). 开始一个事物,以保证后面的数据操作语句均在该事物内完成。在sqlite中,如果没有手工开启一个事物,其所有的DML语句都是在自动提交模式下工作的,既每次操作后数据均被自动提交并写入磁盘文件。然而在非自动提交模式下,只有当其所在的事物被手工COMMIT之后才会将修改的数据写入到磁盘中,之前修改的数据都是仅仅驻留在内存中。显而易见,这样的批量写入方式在效率上势必会远远优于多迭代式的单次写入操作。
2). 基于变量绑定的方式准备待插入的数据,这样可以节省大量的sqlite3_prepare_v2函数调用次数,从而节省了多次将同一sql语句编译成sqlite内部识别的字节码所用的时间。事实上,sqlite的官方文档中已经明确指出,在很多时候sqlite3_prepare_v2函数的执行时间要多于sqlite3_step函数的执行时间,因此建议使用者要尽量避免重复调用sqlite3_prepare_v2函数。在我们的实现中,如果想避免此类开销,只需将待插入的数据以变量的形式绑定到sql语句中,这样该sql语句仅需调用sqlite3_prepare_v2函数编译一次即可,其后的操作只是替换不同的变量数值。
3). 在完成所有的数据插入后显式的提交事物。提交后,sqlite会将当前连接自动恢复为自动提交模式。

下面是示例代码的实现步骤:
1). 创建测试数据表。
2). 通过执行BEGIN TRANSACTION语句手工开启一个事物。
3). 准备插入语句及相关的绑定变量。
4). 迭代式插入数据。
5). 完成后通过执行COMMIT语句提交事物。
6). 删除测试表。
见以下代码及关键性注释:

  1 #include <sqlite3.h>
  2 #include <string>
  3 #include <stdio.h>
  4 
  5 using namespace std;
  6 
  7 void doTest()
  8 {
  9     sqlite3* conn = NULL;
 10     //1. 打开数据库
 11     int result = sqlite3_open("D:/mytest.db",&conn);
 12     if (result != sqlITE_OK) {
 13         sqlite3_close(conn);
 14         return;
 15     }
 16     const char* createTablesql = 
 17         CREATE TABLE TESTTABLE (int_col INT,float_col REAL,string_col TEXT)";
 18     sqlite3_stmt* stmt = NULL;
 19     int len = strlen(createTablesql);
 20     2. 准备创建数据表,如果创建失败,需要用sqlite3_finalize释放sqlite3_stmt对象,以防止内存泄露。 21     if (sqlite3_prepare_v2(conn,createTablesql,len,&stmt,NULL) != sqlITE_OK) {
 22         if (stmt)
 23             sqlite3_finalize(stmt);
 24         sqlite3_close(conn);
 25          26     }
 27     3. 通过sqlite3_step命令执行创建表的语句。对于DDL和DML语句而言,sqlite3_step执行正确的返回值
 28     只有sqlITE_DONE,对于SELECT查询而言,如果有数据返回sqlITE_ROW,当到达结果集末尾时则返回
 29     //sqlITE_DONE。 30     if (sqlite3_step(stmt) != sqlITE_DONE) {
 31         sqlite3_finalize(stmt);
 32         sqlite3_close(conn);
 33          34     }
 35     4. 释放创建表语句对象的资源。 36     sqlite3_finalize(stmt);
 37     printf(Succeed to create test table now.\n");
 38 
 39     5. 显式的开启一个事物。 40     sqlite3_stmt* stmt2 = NULL;
 41     char* beginsql = BEGIN TRANSACTION 42      43         if (stmt2)
 44             sqlite3_finalize(stmt2);
 45         sqlite3_close(conn);
 46          47     }
 48     if (sqlite3_step(stmt2) != sqlITE_DONE) {
 49         sqlite3_finalize(stmt2);
 50         sqlite3_close(conn);
 51          52     }
 53     sqlite3_finalize(stmt2);
 54 
 55     6. 构建基于绑定变量的插入数据。 56     char* insertsql = INSERT INTO TESTTABLE VALUES(?,?,?) 57     sqlite3_stmt* stmt3 = NULL;
 58      59         if (stmt3)
 60             sqlite3_finalize(stmt3);
 61         sqlite3_close(conn);
 62          63     }
 64     int insertCount = 10;
 65     char* strData = This is a test. 66     7. 基于已有的sql语句,迭代的绑定不同的变量数据 67     for (int i = 0; i < insertCount; ++i) {
 68         在绑定时,最左面的变量索引值是1。 69         sqlite3_bind_int(stmt3,1,i);
 70         sqlite3_bind_double(stmt3,128); line-height:1.5!important">2,i * 1.0);
 71         sqlite3_bind_text(stmt3,128); line-height:1.5!important">3,strData,strlen(strData),sqlITE_TRANSIENT);
 72         if (sqlite3_step(stmt3) != sqlITE_DONE) {
 73             sqlite3_finalize(stmt3);
 74             sqlite3_close(conn);
 75              76         }
 77         重新初始化该sqlite3_stmt对象绑定的变量。 78         sqlite3_reset(stmt3);
 79         printf(Insert Succeed.\n 80     }
 81     sqlite3_finalize(stmt3);
 82 
 83     8. 提交之前的事物。 84     char* commitsql = COMMIT 85     sqlite3_stmt* stmt4 = NULL;
 86      87         if (stmt4)
 88             sqlite3_finalize(stmt4);
 89         sqlite3_close(conn);
 90          91     }
 92     if (sqlite3_step(stmt4) != sqlITE_DONE) {
 93         sqlite3_finalize(stmt4);
 94         sqlite3_close(conn);
 95          96     }
 97     sqlite3_finalize(stmt4);
 98 
 99     9. 为了方便下一次测试运行,我们这里需要删除函数创建的数据表,否则在下次运行时将无法
100     创建该表,因为它已经存在。101     char* dropsql = DROP TABLE TESTTABLE102     sqlite3_stmt* stmt5 = NULL;
103     104         if (stmt5)
105             sqlite3_finalize(stmt5);
106         sqlite3_close(conn);
107         108     }
109     if (sqlite3_step(stmt5) == sqlITE_DONE) {
110         printf(The test table has been dropped.\n111     }
112     sqlite3_finalize(stmt5);
113     sqlite3_close(conn);
114 }
115 
116 int main()
117 {
118     doTest();
119     return 0;
120 }
121 输出结果如下:
122 Succeed to create test table now.
123 Insert Succeed.
124 125 126 127 128 129 130 131 132 133 The test table has been dropped.

该结果和上一个例子(普通数据插入)的结果完全相同,只是在执行效率上明显优于前者。

四、数据查询

数据查询是每个关系型数据库都会提供的最基本功能,下面的代码示例将给出如何通过sqlite API获取数据。
1). 创建测试数据表。
2). 插入一条测试数据到该数据表以便于后面的查询
3). 执行SELECT语句检索数据。
4). 删除测试表。
见以下示例代码和关键性注释:

28 29 5. 为后面的查询操作插入测试数据。INSERT INTO TESTTABLE VALUES(20,21.0,'this is a test.') 53 printf(Succeed to insert test data.\n 54 sqlite3_finalize(stmt2); 55 6. 执行SELECT语句查询数据。 57 char* selectsql = SELECT * FROM TESTTABLE 58 sqlite3_stmt* stmt3 = NULL; 59 60 61 sqlite3_finalize(stmt3); 62 sqlite3_close(conn); 63 64 } int fieldCount = sqlite3_column_count(stmt3); do { 67 int r = sqlite3_step(stmt3); if (r == sqlITE_ROW) { 69 0; i < fieldCount; ++i) { 70 这里需要先判断当前记录当前字段的类型,再根据返回的类型使用不同的API函数 71 获取实际的数据值。 72 int vtype = sqlite3_column_type(stmt3,128); line-height:1.5!important"> 73 if (vtype == sqlITE_INTEGER) { 74 int v = sqlite3_column_int(stmt3,128); line-height:1.5!important"> 75 printf(The INTEGER value is %d.\n 76 } else if (vtype == sqlITE_FLOAT) { 77 double v = sqlite3_column_double(stmt3,128); line-height:1.5!important"> 78 printf(The DOUBLE value is %f.\n 79 } if (vtype == sqlITE_TEXT) { 80 char* v = (char*)sqlite3_column_text(stmt3,128); line-height:1.5!important"> 81 printf(The TEXT value is %s.\n 82 } if (vtype == sqlITE_NULL) { 83 printf(This value is NULL.\n 84 } 85 } 86 } if (r == sqlITE_DONE) { 87 printf(Select Finished.\n 88 break; 89 } else { 90 printf(Failed to SELECT.\n 91 sqlite3_finalize(stmt3); 92 sqlite3_close(conn); 93 94 } 95 } while (true); 96 sqlite3_finalize(stmt3); 97 98 7. 为了方便下一次测试运行,我们这里需要删除函数创建的数据表,否则在下次运行时将无法 99 101 sqlite3_stmt* stmt4 = NULL; 102 103 104 sqlite3_finalize(stmt4); 105 sqlite3_close(conn); 106 107 } 108 if (sqlite3_step(stmt4) == sqlITE_DONE) { 109 printf(110 } 111 sqlite3_finalize(stmt4); 112 sqlite3_close(conn); 113 } 114 115 116 { 117 doTest(); 118 119 } 120 Succeed to insert test data. The INTEGER value is 20. The DOUBLE value is 21.000000. The TEXT value is this is a test.. Select Finished. The test table has been dropped.

猜你在找的Sqlite相关文章