背景音乐
SQLite 是不区分大小写的
数据库的 int = integer
from √ / form ×
alter /ˈɔːltər/ 修改
column (恐龙=细长=列) 列
vim 恢复操作:ctrl+r
SQLite3 函数成功都是 SQLITE_OK
第一节 了解
大型数据库 | oracle 公司的 oracle 关系数据库 IBM 的 DB2 |
---|---|
中型数据库 | 微软的 Server |
小型数据库 | oracle 公司的 mySQL |
第二节 SQLite 安装与命令行
SQLite 安装 | Linux 操作系统都自带 ubantu 验证如下 ![]() |
---|---|
SQLite 的菜鸟教程 | https://www.runoob.com/sqlite/sqlite-tutorial.html |
系统命令 | 点开头 例如 .help 查看表的结构图 == .schema ![]() 查看有哪些表 .table |
sql 命令(;结尾) | 进入 sqlite3 命令行 == sqlite3 退出 sqlite3 命令行 == .q / .exit 创建 yqj 表 == create table yqj ( name char , age integer ); 插入一条数据 == insert into yqj values("吃番茄的大灰狼",21); == insert into (name , age) values ("吃番茄的大灰狼",21) ; 查看所有数据 == select * from yqj; == select name ,age from yqj; == select name,age from yqj where age = 21 and name = "吃番茄的大灰狼"; == select name,age from yqj where age = 21 or name = "吃番茄的大灰狼"; 删除一条记录 == delete from yqj where name ="吃番茄的大灰狼" and age = 21; 整个表全部删除(但是表还在)== delete from yqj; 更新一个记录 == update yqj set age = 100 where name = "吃蕃茄的大灰狼"; 添加一列 (性别)== alter table yqj add column six char; drop 掉整个表 == drop table yqj; 修改表的名字 == alter table yqj rename to mytable; |
第三节 SQLite C 接口
sqlite3 函数接口参考手册(不能用 man) | https://www.runoob.com/sqlite/sqlite-c-cpp.html |
---|---|
头文件 | #include |
3.1 sqlite3_open 函数
int sqlite3_open(char *path, salite3**db): |
---|
功能:打开/创建 sqlite3 数据库 |
db:数据库 |
返回值:成功 == SQLITE_OK |
3.2 sqlite3_exec 函数
sqlite3_exec(sqlite3db, const char sql, int (callback)(void,int,char,char*), void \arg, char **errmsg) |
---|
参数 1:数据库 |
参数 2:一条 sql 指令 |
参数 3:callback 回调函数,用于接收 sql 的返回的数据 |
参数 4:回调函数的传参 |
参数 5:字符串指针,用于接收错误消息 |
返回值:成功 SQLITE_OK |
3.2 sqlite_close 函数
int sqlite3_close(sqlite3*db); |
---|
关闭数据库 |
返回值:成功 SQLITE_OK |
3.3 回调函数的传参格式
18 背下来
19 int SQLite_callback(void*argc,int f_num,char**f_value,char**f_name)
20 {
21 //para = 传递给回调函数的参数
22 //f_num = 传回的字段数目
23 //f_value = 字段值的二维指针
24 //f_name = 字段名的二维指针
25 int i;
26 printf("\n回调函数接受到参数:%d\n",*(int*)argc);
27 for(i=0; i 28 {
29 printf("%s = %s\n", f_name[i],f_value[i]);
30 }
31
32 printf("\n");
33
34 return 1;
35 }
36
示例
11 ================================================================*/
12 #include
13 #include
14 #include
15 #include
16 #include
17
18 //背下来
19 int SQLite_callback(void*argc,int f_num,char**f_value,char**f_name)
20 {
21 //para = 传递给回调函数的参数
22 //f_num = 传回的字段数目
23 //f_value = 字段值的二维指针
24 //f_name = 字段名的二维指针
25 int i;
26 printf("\n回调函数接受到参数:%d\n",*(int*)argc);
27 for(i=0; i 28 {
29 printf("%s = %s\n", f_name[i],f_value[i]);
30 }
31
32 printf("\n");
33
34 return 1;
35 }
36
37
38 void main()
39 {
40 sqlite3 * mydb;
41
42 //打开数据库
43 if(SQLITE_OK == sqlite3_open("mydb1.db",&mydb))
44 {
45 printf("\nsqlite_open success\n");
46 }
47 else
48 {
49 perror("\nsqlite3_open error:\n");
50 }
51
52 //sprintf(cmd,"INSERT INTO yqj VALUES(\"%s\",%d);","吃蕃茄",21);
53
54 //创建yqj表
55 char * SQLite_error = {0};
56 if(SQLITE_OK == sqlite3_exec(mydb,"create table yqj(name char,age integer);",NULL,NULL,&SQLite_error))
57 {
58 printf("\n1 sqlite3_exec success\n");
59 }
60 else
61 {
62 printf("\nsqlite3_exec error:%s\n",SQLite_error);
63 }
64 //执行select*from yqj指令
65 //先执行完回调函数,函数再返回
66 if(SQLITE_OK == sqlite3_exec(mydb,"select * from yqj;",SQLite_callback,(void*)777,NULL))
67 {
68 printf("\n3 sqlite3_exec success\n");
69 }
70 else
71 {
72 printf("\nsqlite3_exec error:%s\n",SQLite_error);
73 }
74 //关闭sqlite3数据库
75 close(mydb);
76 }