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

2015-01-25 19:42:20 · 作者: · 浏览: 14
s 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,2,i * 1.0);
?71???????? sqlite3_bind_text(stmt3,3,strData,strlen(strData),SQLITE_TRANSIENT);
?72???????? if (sqlite3_step(stmt3) != SQLITE_DONE) {
?73???????????? sqlite3_finalize(stmt3);
?74???????????? sqlite3_close(conn);
?75???????????? return;
?76???????? }
?77???????? //重新初始化该sqlite3_stmt对象绑定的变量。
?78???????? sqlite3_reset(stmt3);
?79???????? printf("Insert Succeed.\n");
?80???? }
?81???? sqlite3_finalize(stmt3);
?82
?83???? //8. 提交之前的事物。
?84???? const char* commitSQL = "COMMIT";
?85???? sqlite3_stmt* stmt4 = NULL;
?86???? if (sqlite3_prepare_v2(conn,commitSQL,strlen(commitSQL),&stmt4,NULL) != SQLITE_OK) {
?87???????? if (stmt4)
?88???????????? sqlite3_finalize(stmt4);
?89???????? sqlite3_close(conn);
?90???????? return;
?91???? }
?92???? if (sqlite3_step(stmt4) != SQLITE_DONE) {
?93???????? sqlite3_finalize(stmt4);
?94???????? sqlite3_close(conn);
?95???????? return;
?96???? }
?97 ????sqlite3_finalize(stmt4);
?98
?99???? //9. 为了方便下一次测试运行,我们这里需要删除该函数创建的数据表,否则在下次运行时将无法
100???? //创建该表,因为它已经存在。
101???? const char* dropSQL = "DROP TABLE TESTTABLE";
102???? sqlite3_stmt* stmt5 = NULL;
103???? if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),&stmt5,NULL) != SQLITE_OK) {
104???????? if (stmt5)
105???????????? sqlite3_finalize(stmt5);
106???????? sqlite3_close(conn);
107???????? return;
108???? }
109???? if (sqlite3_step(stmt5) == SQLITE_DONE) {
110???????? printf("The test table has been dropped.\n");
111???? }
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 //Insert Succeed.
125 //Insert Succeed.
126 //Insert Succeed.
127 //Insert Succeed.
128 //Insert Succeed.
129 //Insert Succeed.
130 //Insert Succeed.
131 //Insert Succeed.
132 //Insert Succeed.
133 //The test table has been dropped.
?
??? 该结果和上一个例子(普通数据插入)的结果完全相同,只是在执行效率上明显优于前者。
?
四、数据查询:
?
??? 数据查询是每个关系型数据库都会提供的最基本功能,下面的代码示例将给出如何通过SQLite API获取数据。
??? 1). 创建测试数据表。
??? 2). 插入一条测试数据到该数据表以便于后面的查询。
??? 3). 执行SELECT语句检索数据。
??? 4). 删除测试表。
??? 见以下示例代码和关键性注释:
? 1 #include
? 2 #include
? 3 #include
? 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???????? return;
?26???? }
?27???? //3. 通过sqlite3_step命令执行创建表的语句。对于DDL和DML语句而言,sqlite3_step执行正确的返回值
?28???? //只有SQLITE_DONE,对于SELECT查询而言,如果有数据返回SQLITE_ROW,当到达结果集末尾时则返回
?29???? //SQLI