1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#include "sqlite3.h"
int
_tmain(
int
argc, _TCHAR* argv[])
{
sqlite3* db = NULL;
sqlite3_open_v2(
"test.sqlite"
, &db, SQLITE_OPEN_READWRITE, NULL);
// 执行下面代码前先手动清空数据库
#define test1
#ifdef test1
sqlite3_exec(db,
"INSERT INTO tttt (intval, strval) VALUES (0, 'abc')"
, NULL, NULL, NULL);
sqlite3_exec(db,
"INSERT INTO tttt (intval, strval) VALUES (1, 'def')"
, NULL, NULL, NULL);
// 看下数据库,一定没问题,妥妥的2条数据
#endif
int
result = -1;
sqlite3_stmt * stmt = NULL;
#ifdef test2
sqlite3_prepare(db,
"INSERT INTO tttt (intval, strval) VALUES (2, 'abc')"
, -1, &stmt, NULL);
result = sqlite3_step(stmt);
printf
(
"%d\n"
, result);
// SQLITE_DONE:101
result = sqlite3_step(stmt);
printf
(
"%d\n"
, result);
// SQLITE_DONE:101
sqlite3_finalize(stmt);
// 看下数据库,一定没问题,妥妥的2条数据
#endif
#define BIND_IDX(STMT, NAME) sqlite3_bind_parameter_index(STMT, NAME)
#ifdef test3
sqlite3_prepare(db,
"INSERT INTO tttt (intval, strval) VALUES (@intval, 'abc')"
, -1, &stmt, NULL);
sqlite3_bind_int(stmt, BIND_IDX(stmt,
"@intval"
), 33);
result = sqlite3_step(stmt);
printf
(
"%d\n"
, result);
// SQLITE_DONE:101
result = sqlite3_step(stmt);
printf
(
"%d\n"
, result);
// SQLITE_DONE:101
sqlite3_finalize(stmt);
// 看下数据库,一定没问题,妥妥的2条数据
#endif
#ifdef test4
sqlite3_prepare(db,
"INSERT INTO tttt (intval, strval) VALUES (@intval, 'abc')"
, -1, &stmt, NULL);
sqlite3_bind_int(stmt, BIND_IDX(stmt,
"@intval"
), 3);
result = sqlite3_step(stmt);
printf
(
"%d\n"
, result);
// SQLITE_DONE:101
//sqlite3_reset(stmt); // 没有这句,表里会出现2条都是3的数据,也就是说下面这句没起作用
sqlite3_bind_int(stmt, BIND_IDX(stmt,
"@intval"
), 4);
result = sqlite3_step(stmt);
printf
(
"%d\n"
, result);
// SQLITE_DONE:101
sqlite3_finalize(stmt);
// 得出结论: 一旦step过了之后,形成在stmt内部的
#endif
#ifdef test5
char
str[4] = {
'a'
,
'b'
,
'c'
,
'\0'
};
sqlite3_prepare(db,
"INSERT INTO tttt (intval, strval) VALUES (5, @strval)"
, -1, &stmt, NULL);
// 下面2句 2选1
sqlite3_bind_text(stmt, BIND_IDX(stmt,
"@strval"
), str, -1, SQLITE_STATIC);
// 数据库里是'bbc'
//sqlite3_bind_text(stmt, BIND_IDX(stmt,"@strval"), str, -1, SQLITE_TRANSIENT); // 数据库里是'abc'
str[0] =
'b'
;
result = sqlite3_step(stmt);
printf
(
"%d\n"
, result);
// SQLITE_DONE:101
sqlite3_finalize(stmt);
// 得出结论: SQLITE_STATIC就是给了个指针进去, 而SQLITE_TRANSIENT是把指针指向的数据复制到了stmt内部
#endif
#ifdef test6
// 下面4句 4选1
sqlite3_exec(db,
"begin transaction"
, NULL, NULL, NULL);
//sqlite3_exec(db, "begin", NULL, NULL, NULL);
//sqlite3_exec(db, "begin;", NULL, NULL, NULL);
//sqlite3_exec(db, "begin wo cao", NULL, NULL, NULL);
sqlite3_exec(db,
"INSERT INTO tttt (intval, strval) VALUES (6, 'abc')"
, NULL, NULL, NULL);
sqlite3_exec(db,
"commit transaction"
, NULL, NULL, NULL);
// 正常插入1条数据
//sqlite3_exec(db, "commit", NULL, NULL, NULL); // 正常插入1条数据
//sqlite3_exec(db, "commit;", NULL, NULL, NULL); // 正常插入1条数据
//sqlite3_exec(db, "commit wo cao", NULL, NULL, NULL); // 正常插入1条数据
// 得出结论: 前面那个词正确就行了!
#endif
sqlite3_close(db);
getchar
();
return
0;
}
本文转载,未测试和实现,觉得不错就转了。
|