当前位置:网站首页>嵌入式数据库开发编程(零)
嵌入式数据库开发编程(零)
2022-07-05 04:56:00 【光追雨】
文章目录
一 数据库
1.1 在ubuntu中安装数据库
sudo apt-get install sqlite3 libsqlite3-dev
安装测试:
输入sqlite3,如果能够成功进入数据库,说明安装成功:
1.2 数据库的操作
1.2.1 数据库命令的分类
系统命令:是以.开头的命令,主要用于对当前数据库操作
注意:系统命令后面不能加;
普通命令:是以;结尾的命令,主要对数据库中的表进行操作
注意:普通命令前不能加 .
1.2.2 常用的系统命令
.help:查看帮助信息,列出所有的系统命令
.exit: 退出数据库
.quit:退出数据库
.databases:查看当前数据库信息
.tables:列出当前数据库中所有表的表名
.schema:列出数据库中所有表的结构
1.2.3 数据中的常用的语句
-- 进入sqlite3
sqlite3 student.db
-- 退出数据库
.exit
-- 创建一张表
create table stu(id int,name char,sex char,score int);
注意:字符或者字符串的类型指定为char或者text
-- 查看表结构
.schema
-- 插入数据
insert into stu (id,name,sex,score) values(1001,"zhangsan",'m',98);
insert into stu values(1002,"lisi",'f',99);
insert into stu values(1003,"wanger",'m',100),(1004,"xiaoming",'f',101);
--查询数据
select * from stu; //查找所有记录
select *from stu where sex='f'; //查找符合条件的记录
select *from stu where sex='f' and id = 1002;
select *from stu where sex='f' or id = 1001;
select id,name from stu; //只查询id和name
-- 删除记录
delete from stu where id = 1001;
-- 修改记录
update stu set score=77 where id = 1003;
--添加一列
alter table stu add column vip int;
--删除一列
sqlites不允许直接删除一列
1)先创建一张新表
create table stu1 as select id,name from stu;
2) 删除原来的旧表
drop table stu;
3)对新表进行重命名
alter table stu1 rename to stu;
-- 数据库主键(既设置的数据将会是唯一的存在的)
create table usr(name text primary key,passwd text);
-- 删除表
drop table stu;
1.3 sqlite数据库中常用api
1.3.1 sqlite3_open
头文件:#include <sqlite3.h>
原型:int sqlite3_open(const char *filename,sqlite3 **ppDb);
功能:打开或者创建一个数据库
参数:
filename:数据库的名字
PPdb:操作数据库的指针,句柄。
返回值:
成功:返回:SQLITE_OK
失败:error_code 可通过sqlite3_errmsg获取错误信息
1.3.2 int sqlite3_close(sqlite3 * db)
功能:关闭一个数据库
1.3.3 sqlite3_exec
头文件:#include <sqlite3.h>
原型:int sqlite3_exec(sqlite3 db,const char sql,int (callback)(void ,int,char,char),void *arg,char errmsg);
功能:执行一条sql语句
参数:
db:数据库的句柄指针
sql:将要被执行sql语句
callback:回调函数,只有在查询语句时,才会给回调函数传参
arg:为callback传参的
errmsg:错误信息地址
返回值:
成功:返回:SQLITE_OK
失败:error_code 可通过sqlite3_errmsg获取错误信息
******************************************************
int (*callback)(void *arg,int ncolumn,charf_value,char**f_name)
功能:得到查询的结果
参数:
arg:为回调函数传参使用的
ncolumn:记录包含的字段的数目(列数)
f_value:包含每个字段值的指针数组
f_name:包含每个字段名称的指针数组
返回值:
成功 0
出错:非0
1.3.4 sqlite3_get_table
原型:int sqlite3_get_table(sqlite3 *db,const char *sql,char ***pazresult,int *pnRow,int *pnColumn,char **pzErrmsg);
功能:查询数据库,它会创建一个新的内存区域来存放查询的结果信息
参数:
db:数据库的句柄指针
sql:将要被执行sql语句
pazresult:查询的结果
pnRow:行数
pnColumn 列数
pzErrmsg 错误信息
返回值:
成功 0
出错:errcode
pnRow的值为查询到的符合条件的记录数(不包含字段名)。
pnColumn的值为查询到的符合条件的字段数。
1.3.5 void sqlite3_free_table(char ** result)
功能:释放内存
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
int print(void *arg,int column,char **value,char **name) //column为列数,name:字段名 value:数据
{
int i;
for(i = 0 ; i < column;i++)
{
printf("%s = %s ",name[i],value[i]);
}
printf("\n");
return 0;
}
int main(int argc, char const *argv[])
{
sqlite3 *ppdb;
//打开或者创建数据库
int ret = sqlite3_open("stu.db",&ppdb);
if(ret != SQLITE_OK)
{
printf("sqlite3 open: %s\n",sqlite3_errmsg(ppdb));
return -1;
}
//创建表
char sql[128] = {
0};
sprintf(sql,"create table if not exists stu(id int,name char,sex char,score int);");
ret = sqlite3_exec(ppdb,sql,NULL,NULL,NULL);
if(ret != SQLITE_OK)
{
printf("sqlite3_exec: %s\n",sqlite3_errmsg(ppdb));
return -1;
}
//插入数据
int i;
int id,score;
char name[32] = {
0};
char sex;
/*for(i = 0; i < 2;i++) { printf("请输入学号、姓名、性别、分数:\n"); scanf("%d%s %c %d",&id,name,&sex,&score); memset(sql,0,sizeof(sql)); sprintf(sql,"insert into stu values(%d,'%s','%c',%d);",id,name,sex,score); ret = sqlite3_exec(ppdb,sql,NULL,NULL,NULL); if(ret != SQLITE_OK) { printf("sqlite3_exec2: %s\n",sqlite3_errmsg(ppdb)); return -1; } }*/
memset(sql,0,sizeof(sql));
sprintf(sql,"select * from stu;");
/*ret = sqlite3_exec(ppdb,sql,print,NULL,NULL); if(ret != SQLITE_OK) { printf("sqlite3_exec: %s\n",sqlite3_errmsg(ppdb)); return -1; }*/
char **result;
int row,column;
ret = sqlite3_get_table(ppdb,sql,&result,&row,&column,NULL); //row:行 column:列
if(ret != SQLITE_OK)
{
printf("sqlite3_get_table : %s\n",sqlite3_errmsg(ppdb));
return -1;
}
int j;
int Index = column;
for(i = 0; i < row;i++) //行
{
for(j = 0; j < column;j++) //列
{
printf("%s = %s ",result[j],result[Index]);
Index++;
}
putchar(10);
}
return 0;
}
边栏推荐
- 2021 huashubei mathematical modeling idea + reference + paper
- 669. Prune binary search tree ●●
- 質量體系建設之路的分分合合
- Basic knowledge points of dictionary
- Panel panel of UI
- [ideas] 2021 may day mathematical modeling competition / May Day mathematical modeling ideas + references + codes
- Unity enables mobile phone vibration
- 54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
- Unity find the coordinates of a point on the circle
- AutoCAD - stretching
猜你喜欢

django连接数据库报错,这是什么原因

AutoCAD - feature matching

PostgreSQL 超越 MySQL,“世界上最好的编程语言”薪水偏低

Special information | real estate and office buildings - 22.1.9

AutoCAD - continuous annotation

2021 Higher Education Club Cup mathematical modeling national tournament ABCD problem - problem solving ideas

2022 thinking of Mathematical Modeling B problem of American college students / analysis of 2022 American competition B problem

AutoCAD - isometric annotation

Detailed introduction of OSPF header message
![Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]](/img/e7/f699ee982ea325b8d04f8bd467a559.jpg)
Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]
随机推荐
Data is stored in the form of table
猿人学第一题
中国溶聚丁苯橡胶(SSBR)行业研究与预测报告(2022版)
2022/7/1学习总结
Pdf to DWG in CAD
cocos_ Lua listview loads too much data
669. Prune binary search tree ●●
2021 electrician cup idea + code - photovoltaic building integration plate index development trend analysis and prediction: prediction planning issues
Animation
JVM 原理和流程简介
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
中国聚氨酯硬泡市场调研与投资预测报告(2022版)
Unity intelligent NPC production -- pre judgment walking (method 1)
UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
3dsmax snaps to frozen objects
#775 Div.1 B. Integral Array 数学
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
Forecast report on research and investment prospects of Chinese wormwood industry (2022 Edition)
Sqlserver stored procedures pass array parameters
Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?