文章目录
1、安装数据库
我们从SQLi官网下载页面,https://www.sqlite.org/download.html从源代码区下载 sqlite-autoconf-*.tar.gz。
然后在Linux下安装sqlite3
makun@ubuntu-14:~$ wget https://www.sqlite.org/2020/sqlite-autoconf-3310100.tar.gz
makun@ubuntu-14:~$ tar -xzvf sqlite-autoconf-3310100.tar.gz
makun@ubuntu-14:~$ cd sqlite-autoconf-3310100
makun@ubuntu-14:~/sqlite-autoconf-3310100$ ./configure
makun@ubuntu-14:~/sqlite-autoconf-3310100$ make
makun@ubuntu-14:~/sqlite-autoconf-3310100$ sudo make install
按照以上步骤我们就完成了sqlite的安装
2、sqlite的相关函数
2.1 sqlite3_open()
sqliet3_open()
函数原型:
#include <sqlite3.h>
int sqlite3_open(const char *dbname,sqlite3 **db)
函数说明:用来打开一个数据库
参数说明:第一个参数dbname是数据库的名称;
第二个参数db是用于保存打开的数据库文件dbname的信息。
2.2 sqlite_close()
函数原型:
#include <sqlite3.h>
int sqlite3_close(sqlite3 *db)
函数说明:用来关闭一个数据库。
参数说明:db需要关闭的数据库文件。
2.3 sqlite3_exec()
函数原型
#include <sqlite3.h>
int sqlite_exec(sqlite *db, const char *sql, int (*callback)(void *int,char **,char **),void *,char **errmsg);
函数说明:用来执行sqlite3语句
参数说明:第一个参数:db是用于保存打开的数据库文件dbname的信息;
第二个参数:sql你要执行命令的语句;
第三个参数:callback回调函数,当这条语句执行之后,sqlite3会去调用你提供的这个函数。通常设为NULL;
第四个参数:void *是你所提供的指针,你可以传递任何一个指针参数到这里,这个参数最终会传到回调函数里面,如果不需要传递指针给回调函数,可以填NULL。
第五个参数:是错误信息。
说明:说明:通常,sqlite3_callback和它后面的void*这两个位置都可以填NULL。填NULL表示你不需要回调。比如你做insert 操作,做delete操作,就没有必要使用回调。而当你做select 时,就要使用回调,因为sqlite3 把数据查出来,得通过回调告诉你查出了什么数据。虽然回调显得代码整齐,但有时候你还是想要非回调的select查询。这可以通过sqlite3_get_table 函数做到。
2.4 sqlite3_get_table()
函数原型
#include <sqlite3.h>
int sqlite3_get_table(sqlite3 *db, const char *zsql,char ***pazResult, int *nrow, int *ncolumn,char **zErrmsg);
函数说明:执行sql的查询功能
参数说明:第一个参数db:db是用于保存打开的数据库文件dbname的信息;
第二个参数:sqlite3的语句,跟sqlite3_exec里的sql是一样的,是一个很普通的以\0结尾的char *字符串。
第三个参数:查询的结果,它依然是一维数组,他的内存布局时:字段名称,后面时紧接着是每个字段值。
第四个参数:是查询出多少条记录(即查出是多少行,不包括字段名那行)
第五个参数:是多少个字段(多少列)
第六个参数:是错误信息
注意:如果提供了errmsg,用来创建错误消息的内存是在堆上分布的,故在调用后,应该检查一下是否为null值,如果有错误发生,使用sqlite3_free()释放errmsg占用的内存。
3、代码示例
3.1 创建一个数据库
示例代码:
/****************************************************************************
*****
* Copyright: (C) 2020 makun<[email protected]>
* All rights reserved.
*
* Filename: sqlite_create.c
* Description: This file
*
* Version: 1.0.0(2020年03月27日)
* Author: makun <[email protected]>
* ChangeLog: 1, Release initial version on "2020年03月27日
00时47分05秒"
*
****************************************************************************
****/
#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
sqlite3 *db=NULL;
int rc = -1;
//打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
rc = sqlite3_open("mksqlite.db",&db);
if(rc < 0)
{
printf("create mksqlite failure:%s\n",sqlite3_errmsg(db));
return -1;
}
else
{
printf("create mksqlite successfuly\n");
sqlite3_close(db);//关闭数据库
return 0;
}
运行结果:
需要注意的时在编译时需要在末尾加上一个 -lsqlite3,不然会抛错。
当我们再次查看当前目录时,可以发现多一个我们刚命名的数据库mksqlite3
3.2插入数据
示例代码:
/************************************************************************
*********
* Copyright: (C) 2020 makun<[email protected]>
* All rights reserved.
*
* Filename: sqlite_insert.c
* Description: This file
*
* Version: 1.0.0(2020年03月27日)
* Author: makun <[email protected]>
* ChangeLog: 1, Release initial version on "2020年03月27日
00时47分05秒"
*
************************************************************************
********/
#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
sqlite3 *db=NULL;
int rc = -1;
char *zerrmsg=0;
char *sql;
//打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
rc = sqlite3_open("mksqlite.db",&db);
if(rc < 0)
{
printf("create mksqlite failure:%s\n",sqlite3_errmsg(db));
return -1;
}
else
{
printf("create mksqlite successfuly\n");
}
sql = "CREATE TABLE sendata(\
ID number,\
name,\
age\
);";
sqlite3_exec(db, sql, 0, 0,&zerrmsg);
//以下为添加数据
sql = "INSERT INTO sendata VALUES('1','makun','20');";
sqlite3_exec(db, sql, 0, 0,&zerrmsg);
sql = "INSERT INTO sendata VALUES('2','zhangsan','18');";
sqlite3_exec(db, sql, 0, 0,&zerrmsg);
printf("Insert successfuly\n");
sqlite3_close(db);//关闭数据库
return 0;
}
运行结果:
我们创建了一个sendate的一个表
运行之后我们可以通过命令:sqlite3 mksqlite.db 进入我们创建的数据库中 ,在seqlit->中输入selit *from sendata之后就会出现我们刚才往表里插入的内容
3.3查看表的内容
代码如下:
/*********************************************************************************
* Copyright: (C) 2020 makun<[email protected]>
* All rights reserved.
*
* Filename: sqlite_sqlite_cattable.c
* Description: This file
*
* Version: 1.0.0(2020年03月27日)
* Author: makun <[email protected]>
* ChangeLog: 1, Release initial version on "2020年03月27日 00时47分05秒"
*
********************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
sqlite3 *db=NULL;
int rc ;
char *zerrmsg=0;
int nrow=0,ncolum=0;
char **azResult=NULL;
char *sql;
//打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
rc = sqlite3_open("mksqlite.db",&db);
if(rc)
{
printf("create mksqlite failure:%s\n",sqlite3_errmsg(db));
exit(1);
}
printf("create mksqlite successfuly\n");
sql ="SELECT *FROM sendata";
rc=sqlite3_get_table(db, sql, &azResult, &nrow, &ncolum,&zerrmsg);
if(rc ==0 )
{
printf("row:%d column=%d \n", nrow , ncolum);
printf("the result of querying is:\n");
int i=0,j=0;
for (i=0; i< (nrow+1) *ncolum; i++)
{
printf("%s", azResult[i]);
printf("\n");
}
}
else if(rc)
{
printf("SQL error!:%s\n",zerrmsg);//打印错误信息
sqlite3_free(zerrmsg);//释放掉azResult的内存空间
}
sqlite3_close(db);//关闭数据库
return 0;
}
代码运行结果
3.4删除数据
代码如下
/*********************************************************************************
* Copyright: (C) 2020 makun<[email protected]>
* All rights reserved.
*
* Filename: sqlite_delete.c
* Description: This file
*
* Version: 1.0.0(2020年03月27日)
* Author: makun <[email protected]>
* ChangeLog: 1, Release initial version on "2020年03月27日 00时47分05秒"
*
********************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
sqlite3 *db=NULL;
int rc ;
char *zerrmsg=0;
char *sql;
char *data;
//打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
rc = sqlite3_open("mksqlite.db",&db);
if(rc)
{
printf("create mksqlite failure:%s\n",sqlite3_errmsg(db));
exit(1);
}
printf("create mksqlite successfuly\n");
sql ="DELETE FROM sendata";
rc=sqlite3_exec(db,sql,NULL,(void *)data,&zerrmsg);
if(rc)
{
printf("SQL error!:%s\n",zerrmsg);//打印错误信息
sqlite3_free(zerrmsg);//释放掉zerrmsg的内存空间
}
else
{
printf("delete recodes successfuly\n");
}
sqlite3_close(db);//关闭数据库
return 0;
}
运行结果
我们打开sqlite3创建的表mksqlite表中
我们插入的数据已经不见了。关于linux下c语言编程操作数据库sqlite3到这就结束了,想要了解更到可以参考下面的链接https://www.runoob.com/sqlite/sqlite-create-table.html