SQLite学习手册(实例代码<二>)
三、高效的批量数据插入: 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. 该结果和上一个例子(普通数据插入)的结果完全相同,只是在执行效率上明显优于前者。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |