加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-12 19:48:49 所属栏目:百科 来源:网络整理
导读:一、获取表的Schema信息: 1). 动态创建表。 2). 根据sqlite3提供的API,获取表字段的信息,如字段数量以及每个字段的类型。 3). 删除该表。 见以下代码及关键性注释: 1 #include sqlite3.h 2 #include string 3 4 using namespace std; 5 6 void doTest()

一、获取表的Schema信息:

1). 动态创建表。
2). 根据sqlite3提供的API,获取表字段的信息,如字段数量以及每个字段的类型。
3). 删除该表。
见以下代码及关键性注释:

 1 #include <sqlite3.h>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 void doTest()
 7 {
 8     sqlite3* conn = NULL;
 9     //1. 打开数据库
10     int result = sqlite3_open("D:/mytest.db",&conn);
11     if (result != SQLITE_OK) {
12         sqlite3_close(conn);
13         return;
14     }
15     const char* createTableSQL = 
16         CREATE TABLE TESTTABLE (int_col INT,float_col REAL,string_col TEXT)";
17     sqlite3_stmt* stmt = NULL;
18     int len = strlen(createTableSQL);
19     2. 准备创建数据表,如果创建失败,需要用sqlite3_finalize释放sqlite3_stmt对象,以防止内存泄露。20     if (sqlite3_prepare_v2(conn,createTableSQL,len,&stmt,NULL) != SQLITE_OK) {
21         if (stmt)
22             sqlite3_finalize(stmt);
23         sqlite3_close(conn);
24         25     }
26     3. 通过sqlite3_step命令执行创建表的语句。对于DDL和DML语句而言,sqlite3_step执行正确的返回值
27     只有SQLITE_DONE,对于SELECT查询而言,如果有数据返回SQLITE_ROW,当到达结果集末尾时则返回
28     SQLITE_DONE。29     if (sqlite3_step(stmt) != SQLITE_DONE) {
30         sqlite3_finalize(stmt);
31         sqlite3_close(conn);
32         33     }
34     4. 释放创建表语句对象的资源。35     sqlite3_finalize(stmt);
36     printf(Succeed to create test table now.n");
37     5. 构造查询表数据的sqlite3_stmt对象。38     char* selectSQL = SELECT * FROM TESTTABLE WHERE 1 = 039     sqlite3_stmt* stmt2 = NULL;
40     41         if (stmt2)
42             sqlite3_finalize(stmt2);
43         sqlite3_close(conn);
44         45     }
46     6. 根据select语句的对象,获取结果集中的字段数量。47     int fieldCount = sqlite3_column_count(stmt2);
48     printf(The column count is %d.n49     7. 遍历结果集中每个字段meta信息,并获取其声明时的类型。    50     for (int i = 0; i < fieldCount; ++i) {
51         由于此时Table中并不存在数据,再有就是SQLite中的数据类型本身是动态的,所以在没有数据时
52         无法通过sqlite3_column_type函数获取,此时sqlite3_column_type只会返回SQLITE_NULL,
53         直到有数据时才能返回具体的类型,因此这里使用了sqlite3_column_decltype函数来获取表声
54         明时给出的声明类型。55         string stype = sqlite3_column_decltype(stmt2,i);
56         stype = strlwr((char*)stype.c_str());
57         下面的解析规则见该系列的“数据类型-->1. 决定字段亲缘性的规则”部分,其链接如下:
58         http://www.cnblogs.com/stephen-liu74/archive/2012/01/18/2325258.html59         if (stype.find(int") != string::npos) {
60             printf(The type of %dth column is INTEGER.n61         } else charstring::npos
62             || stype.find(text63             printf(The type of %dth column is TEXT.n64         } realstring::npos 
65             || stype.find(floa66             || stype.find(doubstring::npos ) {
67             printf(The type of %dth column is DOUBLE.n68         }
69     }
70     sqlite3_finalize(stmt2);
71     8. 为了方便下一次测试运行,我们这里需要删除该函数创建的数据表,否则在下次运行时将无法
72     //创建该表,因为它已经存在。73     char* dropSQL = DROP TABLE TESTTABLE74     sqlite3_stmt* stmt3 = NULL;
75     76         if (stmt3)
77             sqlite3_finalize(stmt3);
78         sqlite3_close(conn);
79         80     }
81     if (sqlite3_step(stmt3) == SQLITE_DONE) {
82         printf(The test table has been dropped.n83     }
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). 删除测试表。
3 #include <stdio.h> 4 5 6 7 8 { 9 sqlite3* conn = NULL; 12 13 sqlite3_close(conn); 14 15 } 16 17 18 sqlite3_stmt* stmt = NULL; 21 22 23 sqlite3_finalize(stmt); 24 sqlite3_close(conn); 25 26 } 29 30 31 sqlite3_finalize(stmt); 32 sqlite3_close(conn); 33 34 } 35 36 sqlite3_finalize(stmt); 37 printf(38 39 int insertCount = 10; 5. 构建插入数据的sqlite3_stmt对象。41 char* insertSQL = INSERT INTO TESTTABLE VALUES(%d,%f,'%s')42 char* testString = this is a test.43 char sql[1024]; 44 sqlite3_stmt* stmt2 = NULL; 45 0; i < insertCount; ++i) { 46 sprintf(sql,insertSQL,i,i * 1.0,testString); 47 48 49 sqlite3_finalize(stmt2); 50 sqlite3_close(conn); 51 52 } if (sqlite3_step(stmt2) != SQLITE_DONE) { 54 sqlite3_finalize(stmt2); 55 sqlite3_close(conn); 56 57 } 58 printf(Insert Succeed.n59 } 60 sqlite3_finalize(stmt2); 61 6. 为了方便下一次测试运行,我们这里需要删除该函数创建的数据表,否则在下次运行时将无法 62 63 64 sqlite3_stmt* stmt3 = NULL; 65 66 67 sqlite3_finalize(stmt3); 68 sqlite3_close(conn); 69 70 } 72 printf(73 } 74 sqlite3_finalize(stmt3); 75 sqlite3_close(conn); 76 } 77 78 79 { 80 doTest(); 82 } 83 输出结果如下: 84 85 Insert Succeed. 86 87 89 90 91 92 The test table has been dropped.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读