当前位置:网站首页>嵌入式数据库开发编程(零)
嵌入式数据库开发编程(零)
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;
}
边栏推荐
- Judge the position of the monster in the role under unity3d
- [groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
- 【Leetcode】1352. Product of the last K numbers
- 2022 thinking of mathematical modeling D problem of American college students / analysis of 2022 American competition D problem
- 2022/7/2做题总结
- cocos_ Lua loads the file generated by bmfont fnt
- Data is stored in the form of table
- "Measuring curve length" of CAD dream drawing
- 3dsmax scanning function point connection drawing connection line
- Sixth note
猜你喜欢
Is $20billion a little less? Cisco is interested in Splunk?
Use assimp library to read MTL file data
Flutter tips: various fancy nesting of listview and pageview
2022 thinking of mathematical modeling C problem of American college students / analysis of 2022 American competition C problem
3dsmax scanning function point connection drawing connection line
stm32Cubemx(8):RTC和RTC唤醒中断
PostgreSQL 超越 MySQL,“世界上最好的编程语言”薪水偏低
Data is stored in the form of table
Understand encodefloatrgba and decodefloatrgba
669. 修剪二叉搜索树 ●●
随机推荐
C4D simple cloth (version above R21)
Unity synergy
2021 electrician cup idea + code - photovoltaic building integration plate index development trend analysis and prediction: prediction planning issues
質量體系建設之路的分分合合
Unity sends messages and blocks indecent words
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
Detailed explanation of the ranking of the best universities
中国金刚烷行业研究与投资预测报告(2022版)
3dsmax2018 common operations and some shortcut keys of editable polygons
AutoCAD - set layer
Unity ugui source code graphic
UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
AutoCAD - full screen display
Create a pyGame window with a blue background
Lua GBK and UTF8 turn to each other
An article takes you to thoroughly understand descriptors
中国针状焦行业发展研究与投资价值报告(2022版)
猿人学第一题
The difference between heap and stack
"Measuring curve length" of CAD dream drawing