数据库编程之SQLite(二)
liebian365 2024-11-19 06:29 20 浏览 0 评论
在 C程序中使用 SQLite 之前,我们需要确保机器上已经有 SQLite 库。可以查看 SQLite 安装章节了解安装过程。(在上篇文章中有介绍两种安装方式)。
接下来介绍几个C语言的 接口 API。
一、打开/关闭数据库的API
案例分析:(打开/关闭数据库API使用)
#include <stdio.h>
#include <sqlite3.h>
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if(rc != SQLITE_OK)
{
perror("sqlite3_open");
}
else
{
printf("创建数据库成功\n");
}
sqlite3_close(db);
return 0;
}
现在,让我们来编译和运行上面的程序,在当前目录中创建我们的数据库test.db。您可以根据需要改变路径。
$gcc test.c -lpthread -ldl
$./a.out
创建数据库成功
$ls
test.db
二、执行sql语句的API
1、回调法
2、非回调法
案例分析:创建表、插入数据、查询数据等
#include<stdio.h>
#include"sqlite3.h"
int main(int argc, char const *argv[])
{
sqlite3 *db = NULL;
int ret = sqlite3_open("test.db", &db);
if(ret != SQLITE_OK)
{
perror("sqlite3_open");
}
printf("ok\n");
//1、创建一个person表
char *errmsg = NULL;
char *cmd = "create table person(id int, name text, addr text);";
sqlite3_exec(db,cmd,NULL,NULL,&errmsg);
//2、往test.db中插入数据
cmd = "insert into person values(100, \'lucy\', \'sz\');";
sqlite3_exec(db,cmd,NULL,NULL,&errmsg);
cmd = "insert into person values(300, \'bob\', \'bj\');";
sqlite3_exec(db,cmd,NULL,NULL,&errmsg);
cmd = "insert into person values(200, \'tom\', \'sz\');";
sqlite3_exec(db,cmd,NULL,NULL,&errmsg);
//3、查询数据库的数据
cmd = "select * from person where name=\'tom\';";
//调用非回调函数
char **result=NULL;
int nRow = 0;//存放行数
int nCol = 0;//存放列数
sqlite3_get_table(db,cmd,&result, &nRow, &nCol, &errmsg);
printf("nRow = %d\n", nRow);
printf("nCol = %d\n", nCol);
for (size_t i = 0; i < nRow+1; i++)//0 1 2
{
for (size_t j = 0; j < nCol; j++)
{
printf("%s ", result[j+i*nCol] );
}
printf("\n");
}
//释放result指向的堆区空间
if(result != NULL)
{
sqlite3_free_table(result);
result = NULL;
}
sqlite3_close(db);
getchar();
return 0;
}
总结:
如果执行sql命令 不需要结果 sqlte3_exec函数
如果执行sql命令 需要结果 sqlite3_get_table函数
综合案例:【终端实现考勤】
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "rfid_lib.h"
#include "sqlite3.h"
#include <pthread.h>
//串口号
#if 0
#define UART_DEV "/dev/ttyUSB0"
#else
#define UART_DEV "/dev/ttySAC2"
#endif
int flag = 0;//0表示考勤 1表示注册
char rfid_tmp[32]="";//存放rfid
sqlite3 * sqlite3_init(char *db_name)
{
//打开数据库
sqlite3 *db = NULL;
int ret = sqlite3_open(db_name, &db);
if(ret != SQLITE_OK)
{
perror("sqlite3_open");
return NULL;
}
//创建一张考勤表KQ id time rfid
char *cmd = "create table KQ(id int, time text, rfid text);";
sqlite3_exec(db,cmd,NULL, NULL, NULL);
//创建一张员工表person id name rfid
cmd = "create table person(id int, name text, rfid text);";
sqlite3_exec(db,cmd,NULL, NULL, NULL);
//创建一张管理员表amdin usr psw
cmd = "create table admin(usr text, psw text);";
sqlite3_exec(db,cmd,NULL, NULL, NULL);
return db;
}
void* usr_input_fun(void *arg)
{
sqlite3 *db = (sqlite3 *)arg;
printf("注册:register\n");
printf("查询:search\n");
while(1)
{
char cmd[32]="";
fgets(cmd,sizeof(cmd),stdin);
cmd[strlen(cmd)-1] = 0;//去掉回车
if(strncmp(cmd,"register", strlen("register")) == 0)
{
//注册信息
int id=0;
char name[32]="";
char rfid[64]="";
printf("请输入需要注册的id name:");
scanf("%d %s", &id, name);
flag = 1;//注册
while(flag);//等待线程2获取rfid卡号
strcpy(rfid, rfid_tmp);
memset(rfid_tmp, 0, sizeof(rfid_tmp));
char sql[128]="";
sprintf(sql,"insert into person values(%d, \'%s\', \'%s\');", id, name, rfid);
sqlite3_exec(db,sql,NULL, NULL,NULL);
printf("成功注册");
}
else if(strncmp(cmd,"search", strlen("search")) == 0)
{
//查询
printf("请输入要查询的姓名:");
char name[32]="";
fgets(name, sizeof(name), stdin);
name[strlen(name)-1] = 0;
char sql[128]="";
sprintf(sql,"select name,time from KQ,person where person.id=KQ.id and person.name=\'%s\';",name);
char **result=NULL;
int nRow = 0;
int nCol = 0;
sqlite3_get_table(db,sql, &result, &nRow, &nCol, NULL);
int i=0,j=0;
for (i = 0; i < nRow+1; i++)
{
for (j = 0; j < nCol; j++)
{
printf("%s ", result[j+i*nCol]);
}
printf("\n");
}
}
}
}
int main(void)
{
int len,i;char type;
unsigned char id[18] = {0};
//捕获信号
//rfid初始化
uart_rfid_init(UART_DEV);
//数据库初始化
sqlite3 *db = sqlite3_init("rfid.db");
//创建一个用户输入线程(项目中用网页实现)
pthread_t tid;
pthread_create(&tid, NULL, usr_input_fun , db);
pthread_detach(tid);
while(1)
{
if(len = get_rfid_card_id(id,&type)){
printf("%c类卡卡号:",type);
for(i=0;i<len;i++)
printf("%02x ",id[i]);
puts("");
char rfid[128]="";
for (i = 0; i < len; i++)
{
sprintf(rfid+strlen(rfid),"%02x-", id[i]);
}
rfid[strlen(rfid)-1]=0;//去掉最后一个‘-’
if(flag == 1)//注册
{
flag =0;
//遍历数据库 如果不存在此卡 才允许注册
strcpy(rfid_tmp,rfid);
}
else if(flag == 0)//考勤
{
//在person通过rfid的id
char sql[128]="";
sprintf(sql,"select id from person where rfid=\'%s\';", rfid);
char **result=NULL;
int nRow = 0;
int nCol = 0;
sqlite3_get_table(db,sql, &result, &nRow, &nCol, NULL);
if(nRow == 0)
{
printf("无效的卡\n");
}
else
{
//id存放在resualt[1]中
memset(sql,0,sizeof(sql));
sprintf(sql,"insert into KQ values(%s, datetime(\'now\'), \'%s\');",\
result[1], rfid);
sqlite3_exec(db,sql,NULL,NULL,NULL);
printf("KQ ok\n");
}
}
}
}
return 0;
}
相关推荐
- 4万多吨豪华游轮遇险 竟是因为这个原因……
-
(观察者网讯)4.7万吨豪华游轮搁浅,竟是因为油量太低?据观察者网此前报道,挪威游轮“维京天空”号上周六(23日)在挪威近海发生引擎故障搁浅。船上载有1300多人,其中28人受伤住院。经过数天的调...
- “菜鸟黑客”必用兵器之“渗透测试篇二”
-
"菜鸟黑客"必用兵器之"渗透测试篇二"上篇文章主要针对伙伴们对"渗透测试"应该如何学习?"渗透测试"的基本流程?本篇文章继续上次的分享,接着介绍一下黑客们常用的渗透测试工具有哪些?以及用实验环境让大家...
- 科幻春晚丨《震动羽翼说“Hello”》两万年星间飞行,探测器对地球的最终告白
-
作者|藤井太洋译者|祝力新【编者按】2021年科幻春晚的最后一篇小说,来自大家喜爱的日本科幻作家藤井太洋。小说将视角放在一颗太空探测器上,延续了他一贯的浪漫风格。...
- 麦子陪你做作业(二):KEGG通路数据库的正确打开姿势
-
作者:麦子KEGG是通路数据库中最庞大的,涵盖基因组网络信息,主要注释基因的功能和调控关系。当我们选到了合适的候选分子,单变量研究也已做完,接着研究机制的时便可使用到它。你需要了解你的分子目前已有哪些...
- 知存科技王绍迪:突破存储墙瓶颈,详解存算一体架构优势
-
智东西(公众号:zhidxcom)编辑|韦世玮智东西6月5日消息,近日,在落幕不久的GTIC2021嵌入式AI创新峰会上,知存科技CEO王绍迪博士以《存算一体AI芯片:AIoT设备的算力新选择》...
- 每日新闻播报(September 14)_每日新闻播报英文
-
AnOscarstatuestandscoveredwithplasticduringpreparationsleadinguptothe87thAcademyAward...
- 香港新巴城巴开放实时到站数据 供科技界研发使用
-
中新网3月22日电据香港《明报》报道,香港特区政府致力推动智慧城市,鼓励公私营机构开放数据,以便科技界研发使用。香港运输署21日与新巴及城巴(两巴)公司签署谅解备忘录,两巴将于2019年第3季度,开...
- 5款不容错过的APP: Red Bull Alert,Flipagram,WifiMapper
-
本周有不少非常出色的app推出,鸵鸟电台做了一个小合集。亮相本周榜单的有WifiMapper's安卓版的app,其中包含了RedBull的一款新型闹钟,还有一款可爱的怪物主题益智游戏。一起来看看我...
- Qt动画效果展示_qt显示图片
-
今天在这篇博文中,主要实践Qt动画,做一个实例来讲解Qt动画使用,其界面如下图所示(由于没有录制为gif动画图片,所以请各位下载查看效果):该程序使用应用程序单窗口,主窗口继承于QMainWindow...
- 如何从0到1设计实现一门自己的脚本语言
-
作者:dong...
- 三年级语文上册 仿写句子 需要的直接下载打印吧
-
描写秋天的好句好段1.秋天来了,山野变成了美丽的图画。苹果露出红红的脸庞,梨树挂起金黄的灯笼,高粱举起了燃烧的火把。大雁在天空一会儿写“人”字,一会儿写“一”字。2.花园里,菊花争奇斗艳,红的似火,粉...
- C++|那些一看就很简洁、优雅、经典的小代码段
-
目录0等概率随机洗牌:1大小写转换2字符串复制...
- 二年级上册语文必考句子仿写,家长打印,孩子照着练
-
二年级上册语文必考句子仿写,家长打印,孩子照着练。具体如下:...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- wireshark怎么抓包 (75)
- qt sleep (64)
- cs1.6指令代码大全 (55)
- factory-method (60)
- sqlite3_bind_blob (52)
- hibernate update (63)
- c++ base64 (70)
- nc 命令 (52)
- wm_close (51)
- epollin (51)
- sqlca.sqlcode (57)
- lua ipairs (60)
- tv_usec (64)
- 命令行进入文件夹 (53)
- postgresql array (57)
- statfs函数 (57)
- .project文件 (54)
- lua require (56)
- for_each (67)
- c#工厂模式 (57)
- wxsqlite3 (66)
- dmesg -c (58)
- fopen参数 (53)
- tar -zxvf -c (55)
- 速递查询 (52)