目录
1.两种创建方式--- 创建一张表 create table
2.插入一条记录--- 往表中添加数据 insert into
3.sqlite3_exec ------执行相关的sql语句
sqlite3_free_table ---- 释放堆区空间
一.安装
安装数据库管理系统
sudo apt-get install sqlite3
查看是否装好
sqlite3 --version
二.进入
进入数据库管理系统
aqlite3
查看使用方法 -- .help
退出 .quit / .exit
三.打开一个数据库
sqlite3 stu.db(文件) ------- db : database 数据库
输入命令 sqlite3 stu.db
再输入 .database
四.教程
语句
1.增 -- 不区分大小写
CREATE TABLE 表名(
column1 datatype PRIMARY KEY(one or more columns),
column2 datatype,
column3 datatype,
.....
columnN datatype,
);
1.两种创建方式--- 创建一张表 create table
必须以分号结尾
2.插入一条记录--- 往表中添加数据 insert into
INSERT INTO TABLE_NAME [(column1, column2, column3,...columnN)]
VALUES (value1, value2, value3,...valueN);
//(name,sex,age,score);
insertinto表名values"tom""ma1e",19,99.5);
eg:
//全部列都给到insert into stu values ("tom","male",19,99.5) ;
/给部分列插入数据
irsert into stu(name,sex) values ("jack","male");
2.删
1.delete 删除记录:
delete from 表名 where 条件;
2.drop 删除整张表
语法:
drop table 表名称
不支持删字段
3.改
1.添加字段(alter):
ALTER TABLE database_name.table_name RENAME TO new_table_name;
alter table 表名 add 列名;
eg:
alter talbe stu add sno;
2.修改表中的数据(update):
update表名set列1=值1【,列2=值2,...][匹配条件];
条件语句:
where 字句
where 列名 操作符 列值
where age > 10
update stu set sno=110 where name="tom";
注意:等于号前后不能有空格
一次插入多条
4.查
SELECT column1, column2, columnN FROM table_name;
select * from stu
查看所有
以什么格式
=========================================================================
1.查询部分字段 --- select 列名 ...
2. in 字段
3.and 语句
4.or 或
5.between A and B
6. select + like
可实现模糊查找
7. order by
补充
后面项目记录信息时间
SQLite 日期 & 时间 | 菜鸟教程 ----查看函数
datetime ---- 记录当前时间
操作:
插入时间
编号自动增长
order by
五.可视化
SQL 语句粘贴进去可创建
导入 导出 --------- 或者直接拷文件
六.编程:
1.打开
代码
不是系统提供的-----都需要链接库文件
2.关闭
3.sqlite3_exec ------执行相关的sql语句
查询类(select)命令 ----- 会需要回调函数指针
无分号
有分号
已插入
回调函数指针
查询结果是几条----回调函数就会被执行几次
4.sqlite3_get_table
两者类似
大规模的数据:用exec
小数据:用get_table
***resultp ---- 因为数据放在堆区,将指针数组的数组名(二级指针)带出来,需要三级指针
段错误:因为被调修改主调
sqlite3_free_table ---- 释放堆区空间
对比
总结
练习
#include "../head.h"
int flag = 0;
int callback(void *arg,int n_col,char **p_val,char **p_name)
{
// static int flag = 0;//让变量的 “生命周期延长到整个程序运行期间”,且 “初始化仅执行一次”
int i = 0;
if(flag == 0)
{
for(i = 0;i < n_col;i++)
{
if(i == 1)
{
printf("%-17s",p_name[i]);
continue;
}
printf("%s\t",p_name[i]);
}
putchar('\n');
}
flag++;
for(i = 0;i < n_col;i++) // col = 3
{
if(i == 1)
{
printf("%-17s", p_val[i]);
continue;
}
printf("%s\t",p_val[i]);
}
putchar('\n');
// //只打印前20行 //select * from dict limit 20; 自己会看多少行
// if(flag == 20)
// {
// return 0;
// }
return 0;
}
//修改回调函数打印格式每次会刷新
//修改'跳过空格'insert内容格式----需要每次删除文件
int main(int argc,const char *argv[])
{
FILE *fp = NULL;
sqlite3 *db = NULL;
int ret = sqlite3_open("dict.db",&db);
if(ret != SQLITE_OK)
{
printf("sqlite3_open fail");
return -1;
}
char create[1024] = {"create table if not exists dict (id INTEGER PRIMARY KEY ASC,word char,explain char);"};
char *errmsg = NULL;
ret = sqlite3_exec(db,create,NULL,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("create fail");
return -1;
}
fp = fopen("dict.txt","r");
if(NULL == fp)
{
perror("fail to fopen");
return -1;
}
char line[1024] = {0};
char *word;
char *explain;
char sql[1024] = {0};
//fdopen 可以转成file *
//insert dict
while(1)
{
//fgets不会去掉从终端接收的\n字符,遇到\n读取截止
char *get = fgets(line,sizeof(line),fp);
if(get == NULL)//读到文件末尾返回NULL
{
break;
}
// line[strlen(line) - 2] ='\0'; //去掉\r\n
//分割
word = strtok(line," ");
if (word == NULL) // 无单词,跳过该行
{
continue;
}
explain = strtok(NULL,"\r\n");
if (explain == NULL) // 无释义,设为空字符串 --这样就不是空指针了--"hello"是指针--字符串常量退化为指针--字符串常量 在只读数据区
{
explain = "";
}
// // printf("==%s\n",explain);
while(*explain == ' ')
{
explain++;
}
sprintf(sql,"insert into dict values (NULL,\"%s\",\"%s\");",word,explain);
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("sqlite3_exec insert fail: %s\n",errmsg);
return -1;
}
}
printf("dict.txt 插入成功\n");
//输入命令
while(1)
{
//查询
char sqlin[1024] = {0};
printf("\033[33m my_sqlite3> \033[0m");
fgets(sqlin,sizeof(sqlin),stdin);
sqlin[strlen(sqlin) - 1] = '\0';
//.quit
if(!strcmp(sqlin,".quit"))
{
break;
}
flag = 0;
ret = sqlite3_exec(db,sqlin,callback,NULL,&errmsg);
if (ret != SQLITE_OK)
{
printf("sqlite3_exec fail: %s\n",errmsg);
return -1;
}
}
fclose(fp);
sqlite3_close(db);
return 0;
}