当前位置:网站首页>嵌入式数据库开发编程(六)——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;
}
六、字节缓冲
效率最高
七、总结
细心就行,要封装好
边栏推荐
- Cocos create Jiugongge pictures
- 2022 American College Students' mathematical modeling ABCDEF problem thinking /2022 American match ABCDEF problem analysis
- Unity check whether the two objects have obstacles by ray
- AutoCAD - Center zoom
- 【Leetcode】1352. 最后 K 个数的乘积
- 2022 thinking of mathematical modeling D problem of American college students / analysis of 2022 American competition D problem
- Establish cloth effect in 10 seconds
- AutoCAD - graphic input and output
- Chinese notes of unit particle system particle effect
- Detailed explanation of the ranking of the best universities
猜你喜欢
django连接数据库报错,这是什么原因
Unity find the coordinates of a point on the circle
Séparation et combinaison de la construction du système qualité
【Leetcode】1352. Product of the last K numbers
How to choose a panoramic camera that suits you?
2021 huashubei mathematical modeling idea + reference + paper
[groovy] closure (closure as function parameter | code example)
Unity check whether the two objects have obstacles by ray
2022 thinking of mathematical modeling C problem of American college students / analysis of 2022 American competition C problem
Autocad-- dynamic zoom
随机推荐
The difference between heap and stack
【Leetcode】1352. Product of the last K numbers
Common technologies of unity
Recherche de mots pour leetcode (solution rétrospective)
Listview is added and deleted at the index
Listview pull-down loading function
AutoCAD - set layer
GameObject class and transform class of unity
Unity find the coordinates of a point on the circle
猿人学第一题
2021 higher education social cup mathematical modeling national tournament ABCD questions - problem solving ideas - Mathematical Modeling
Pause and resume of cocos2dx Lua scenario
Emlog博客主题模板源码简约好看响应式
2021 electrician Cup - high speed rail traction power supply system operation data analysis and equivalent modeling ideas + code
数论函数及其求和 待更新
3dsmax snaps to frozen objects
Sixth note
AutoCAD - lengthening
Séparation et combinaison de la construction du système qualité
AutoCAD - isometric annotation