当前位置:网站首页>嵌入式数据库开发编程(六)——C API
嵌入式数据库开发编程(六)——C API
2022-07-05 04:56:00 【光追雨】
一、打开、关闭和错误处理
错误处理
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("Please input db name\n");
exit(1);
}
sqlite3 *db;
int ret = sqlite3_open(argv[1], &db);
if(ret != SQLITE_OK)
{
printf("sqlite3 open:%s",sqlite3_errmsg(db));
exit(1);
}
printf("sqlite open db successful!\n");
sqlite3_close(db);
return 0;
}
二、执行sql
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
void print_error(int ret, char *err, sqlite3 *db)
{
if (ret != SQLITE_OK)
{
printf("%s:%s\n",err, sqlite3_errmsg(db));
exit(1);
}
}
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("Please input db name\n");
exit(1);
}
sqlite3 *db;
char *errmsg;
char sql[1024] = {
0};
int ret = sqlite3_open(argv[1], &db);
print_error(ret, "sqlite_open",db);
printf("sqlite open db successful!\n");
memset(sql, 0, sizeof(sql));
strcpy(sql, "create table IF NOT EXISTS student (integer primary key, name text, age integer)");
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
print_error(ret, "sqlite exec create table", db);
sqlite3_close(db);
return 0;
}
输入sqlitebrower test.db
这样就创建成功了,如果没有sqlitebrower
输入:sudo apt-get install sqlitebrower
三、封装sql
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
void print_error(int ret, char *err, sqlite3 *db)
{
if (ret != SQLITE_OK)
{
printf("%s:%s\n",err, sqlite3_errmsg(db));
exit(1);
}
}
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("Please input db name\n");
exit(1);
}
sqlite3 *db;
char *errmsg;
char sql[1024] = {
0};
int id;
char name[32];
int age;
int ret = sqlite3_open(argv[1], &db);
print_error(ret, "sqlite_open",db);
printf("sqlite open db successful!\n");
memset(sql, 0, sizeof(sql));
strcpy(sql, "create table IF NOT EXISTS student (id integer primary key, name text, age integer)");
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
print_error(ret, "sqlite exec create table", db);
//插入2行数据:id,name,age 键盘输入
for (size_t i = 0; i < 2; i++)
{
printf("Please input id:\n");
scanf("%d", &id);
printf("Please input name:\n");
scanf("%s", name);
printf("Please input age:\n");
scanf("%d", &age);
//sql:insert into student(id, name, age) values()
//sprintf(); fprintf
memset(sql, 0, sizeof(sql));
sprintf(sql, "insert into student(id, name, age) values(%d, '%s', %d)",id, name, age);
printf("%s\n",sql);
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
print_error(ret, "insert into", db);
}
//删除
memset(sql, 0, sizeof(sql));
printf("Please input delete name:\n");
scanf("%s", name);
sprintf(sql, "delete from student where name = '%s'", name);
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
print_error(ret, "delete", db);
sqlite3_close(db);
return 0;
}
四、回调函数
行缓冲
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
void print_error(int ret, char *err, sqlite3 *db)
{
if (ret != SQLITE_OK)
{
printf("%s:%s\n",err, sqlite3_errmsg(db));
exit(1);
}
}
//放在指针数组里面了
int my_sqlite_callback(void *para, int columnCount, char **columnValue, char **columnName)
{
int flag = *((int *)para);
printf("flag = %d\n",flag);
printf("columbCount = %d\n", columnCount);
for (size_t i = 0; i < columnCount; i++)
{
printf("%s:%s|",columnName[i] ,columnValue[i]);
}
printf("\n");
return 0;
}
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("Please input db name\n");
exit(1);
}
sqlite3 *db;
char *errmsg;
char sql[1024] = {
0};
int id;
char name[32];
int age;
int ret = sqlite3_open(argv[1], &db);
print_error(ret, "sqlite_open",db);
printf("sqlite open db successful!\n");
memset(sql, 0, sizeof(sql));
strcpy(sql, "create table IF NOT EXISTS student (id integer primary key, name text, age integer)");
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
print_error(ret, "sqlite exec create table", db);
//插入2行数据:id,name,age 键盘输入
#if 0
for (size_t i = 0; i < 1; i++)
{
printf("Please input id:\n");
scanf("%d", &id);
printf("Please input name:\n");
scanf("%s", name);
printf("Please input age:\n");
scanf("%d", &age);
//sql:insert into student(id, name, age) values()
//sprintf(); fprintf
memset(sql, 0, sizeof(sql));
sprintf(sql, "insert into student(id, name, age) values(%d, '%s', %d)",id, name, age);
printf("%s\n",sql);
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
print_error(ret, "insert into", db);
}
//删除
memset(sql, 0, sizeof(sql));
printf("Please input delete name:\n");
scanf("%s", name);
sprintf(sql, "delete from student where name = '%s'", name);
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
print_error(ret, "delete", db);
#endif
memset(sql, 0, sizeof(sql));
strcpy(sql, "select * from student");
int flag = 0;
ret = sqlite3_exec(db, sql, my_sqlite_callback, (void *)&flag, &errmsg);
//有多少行,就会执行所少次
print_error(ret, "select", db);
sqlite3_close(db);
//flag无法在函数my_sqlite_callback中使用
printf("flag = %d\n",flag);
//不是select的时候,callback不作用
return 0;
}
五、全缓冲查询
全局缓冲
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
void print_error(int ret, char *err, sqlite3 *db)
{
if (ret != SQLITE_OK)
{
printf("%s:%s\n",err, sqlite3_errmsg(db));
exit(1);
}
}
//放在指针数组里面了
int my_sqlite_callback(void *para, int columnCount, char **columnValue, char **columnName)
{
int flag = *((int *)para);
printf("flag = %d\n",flag);
printf("columbCount = %d\n", columnCount);
for (size_t i = 0; i < columnCount; i++)
{
printf("%s:%s|",columnName[i] ,columnValue[i]);
}
printf("\n");
return 0;
}
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("Please input db name\n");
exit(1);
}
sqlite3 *db;
char *errmsg;
char sql[1024] = {
0};
int id;
char name[32];
int age;
int ret = sqlite3_open(argv[1], &db);
print_error(ret, "sqlite_open",db);
printf("sqlite open db successful!\n");
memset(sql, 0, sizeof(sql));
strcpy(sql, "create table IF NOT EXISTS student (id integer primary key, name text, age integer)");
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
print_error(ret, "sqlite exec create table", db);
//插入2行数据:id,name,age 键盘输入
#if 0
for (size_t i = 0; i < 1; i++)
{
printf("Please input id:\n");
scanf("%d", &id);
printf("Please input name:\n");
scanf("%s", name);
printf("Please input age:\n");
scanf("%d", &age);
//sql:insert into student(id, name, age) values()
//sprintf(); fprintf
memset(sql, 0, sizeof(sql));
sprintf(sql, "insert into student(id, name, age) values(%d, '%s', %d)",id, name, age);
printf("%s\n",sql);
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
print_error(ret, "insert into", db);
}
//删除
memset(sql, 0, sizeof(sql));
printf("Please input delete name:\n");
scanf("%s", name);
sprintf(sql, "delete from student where name = '%s'", name);
ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
print_error(ret, "delete", db);
#endif
#if 0
memset(sql, 0, sizeof(sql));
strcpy(sql, "select * from student");
int flag = 0;
ret = sqlite3_exec(db, sql, my_sqlite_callback, (void *)&flag, &errmsg);
//有多少行,就会执行所少次
print_error(ret, "select", db);
//flag无法在函数my_sqlite_callback中使用
printf("flag = %d\n",flag);
//不是select的时候,callback不作用
#endif
char **result;
int nrow;
int ncolumn;
memset(sql, 0, sizeof(sql));
strcpy(sql, "select * from student");
ret = sqlite3_get_table(db, sql, &result, &nrow, &ncolumn, &errmsg);
//从1开始遍历,要是从0的话,列名也会打印出来
for (size_t i = 1; i <= nrow; i++)
{
for (size_t j = 0; j < ncolumn; j++)
{
printf("%s|", result[i * ncolumn + j]);
}
printf("\n");
}
sqlite3_close(db);
return 0;
}
六、字节缓冲
效率最高
七、总结
细心就行,要封装好
边栏推荐
猜你喜欢
2022 thinking of mathematical modeling C problem of American college students / analysis of 2022 American competition C problem
AutoCAD - continuous annotation
AutoCAD - scaling
Unity check whether the two objects have obstacles by ray
Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]
2022 thinking of mathematical modeling a problem of American college students / analysis of 2022 American competition a problem
669. 修剪二叉搜索树 ●●
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
【acwing】240. food chain
Recherche de mots pour leetcode (solution rétrospective)
随机推荐
stm32Cubemx(8):RTC和RTC唤醒中断
数论函数及其求和 待更新
中国聚氨酯硬泡市场调研与投资预测报告(2022版)
2021 Higher Education Club Cup mathematical modeling national tournament ABCD problem - problem solving ideas
3dsmax2018 common operations and some shortcut keys of editable polygons
中国针状焦行业发展研究与投资价值报告(2022版)
XSS injection
MD5 bypass
Chinese notes of unit particle system particle effect
Create a pyGame window with a blue background
Special information | finance, accounting, audit - 22.1.23
Unity3d learning notes
2022 American College Students' mathematical modeling ABCDEF problem thinking /2022 American match ABCDEF problem analysis
Unity and database
用 Jmeter 工具做个小型压力测试
Unity connects to the database
[groovy] closure closure (customize closure parameters | customize a single closure parameter | customize multiple closure parameters | specify the default value of closure parameters)
Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
Number theoretic function and its summation to be updated
PR first time