当前位置:网站首页>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
边栏推荐
- Unity check whether the two objects have obstacles by ray
- Data is stored in the form of table
- xss注入
- The difference between heap and stack
- 2020-10-27
- Unity intelligent NPC production -- pre judgment walking (method 1)
- cocos_ Lua listview loads too much data
- Research on the value of background repeat of background tiling
- PostgreSQL 超越 MySQL,“世界上最好的编程语言”薪水偏低
- Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
猜你喜欢
stm32Cubemx(8):RTC和RTC唤醒中断
Understand encodefloatrgba and decodefloatrgba
Unity parallax infinite scrolling background
Generate filled text and pictures
On-off and on-off of quality system construction
Emlog blog theme template source code simple good-looking responsive
Pdf to DWG in CAD
Introduction to JVM principle and process
3dsmax scanning function point connection drawing connection line
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
随机推荐
Unity3d learning notes
"Measuring curve length" of CAD dream drawing
Listview is added and deleted at the index
Collapse of adjacent vertical outer margins
cocos_ Lua listview loads too much data
669. Prune binary search tree ●●
Cocos2dx screen adaptation
AutoCAD - scaling
AutoCAD - full screen display
2020-10-27
C # perspective following
Introduction to JVM principle and process
cocos_ Lua loads the file generated by bmfont fnt
3dsmax common commands
Unity parallax infinite scrolling background
AutoCAD - continuous annotation
mysql审计日志归档
2022/7/1学习总结
mysql審計日志歸檔
Generate filled text and pictures