forked from arceos-org/arceos-staging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
96 lines (77 loc) · 2.05 KB
/
main.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
#include <sqlite3.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
NotUsed = NULL;
for (int i = 0; i < argc; ++i) {
printf("%s = %s\n", azColName[i], (argv[i] ? argv[i] : "NULL"));
}
printf("\n");
return 0;
}
void exec(sqlite3 *db, char *sql)
{
printf("sqlite exec:\n %s\n", sql);
char *errmsg = NULL;
int rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
if (rc != SQLITE_OK) {
printf("sqlite exec error: %s\n", errmsg);
}
}
void query(sqlite3 *db, char *sql)
{
printf("sqlite query:\n %s\n", sql);
char *errmsg = NULL;
int rc = sqlite3_exec(db, sql, callback, NULL, &errmsg);
if (rc != SQLITE_OK) {
printf("%s\n", errmsg);
}
}
void query_test(sqlite3 *db, const char *args)
{
puts("======== init user table ========");
exec(db, "create table user("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"username TEXT,"
"password TEXT"
")");
puts("======== insert user 1, 2, 3 into user table ========");
char cmd[256] = {0};
sprintf(cmd,
"insert into user (username, password) VALUES ('%s_1', 'password1'), ('%s_2', "
"'password2'), ('%s_3', 'password3')",
args, args, args);
exec(db, cmd);
puts("======== select all ========");
query(db, "select * from user");
puts("======== select id = 2 ========");
query(db, "select * from user where id = 2");
}
void memory()
{
sqlite3 *db;
int ret = sqlite3_open(":memory:", &db);
printf("sqlite open memory status %d \n", ret);
query_test(db, "memory");
}
void file()
{
sqlite3 *db;
int ret = sqlite3_open("file.sqlite", &db);
printf("sqlite open /file.sqlite status %d \n", ret);
if (ret != 0) {
printf("sqlite open error");
return;
}
query_test(db, "file");
sqlite3_close(db);
}
int main()
{
printf("sqlite version: %s\n", sqlite3_libversion());
memory();
file();
return 0;
}