79???????? return;
80???? }
81???? if (sqlite3_step(stmt3) == SQLITE_DONE) {
82???????? printf("The test table has been dropped.\n");
83???? }
84???? sqlite3_finalize(stmt3);
85???? sqlite3_close(conn);
86 }
87
88 int main()
89 {
90???? doTest();
91???? return 0;
92 }
93 //输出结果为:
94 //Succeed to create test table now.
95 //The column count is 3.
96 //The type of 0th column is INTEGER.
97 //The type of 1th column is DOUBLE.
98 //The type of 2th column is TEXT.
99 //The test table has been dropped.
?
二、常规数据插入:
?
??? 1). 创建测试数据表。
??? 2). 通过INSERT语句插入测试数据。
??? 3). 删除测试表。
??? 见以下代码及关键性注释:
?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,当到达结果集末尾时则返回
30???? if (sqlite3_step(stmt) != SQLITE_DONE) {
31???????? sqlite3_finalize(stmt);
32???????? sqlite3_close(conn);
33???????? return;
34???? }
35???? //4. 释放创建表语句对象的资源。
36???? sqlite3_finalize(stmt);
37???? printf("Succeed to create test table now.\n");
38
39???? int insertCount = 10;
40???? //5. 构建插入数据的sqlite3_stmt对象。
41???? const char* insertSQL = "INSERT INTO TESTTABLE VALUES(%d,%f,'%s')";
42???? const char* testString = "this is a test.";
43???? char sql[1024];
44???? sqlite3_stmt* stmt2 = NULL;
45???? for (int i = 0; i < insertCount; ++i) {
46???????? sprintf(sql,insertSQL,i,i * 1.0,testString);
47???????? if (sqlite3_prepare_v2(conn,sql,strlen(sql),&stmt2,NULL) != SQLITE_OK) {
48???????????? if (stmt2)
49???????????????? sqlite3_finalize(stmt2);
50???????????? sqlite3_close(conn);
51???????????? return;
52???????? }
53???????? if (sqlite3_step(stmt2) != SQLITE_DONE) {
54???????????? sqlite3_finalize(stmt2);
55???????????? sqlite3_close(conn);
56???????????? return;
57???????? }
58???????? printf("Insert Succeed.\n");
59???? }
60???? sqlite3_finalize(stmt2);
61???? //6. 为了方便下一次测试运行,我们这里需要删除该函数创建的数据表,否则在下次运行时将无法
62???? //创建该表,因为它已经存在。
63???? const char* dropSQL = "DROP TABLE TESTTABLE";
64???? sqlite3_stmt* stmt3 = NULL;
65???? if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),&stmt3,NULL) != SQLITE_OK) {
66???????? if (stmt3)
67???????????? sqlite3_finalize(stmt3);
68???????? sqlite3_close(conn);
69???????? return;
70???? }
71???? if (sqlite3_step(stmt3) == SQLITE_DONE) {
72???????? printf("The test table has been dropped.\n");
73???? }
74???? sqlite3_finalize(stmt3);
75???? sqlite3_close(conn);
76 }
77
78 int main()
79 {
80???? doTest();
81???? return 0;
82 }
83 //输出结果如下:
84 //Succeed to create test table now.
85 //Insert Succeed