-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsqlite3.c
112 lines (102 loc) · 3.15 KB
/
sqlite3.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <kcs/sqlite3.h>
void error(sqlite3_t *db, const char *msg)
{
printf("%s: ", msg);
const char *err = sqlite3_last_errmsg(db);
if (err) {
printf("%s\n", err);
} else {
printf("Unknown error\n");
}
}
int main(void)
{
sqlite3_t *db = NULL;
sqlite3_stmt_t *stmt = NULL;
db = sqlite3_open("sample.db", SQLITE_NO_TIMEOUT);
if (!db) {
error(db, "File open error");
goto END;
}
int r = sqlite3_exec(db,
"CREATE TABLE IF NOT EXISTS SampleTable "
"("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"name CHAR(32) NOT NULL"
")"
);
if (r != SQLITE_OK) {
error(db, "Create table error");
goto END;
}
int rows = 0;
stmt = sqlite3_prepare(db, "SELECT count(*) FROM SampleTable");
if ((r = sqlite3_step(stmt)) == SQLITE_ROW) {
const char *name = sqlite3_column_name(stmt, 0);
rows = sqlite3_column_int(stmt, 0);
printf("rows check: %s = %d\n", name, rows);
sqlite3_finalize(stmt);
stmt = NULL;
}
if (rows == 0) {
r = sqlite3_exec(db, "INSERT INTO SampleTable(id,name) VALUES(10, 'aaa')");
r = sqlite3_exec(db, "INSERT INTO SampleTable(id,name) VALUES(20, 'bbb')");
r = sqlite3_exec(db, "INSERT INTO SampleTable(id,name) VALUES(30, 'ccc')");
if (r != SQLITE_OK) {
error(db, "Data insertion error");
goto END;
}
}
stmt = sqlite3_prepare(db, "SELECT * FROM SampleTable WHERE id > ?");
if (!stmt) {
error(db, "Create statement error");
goto END;
}
r = sqlite3_bind_int(stmt, 1, 10);
if (r != SQLITE_OK) {
error(db, "Binding error");
goto END;
}
int row = 0;
while ((r = sqlite3_step(stmt)) == SQLITE_ROW) {
printf("[row:%d]\n", ++row);
int cols = sqlite3_column_count(stmt);
for (int i = 0; i < cols; ++i) {
int type = sqlite3_column_type(stmt, i);
printf(" [col:%d] ", i);
const char *name = sqlite3_column_name(stmt, i);
switch (type) {
case SQLITE_INTEGER:
printf("%s = %d\n", name, sqlite3_column_int(stmt, i));
break;
case SQLITE_FLOAT:
printf("%s = %f\n", name, sqlite3_column_double(stmt, i));
break;
case SQLITE_TEXT:
printf("%s = %s\n", name, sqlite3_column_text(stmt, i));
break;
case SQLITE_BLOB:
printf("%s = %p, len(%d)\n", name, sqlite3_column_blob(stmt, i), sqlite3_column_bytes(stmt, i));
break;
case SQLITE_NULL:
printf("%s = (NULL)\n", name);
break;
}
}
}
if (r != SQLITE_DONE){
error(db, "Reading data error");
goto END;
}
END:
if (stmt) {
sqlite3_finalize(stmt);
}
if (db) {
sqlite3_close(db);
}
return 0;
}