How to use sqlite3 in C language
Link: https://www.cnblogs.com/heyongshen/p/16824711.html
There are many sqlite3 programming interfaces. For beginners, we only need to master a few commonly used functions for now, and we will naturally know how to use other functions.
database
This article assumes that the database is my.db and there is a data table student.
no | name | score |
---|---|---|
4 | A bite of Linux | 89.0 |
The statement to create the table is as follows:
CREATE TABLE IF NOT EXISTS student (no integer primary key, name text, score real);
Common functions
sqlite3_open
int sqlite3_open(char *path, sqlite3 **db);
功能:
打开sqlite数据库
参数:
path: 数据库文件路径
db: 指向sqlite句柄的指针,后面对数据库所有的操作都要依赖这个句柄
返回值:
成功返回0,失败返回错误码(非零值)
sqlite3_close
int sqlite3_close(sqlite3 *db);
功能:
关闭sqlite数据库
返回值:
成功返回0,失败返回错误码
const char *sqlite3_errmsg(sqlite3 *db);
功能:
打印错误信息
返回值:
返回错误信息
Execute SQL statements without callback functions
sqlite3_get_table
int sqlite3_get_table(sqlite3 *db, const char *sql, char ***resultp, int*nrow, int *ncolumn, char **errmsg);
功能:
执行SQL操作
参数:
db:数据库句柄
sql:SQL语句
resultp:用来指向sql执行结果的指针
nrow:满足条件的记录的数目
ncolumn:每条记录包含的字段数目
errmsg:错误信息指针的地址
返回值:
成功返回0,失败返回错误码
Example
For example, if we want to display all the data information in the student table, we can use sqlite3_get_table() to execute the statement:
select * from student
The implementation code is as follows:
void do_show_sample(sqlite3 *db)
{
char **result, *errmsg;
int nrow, ncolumn, i, j, index;
if (sqlite3_get_table(db, "select * from student", &result, &nrow, &ncolumn, &errmsg) != 0)
{
printf("error : %s\n", errmsg);
sqlite3_free(errmsg);
}
index = ncolumn;
for (i=0; i<nrow; i++)
{
for (j=0; j<ncolumn; j++)
{
printf("%-8s : %-8s\n", result[j], result[index]);
index++;
}
printf("************************\n");
}
sqlite3_free_table(result);
return;
}
Assume that the current table data information is as follows:
no | name | score |
---|---|---|
4 | A bite of Linux | 77.0 |
5 | A mouthful of peng | 88.0 |
6 | A | 99.0 |
7 | Yikou.com | 66.0 |
As for the specific meaning of these parameters appearing in this function, we can see the following figure:
There are many sqlite3 programming interfaces. For beginners, we only need to master a few commonly used functions for now, and we will naturally know how to use other functions.
database
This article assumes that the database is my.db and there is a data table student.
no | name | score |
---|---|---|
4 | A bite of Linux | 89.0 |
The statement to create the table is as follows:
CREATE TABLE IF NOT EXISTS student (no integer primary key, name text, score real);
Common functions
sqlite3_open
int sqlite3_open(char *path, sqlite3 **db);
功能:
打开sqlite数据库
参数:
path: 数据库文件路径
db: 指向sqlite句柄的指针
返回值:
成功返回0,失败返回错误码(非零值)
sqlite3_close
int sqlite3_close(sqlite3 *db);
功能:
关闭sqlite数据库
返回值:
成功返回0,失败返回错误码
const char *sqlite3_errmsg(sqlite3 *db);
功能:
打印错误信息
返回值:
返回错误信息
Execute SQL statements without callback functions
sqlite3_get_table
int sqlite3_get_table(sqlite3 *db, const char *sql, char ***resultp, int*nrow, int *ncolumn, char **errmsg);
功能:
执行SQL操作
参数:
db:数据库句柄
sql:SQL语句
resultp:用来指向sql执行结果的指针
nrow:满足条件的记录的数目
ncolumn:每条记录包含的字段数目
errmsg:错误信息指针的地址
返回值:
成功返回0,失败返回错误码
Example
For example, if we want to display all the data information in the student table, we can use sqlite3_get_table() to execute the statement:
select * from student
The implementation code is as follows:
void do_show_sample(sqlite3 *db)
{
char **result, *errmsg;
int nrow, ncolumn, i, j, index;
if (sqlite3_get_table(db, "select * from student", &result, &nrow, &ncolumn, &errmsg) != 0)
{
printf("error : %s\n", errmsg);
sqlite3_free(errmsg);
}
index = ncolumn;
for (i=0; i<nrow; i++)
{
for (j=0; j<ncolumn; j++)
{
printf("%-8s : %-8s\n", result[j], result[index]);
index++;
}
printf("************************\n");
}
sqlite3_free_table(result);
return;
}
Assume that the current table data information is as follows:
no | name | score |
---|---|---|
4 | A bite of Linux | 77.0 |
5 | A mouthful of peng | 88.0 |
6 | A | 99.0 |
7 | Yikou.com | 66.0 |
As for the specific meaning of these parameters appearing in this function, we can see the following figure:
As can be seen from the figure above: In the code:
ncolumn = 3
nrow = 5
result 指向所有的结果组成的字符串数组,
各个具体字符串的下标,图上已经标明。
By understanding the code in conjunction with this diagram, it is easy to understand the implementation principle of the code.
Execute SQL statements using callback functions
sqlite3_exec
typedef int (*sqlite3_callback)(void *, int, char **, char **);
int sqlite3_exec(sqlite3 *db, const char *sql, sqlite3_callback callback, void *, char **errmsg);
功能:
执行SQL操作
参数:
db:数据库句柄
sql:SQL语句,就是我们前面两章用于操作表的增删改查语句
callback:回调函数
errmsg:错误信息指针的地址
返回值:
成功返回0,失败返回错误码
Callback Function
typedef int (*sqlite3_callback)(void *para, int f_num, char **f_value, char **f_name);
功能:
每找到一条记录自动执行一次回调函数
参数:
para:传递给回调函数的参数
f_num:记录中包含的字段数目
f_value:包含每个字段值的指针数组
f_name:包含每个字段名称的指针数组
返回值:
成功返回0,失败返回-1
Example
sqlite3 *db;
char *errmsg,**resultp;
int callback(void *para, int f_num, char **f_val, char **f_name)
{
int i;
for (i=0; i<f_num; i++)
{
printf("%-8s", f_val[i]);
}
printf("\n");
return 0;
}
void do_show(sqlite3 *db)
{
char *errmsg;
printf("no name score\n");
if (sqlite3_exec(db, "select * from student", callback, NULL, &errmsg) != 0)
{
printf("error : %s\n", sqlite3_errmsg(db));
}
printf("\n");
return;
}
The callback function method implementation code needs to implement a callback function: callback. The function sqlite3_exec() will call the callback function once when parsing the command "select * from student" and not getting a row of data. Refer to the table student above.
callback()总共会被调用5次,
f_num 对应结果的列数,为3
f_value 则指向 每一列对应的值组成的字符串数组
Assume that the callback is called for the fourth time, as shown below:
Operation Results
Compilation requires the use of a third-party library, lsqlite3.
gcc student.c -o run -lsqlite3
Other functions
sqlite3 *pdb, 数据库句柄,跟文件句柄FILE很类似
sqlite3_stmt *stmt, 这个相当于ODBC的Command对象,用于保存编译好的SQL语句
sqlite3_exec(), 执行非查询的sql语句
sqlite3_prepare(), 准备sql语句,执行select语句或者要使用parameter bind时,用这个函数(封装了sqlite3_exec)
Sqlite3_step(), 在调用sqlite3_prepare后,使用这个函数在记录集中移动
There are also a series of functions for getting data from record set fields, such as
sqlite3_column_text(), 取text类型的数据
sqlite3_column_blob(),取blob类型的数据
sqlite3_column_int(), 取int类型的数据
According to international practice, here is the complete code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sqlite3.h>
void do_insert(sqlite3 *db)
{
int no;
char name[16];
float score;
char sqlstr[128], *errmsg;
printf("input no : ");
scanf("%d", &no);
printf("input name : ");
scanf("%s", name);
printf("input score : ");
scanf("%f", &score);
sprintf(sqlstr, "insert into student values (%d, '%s', %.1f)",
no, name, score);
#if __DEBUG
printf("cmd:%s\n",sqlstr);
#endif
if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != 0)
{
printf("error : %s\n", sqlite3_errmsg(db));
}
else
{
printf("insert is done\n");
}
printf("\n");
return;
}
void do_delete(sqlite3 *db)
{
char *errmsg;
char sqlstr[128], expression[64];
printf("input expression : ");
scanf("%s", expression);//name='ma'
sprintf(sqlstr, "delete from student where %s", expression);
#if __DEBUG
printf("cmd:%s\n",sqlstr);
#endif
if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != 0)
{
printf("error : %s\n", sqlite3_errmsg(db));
}
else
{
printf("deletet is done\n");
}
printf("\n");
return;
}
int callback(void *para, int f_num, char **f_val, char **f_name)
{
int i;
for (i=0; i<f_num; i++)
{
printf("%-8s", f_val[i]);
}
printf("\n");
return 0;
}
void do_show(sqlite3 *db)
{
char *errmsg;
printf("no name score\n");
if (sqlite3_exec(db, "select * from student", callback, NULL, &errmsg) != 0)
{
printf("error : %s\n", sqlite3_errmsg(db));
}
printf("\n");
return;
}
void do_show_sample(sqlite3 *db)
{
char **result, *errmsg;
int nrow, ncolumn, i, j, index;
if (sqlite3_get_table(db, "select * from student", &result, &nrow, &ncolumn, &errmsg) != 0)
{
printf("error : %s\n", errmsg);
sqlite3_free(errmsg);
}
index = ncolumn;
for (i=0; i<nrow; i++)
{
for (j=0; j<ncolumn; j++)
{
printf("%-8s : %-8s\n", result[j], result[index]);
index++;
}
printf("************************\n");
}
sqlite3_free_table(result);
return;
}
int main()
{
sqlite3 *db;
int n;
char clean[64];
if (sqlite3_open("my.db", &db) < 0)
{
printf("fail to sqlite3_open : %s\n", sqlite3_errmsg(db));
return -1;
}
while ( 1 )
{
printf("*********************************************\n");
printf("1: insert record \n2: delete record \n3: show record \n4: quit\n");
printf("*********************************************\n");
printf("please select : ");
if (scanf("%d", &n) != 1)
{
fgets(clean, 64, stdin);
printf("\n");
continue;
}
switch ( n )
{
case 1 :
do_insert(db);
break;
case 2 :
do_delete(db);
break;
case 3 :
do_show_sample(db);
break;
case 4 :
sqlite3_close(db);
exit(0);
}
}
return 0;
}
Run the main page:
Insert record: Display record: Delete record:
Let's learn how to operate the database through C language programs.
Autumn
The recruitment has already begun. If you are not well prepared,
Autumn
It's hard to find a good job.
Here is a big employment gift package for everyone. You can prepare for the spring recruitment and find a good job!