文章目錄
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