当前位置:网站首页>Embedded database development programming (VI) -- C API
Embedded database development programming (VI) -- C API
2022-07-05 05:07:00 【Light chasing rain】
List of articles
One 、 open 、 Shutdown and error handling
Error handling
#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;
}
Two 、 perform 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;
}
Input sqlitebrower test.db
So it's a success , without sqlitebrower
Input :sudo apt-get install sqlitebrower
3、 ... and 、 encapsulation 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);
// Insert 2 Row data :id,name,age Keyboard entry
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);
}
// Delete
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;
}
Four 、 Callback function
The line buffer
#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);
}
}
// In the pointer array
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);
// Insert 2 Row data :id,name,age Keyboard entry
#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);
}
// Delete
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);
// How many lines , Will be executed less times
print_error(ret, "select", db);
sqlite3_close(db);
//flag Cannot be in function my_sqlite_callback Use in
printf("flag = %d\n",flag);
// No select When ,callback It doesn't work
return 0;
}
5、 ... and 、 Fully buffered query
Global buffering
#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);
}
}
// In the pointer array
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);
// Insert 2 Row data :id,name,age Keyboard entry
#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);
}
// Delete
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);
// How many lines , Will be executed less times
print_error(ret, "select", db);
//flag Cannot be in function my_sqlite_callback Use in
printf("flag = %d\n",flag);
// No select When ,callback It doesn't work
#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);
// from 1 To traverse the , If you start from 0 Words , The column names will also be printed
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;
}
6、 ... and 、 Byte buffer
Highest efficiency
7、 ... and 、 summary
Just be careful , It should be sealed
边栏推荐
- How to choose a panoramic camera that suits you?
- 54. Spiral matrix & 59 Spiral matrix II ●●
- cocos_ Lua loads the file generated by bmfont fnt
- 2022/7/1 learning summary
- Rip notes [rip message security authentication, increase of rip interface measurement]
- Unity sends messages and blocks indecent words
- Sqlserver stored procedures pass array parameters
- 【Leetcode】1352. 最后 K 个数的乘积
- Transport connection management of TCP
- Simple HelloWorld color change
猜你喜欢
Recherche de mots pour leetcode (solution rétrospective)
Rip notes [rip message security authentication, increase of rip interface measurement]
xss注入
十年不用一次的JVM调用
Panel panel of UI
Unity ugui source code graphic
Unity parallax infinite scrolling background
Do a small pressure test with JMeter tool
669. 修剪二叉搜索树 ●●
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
随机推荐
China as resin Market Research and investment forecast report (2022 Edition)
Listview pull-down loading function
The first topic of ape Anthropology
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
Lua determines whether the current time is the time of the day
Unity and database
cocos2dx_ Lua card flip
中国溶聚丁苯橡胶(SSBR)行业研究与预测报告(2022版)
AutoCAD - Zoom previous
用 Jmeter 工具做个小型压力测试
Lua GBK and UTF8 turn to each other
mysql審計日志歸檔
win下一键生成当日的时间戳文件
中国针状焦行业发展研究与投资价值报告(2022版)
UE fantasy engine, project structure
AutoCAD - Document Management
小程序直播+电商,想做新零售电商就用它吧!
【Leetcode】1352. 最后 K 个数的乘积
775 Div.1 B. integral array mathematics
Django reports an error when connecting to the database. What is the reason