Sqlite3入门简记
时间:2021-07-01 10:21:17
帮助过:19人阅读
#include <stdio.h>
2 #include <sqlite3.h>
3
4 int callback(
void *pv,
int argc,
char **argv,
char **
col)
5 {
6 int cnt_i =
0;
7 for(cnt_i =
0;cnt_i < argc;cnt_i++
)
8 {
9 printf(
"%s\t%s\n",col[cnt_i],argv[cnt_i]);
10 }
11 printf(
"\n");
12 return 0;
13 }
14
15 int main(
void)
16 {
17 sqlite3 *
db;
18 int result =
0;
19 char *rerrmsg =
NULL;
20 char *sql =
NULL;
21 char *data =
"callback";
22
23 result = sqlite3_open(
"sample.db",&
db);
24 if(result >
0)
25 {
26 printf(
"open database err:%s\n",sqlite3_errmsg(db));
27 return -
1;
28 }
29
30 else
31 {
32 printf(
"open database successfully!\n");
33
34 sql =
"CREATE TABLE STUDENT(" 35 "NUM INT PRIMARY KEY NOT NULL," 36 "NAME TEXT NOT NULL," 37 "AGE INT NOT NULL," 38 "SORCE REAL);";
39
40 result = sqlite3_exec(db,sql,callback,NULL,&
rerrmsg);
41 if(result !=
0)
42 {
43 printf(
"creat table err:%s\n",rerrmsg);
44 sqlite3_free(rerrmsg);
45 return -
2;
46 }
47
48 else
49 {
50 printf(
"create table successfully!\n");
51
52 sql =
"INSERT INTO STUDENT(NUM,NAME,AGE,SORCE)" 53 "VALUES(1,‘Paul‘,13,99.1);" 54 "INSERT INTO STUDENT(NUM,NAME,AGE,SORCE)" 55 "VALUES(2,‘Kate‘,15,94.1);" 56 "INSERT INTO STUDENT(NUM,NAME,AGE,SORCE)" 57 "VALUES(3,‘Jim‘,12,95.1);" 58 "INSERT INTO STUDENT(NUM,NAME,AGE,SORCE)" 59 "VALUES(4,‘Tom‘,13,99.4);" 60 "INSERT INTO STUDENT(NUM,NAME,AGE,SORCE)" 61 "VALUES(5,‘Jack‘,13,89.1);";
62
63 result = sqlite3_exec(db,sql,callback,NULL,&
rerrmsg);
64 if(result !=
0)
65 {
66 printf(
"insert data err:%s\n",rerrmsg);
67 sqlite3_free(rerrmsg);
68 return -
3;
69 }
70
71 else
72 {
73 printf(
"insert data successfully!\n");
74
75 sql =
"SELECT * FROM STUDENT";
76 result = sqlite3_exec(db,sql,callback,(
void *)data,&
rerrmsg);
77 if(result !=
0)
78 {
79 printf(
"select data err:%s\n",rerrmsg);
80 sqlite3_free(rerrmsg);
81 return -
4;
82 }
83
84 else
85 {
86 printf(
"select data successfully!\n");
87 }
88 }
89 }
90 }
91
92 sqlite3_close(db);
93
94 return 0;
95 }
View Code
六,图形界面管理工具
SQLite Expert - Personal Edition ,简单入门,直接导入数据库文件即可
以上,
2017/03/30
Sqlite3入门简记
标签:primary bsp 错误代码 database into rms i++ 命令行界面 自己