云南网是什么网站,广州外贸型网站,工作服定制电话,学校网站首页设计数据库的概念
SQL#xff08;Structured Query Language#xff09;是一种专门用来与数据库进行交互的编程语言#xff0c;它允许用户查询、更新和管理关系型数据库中的数据。关系型数据库是基于表#xff08;Table#xff09;的数据库#xff0c;其中表由行#xff08…数据库的概念
SQLStructured Query Language是一种专门用来与数据库进行交互的编程语言它允许用户查询、更新和管理关系型数据库中的数据。关系型数据库是基于表Table的数据库其中表由行Row和列Column组成每一行代表一个记录Record每一列代表一个字段Field。
非关系型数据库MongoDB 1、分类 大型 中型 小型 ORACLE MYSQL/MSSQL SQLITE DBII powdb 关系型数据库 2、名词 DB 数据库 select update database DBMS 数据库管理系统 MIS 管理信息系统 OA 办公自动化 3、嵌入式数据库 sqlite3 www.sqlite.org www.kernal.org GNU 特点 1、开源 C语言开发 2、代码量少 1万行左右总大小10M以内 3、绿色软件无需安装 4、文件型数据库可以移动。 5、数据容量最大 2T
4、sqlite3的安装 LTS long term support 1、在线安装 sudo apt-get install sqlite3 sudo apt-get install libsqlite3-dev gcc test.c -lsqlite3 -lpthread 2、验证是否安装成功 sqlite3 --version sqlite3 --help
5、sqlite3的使用 0、启动sqlite3 sqlite3 xxx.db 用sqlite3 来打开一个名称为test.db的本地数据库。 出现如下提示符表明数据库管理系统启动。 sqlite 退出数据库 .q 命令 注意如果一直出现如下符号 ... 则写;结束。
sql命令
以下所有命令必须在 sqlite 后执行。 创建一个数据库 1、touch xxx.db 2、sqlite3 xxx.db 系统维护命令 .help 出现所有相关的系统维护命令都是以 .开头。 .database 列出当前库和系统中那个文件在关联 .tables 列出当期数据库中的所有表 .schema xxx 列出当前指定的xxx表结构 .dump user 导出数据库 重定向 sqlite3 test.db .dump 123.sql sqlite3 xxx.db test.sql 导入数据库 sql语句 标准SQL语句》通用语法在其他平台可以直接使用。struct query language; 注意所有的sql语句都以;结尾。 创建一个表ddl create table 表名(表字段1表字段2...); eg: create table user(id,name,age); char 注意以上表的表字段支持如下数据类型。int text real blob 默认是text类型。char ; create table 表名 表字段 类型表字段 类型。。。。); eg: create table user(id int ,name char,age int); 删除一个表 drop table 表名 egdrop table user; 数据库常规操作 增加 删除 修改 查询 向表中增加数据 insert into 表名 (字段名称 ) values (值名称); eg:insert into user (id,age) values (1,10); insert into user values(3,wang,11); insert into user (age) values ( 12); 查询表中的数据 select 列名 from 表名 条件 egselect * from user ; select id from user; select id,name from user where not age 30 where name like 三一 % _ 通配符 asc select *from user where age20 or age50 order by age desc limit 2 ; || 修改表中数据 update 表名 set 表字段 值 满足条件 eg update user set id 1 where name li; update user set id 1 where name li and passwd 123; update user set id 2 where name li or name zhao; 删除表中数据 delete from 表名 满足条件 egdelete from user ; ///删除表中所有数据 delete from user where id 1; ///删除id1 的数据 delete from user where id 1 and name zhang; delete from user where id 1 or id 2;
2022-1-1 and 2018-12-31
插入时间列 int int; unicode CREATE TABLE user1(id int,name char,age int,dt datetime);2022-07-01 19:00:00 insert into user1 values (2,张三,23,datetime(now,8 hours));
自动增长列 sqlite CREATE TABLE user3(id INTEGER PRIMARY KEY ASC,name char,age int,dt datetime); 主键 sqlite insert into user3 (NULL,李四,23,datetime(now)); void*0 asc where (group by having) order by [desc] limit; select * from user where id10 order by id limit 2;
维护命令
1、数据的导出 sqlite3 xxx.db .dump xxx.sql //将数据库名称为xxx的数据库整体导出到脚本中。 2、数据的导入 sqlite3 xxx.db xxx.sql
3、可视化工具安装 sudo apt-get install sqlitebrowser
sqlite3 数据库编程接口
1、需要的头文件 sqlite3.h
2、编译过程 -lsqlite3
3、编程框架 打开数据库 》读写数据库(增删改查) 》关闭数据库
sqlite3.h
3.1 打开数据库 sqlite3_open int sqlite3_open(char * path,sqlite3 ** db); 功能打开指定path路径文件名称的数据库并将 打开的地址指向db变量的句柄。 参数path 要打开的数据库路径名称 db 要打开的数据库地址指针 返回值成功 0 失败 -1
3.2 关闭数据库 sqlite3_close int sqlite3_close(sqlite3 *db); 功能关闭指定的数据库 参数要关闭的数据库地址 返回值成功 0 失败 -1
3.3 数据库操作 查询操作sqlite3_get_table(); select int sqlite3_get_table(sqlite3 *db,char *sql, char *** rest,int *nrow,int *ncol, char ** errmsg); 功能在db数据库上执行sql查询语句并将执行的 结果集返回到rest地址上同时返回查询的行和列。 参数db 要执行查询语句的数据库 sql 要执行的select查询语句 rest 查询的结果集是一个三级指针 nrow 查询的结果的行数 ncol 查询的结果的列数 errmsg 如果执行有错误则存储错误。 返回值成功 0 失败 非0 执行sql语句sqlite3_exec(); insert delete update int sqlite3_exec(sqlite3 *db,char *sql,callback fun, void * arg,char ** errmsg); 功能在db数据库上执行sql 非查询语句。 并将结果返回。 参数db 要执行sql的数据库 sql 要执行的非查询sql语句。 fun 如果该函数要执行查询语句则该回调函数 用来回收查询的结果。 arg 回调函数的参数如果没有回调函数则该参数为NULL errmsg 执行过程中的错误信息。 返回值执行成功 0 失败 非0 int fun(void *arg ,int f_num,char ** f_value, char ** f_name) 功能该函数用于sqlite3_exec执行select语句的 结果集返回数据。 参数arg 由sqlite3_exec传入的参数 f_num 执行该命令所返回测结果集的字段个数。 f_value 查询结果集中的字段的值。 f_name 查询结果集中的字段的名称。 返回值成功 0 失败 非0 注意该回调函数必须有返回值否则可能导致查询异常。
使用打印一张表
#include stdio.h
#include sqlite3.h
int show(void*arg,int col,char**result,char**title)
{static int flag 0;int i 0 ;if(0 flag){for(i 0 ;icol;i){printf(%s\t,title[i]);}printf(\n);flag 1;}for(i 0 ;icol;i){printf(%s\t,result[i]);}printf(\n);return 0;
}
int main()
{sqlite3* dbNULL;int ret sqlite3_open(/home/linux/20240812/sec4/aaa.db,db);if(SQLITE_OK!ret){fprintf(stderr,cant opendb,%s\n,sqlite3_errstr(ret));sqlite3_close(db);return 1;}char sql_cmd[128]select * from user;;char * errmsgNULL;ret sqlite3_exec(db,sql_cmd,show,NULL,errmsg);if(SQLITE_OK!ret){fprintf(stderr,exec error,%s\n,errmsg);sqlite3_free(errmsg);sqlite3_close(db);return 1;}sqlite3_close(db);printf(Hello World!\n);return 0;
}
提升插入速度
在SQLite3中提升数据插入速度的方法主要包括以下几个方面
1. 使用事务Transaction
事务可以将多个插入操作组合在一起从而减少每次插入操作的开销。在SQLite中每调用一次sqlite3_exec()函数就会隐式地开启一个事务。如果插入一条数据就调用一次该函数事务会被反复地开启和关闭这会增大IO量。因此在插入数据前显式开启事务并在插入完成后一起提交可以显著提高IO效率进而加快数据插入速度。
#include stdio.h
#include sqlite3.h
#include string.h
int main()
{char * errmsgNULL;sqlite3* dbNULL;int ret sqlite3_open(/home/linux/20240812/sec4/aaa.db,db);if(SQLITE_OK!ret){fprintf(stderr,cant opendb,%s\n,sqlite3_errstr(ret));sqlite3_close(db);return 1;}char sql_cmd[1024]{0};sprintf(sql_cmd,create table if not exists dict(x INTEGER PRIMARY KEY ASC,word char,mean text););ret sqlite3_exec(db,sql_cmd,NULL,NULL,errmsg);if(SQLITE_OK!ret){fprintf(stderr,exec error create table,%s\n,errmsg);sqlite3_free(errmsg);sqlite3_close(db);return 1;}bzero(sql_cmd,sizeof(sql_cmd));sprintf(sql_cmd,delete from dict);ret sqlite3_exec(db,sql_cmd,NULL,NULL,errmsg);if(SQLITE_OK!ret){fprintf(stderr,exec error create table,%s\n,errmsg);sqlite3_free(errmsg);sqlite3_close(db);return 1;}FILE* fp fopen(/home/linux/dict.txt,r);if(NULL fp){perror(fopen);return 1;}// BEGIN TRANSACTION;bzero(sql_cmd,sizeof(sql_cmd));sprintf(sql_cmd,BEGIN TRANSACTION;);ret sqlite3_exec(db,sql_cmd,NULL,NULL,errmsg);if(SQLITE_OK!ret){fprintf(stderr,exec error create table,%s\n,errmsg);sqlite3_free(errmsg);sqlite3_close(db);return 1;}while(1){//char sql_cmd[128]insert into user values(7,bbb,11);;char line[512]{0};if(NULL fgets(line,sizeof(line),fp)){break;}char* word strtok(line, );char *mean strtok(NULL,\r);bzero(sql_cmd,sizeof(sql_cmd));sprintf(sql_cmd,insert into dict values(NULL,\%s\,\%s\);,word,mean);ret sqlite3_exec(db,sql_cmd,NULL,NULL,errmsg);if(SQLITE_OK!ret){fprintf(stderr,exec error,%s\n,errmsg);sqlite3_free(errmsg);sqlite3_close(db);return 1;}}bzero(sql_cmd,sizeof(sql_cmd));sprintf(sql_cmd,COMMIT;);ret sqlite3_exec(db,sql_cmd,NULL,NULL,errmsg);if(SQLITE_OK!ret){fprintf(stderr,exec error create table,%s\n,errmsg);sqlite3_free(errmsg);sqlite3_close(db);return 1;}sqlite3_close(db);printf(Hello World!\n);return 0;
}
2. 关闭写同步Synchronous OFF
SQLite的synchronous模式控制数据写入物理存储的同步方式。该模式有三种可选状态full、normal、off。其中full写入速度最慢但保证数据安全性而off可以加速数据库操作但在系统崩溃或断电时可能会导致数据库损毁。如果应用场景允许少量数据丢失可以将synchronous设置为OFF以提升插入速度。
通过设置PRAGMA synchronous OFF;可以关闭数据库的同步写入功能这将允许SQLite在将数据传递给操作系统后不等待磁盘写入完成就继续执行从而提高插入速度。但请注意这可能会增加数据丢失的风险。
#include stdio.h
#include sqlite3.h
#include string.h int main()
{ char *errmsg NULL; sqlite3 *db NULL; int ret sqlite3_open(/home/linux/20240812/sec4/aaa.db, db); if (SQLITE_OK ! ret) { fprintf(stderr, cant open db, %s\n, sqlite3_errstr(ret)); return 1; } // 关闭同步 char *pragma_cmd PRAGMA synchronous OFF;; ret sqlite3_exec(db, pragma_cmd, NULL, NULL, errmsg); if (SQLITE_OK ! ret) { fprintf(stderr, exec error setting synchronous, %s\n, errmsg); sqlite3_free(errmsg); sqlite3_close(db); return 1; } // ... (其他数据库操作如创建表、插入数据等) sqlite3_close(db); printf(Hello World!\n); return 0;
}
文章转载自: http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn http://www.morning.qtyfb.cn.gov.cn.qtyfb.cn http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.tsdqr.cn.gov.cn.tsdqr.cn http://www.morning.cwskn.cn.gov.cn.cwskn.cn http://www.morning.qnypp.cn.gov.cn.qnypp.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.krjrb.cn.gov.cn.krjrb.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.dtfgr.cn.gov.cn.dtfgr.cn http://www.morning.kaakyy.com.gov.cn.kaakyy.com http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.bcnsl.cn.gov.cn.bcnsl.cn http://www.morning.fjscr.cn.gov.cn.fjscr.cn http://www.morning.bwjws.cn.gov.cn.bwjws.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.kfwrq.cn.gov.cn.kfwrq.cn http://www.morning.spfq.cn.gov.cn.spfq.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.tslfz.cn.gov.cn.tslfz.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.nyqnk.cn.gov.cn.nyqnk.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.rbkdg.cn.gov.cn.rbkdg.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.lmfmd.cn.gov.cn.lmfmd.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.qkrqt.cn.gov.cn.qkrqt.cn http://www.morning.lprfk.cn.gov.cn.lprfk.cn http://www.morning.pamdeer.com.gov.cn.pamdeer.com http://www.morning.wnhsw.cn.gov.cn.wnhsw.cn http://www.morning.trmpj.cn.gov.cn.trmpj.cn http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.pkrb.cn.gov.cn.pkrb.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.nmbbt.cn.gov.cn.nmbbt.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.jghty.cn.gov.cn.jghty.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.jfjfk.cn.gov.cn.jfjfk.cn http://www.morning.zlwg.cn.gov.cn.zlwg.cn http://www.morning.kpnpd.cn.gov.cn.kpnpd.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.zyffq.cn.gov.cn.zyffq.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.tzlfc.cn.gov.cn.tzlfc.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.jrsgs.cn.gov.cn.jrsgs.cn http://www.morning.wjlrw.cn.gov.cn.wjlrw.cn http://www.morning.mdwb.cn.gov.cn.mdwb.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.jzlkq.cn.gov.cn.jzlkq.cn