使用sqlite3與C介面開發資料庫程式

2021-09-07 16:33:46 字數 4403 閱讀 1747

最近我正在linux平台寫乙個軟體,需要用到乙個簡單的資料庫。mysql做資料庫固然很好,但其資料是存放在伺服器的。我想要的基本功能也就是使用c程式建立乙個資料庫本地檔案,然後可以對這個資料庫檔案執行基本的sql操作. 就像在windows平台基於vc6.0的dao資料庫程式設計一樣(建立乙個本地檔案.mdb).

從網上找到了乙個開源免費的資料庫開發工具--sqlite, 網上的關於sqlite的介紹有很多,詳細見官方**: 

.  我發現sqlite正是我需要的. 總結一下幾個特點:

1. 開放源**

2. 程式特別小,在windows下應用程式sqlite.exe僅僅200kb以內。

3. 支援大多數sql指令,速度極快

4. 直接建立乙個***.db, 就是乙個資料庫,不需要伺服器支援

5. 簡潔的c語言api介面

基於上面幾點,足可以看出sqlite的強大和優異之處。源**公開和程式特別小,甚至可以跨平台直接編譯"gcc -o sqlite3 *",將這融合到潛入式系統是多麼的美妙!

在ubuntu6.10平台安裝sqlite3及其開發包:

#sudo apt-get install sqlite3 libsqlite3-dev

鏈結這篇文章介紹了sqlite的使用方法:

mysqlite/4/378.html

下面是我總結的sqlite3與c介面的api

我用到的主要是下面幾個函式(標頭檔案sqlite3.h):

int sqlite3_open(const char*, sqlite3**); //開啟乙個資料庫

int sqlite3_close(sqlite3*); //關閉

int sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void*, char**);//執行

int sqlite3_get_table(sqlite3*, const char *sql,char***result, int *nrow,int *ncolumn ,char **errmsg );

//result中是以陣列的形式存放所查詢的資料,首先是表名,再是資料; 

//nrow/ncolumn分別為查詢語句返回的結果集的行數/列數,沒有查到結果時返回0

sqlite3_errcode() 通常用來獲取最近呼叫的api介面返回的錯誤**. 

sqlite3_errmsg() 則用來得到這些錯誤**所對應的文字說明.

exec錯誤碼

#define sqlite_ok           0   /* successful result */

#define sqlite_error        1   /* sql error or missing database */

#define sqlite_internal     2   /* an internal logic error in sqlite */

#define sqlite_perm         3   /* access permission denied */

#define sqlite_abort        4   /* callback routine requested an abort */

#define sqlite_busy         5   /* the database file is locked */

#define sqlite_locked       6   /* a table in the database is locked */

#define sqlite_nomem        7   /* a malloc() failed */

#define sqlite_readonly     8   /* attempt to write a readonly database */

#define sqlite_interrupt    9   /* operation terminated by sqlite_interrupt() */

#define sqlite_ioerr       10   /* some kind of disk i/o error occurred */

#define sqlite_corrupt     11   /* the database disk image is malformed */

#define sqlite_notfound    12   /* (internal only) table or record not found */

#define sqlite_full        13   /* insertion failed because database is full */

#define sqlite_cantopen    14   /* unable to open the database file */

#define sqlite_protocol    15   /* database lock protocol error */

#define sqlite_empty       16   /* (internal only) database table is empty */

#define sqlite_schema      17   /* the database schema changed */

#define sqlite_toobig      18   /* too much data for one row of a table */

#define sqlite_constraint  19   /* abort due to contraint violation */

#define sqlite_mismatch    20   /* data type mismatch */

#define sqlite_misuse      21   /* library used incorrectly */

#define sqlite_nolfs       22   /* uses os features not supported on host */

#define sqlite_auth        23   /* authorization denied */

#define sqlite_row         100  /* sqlite_step() has another row ready */

#define sqlite_done        101  /* sqlite_step() has finished executing */

應用例項:

定義sqlite3 *db = null;

char * errmsg = null;

char sql_cmd[200];

開啟檔案:

int rc = sqlite3_open("***.db", &db);

if(rc) //開啟失敗

printf("open database failed!\n");

else 

printf("create the database successful!\n");

建立**:

sprintf(sql_cmd,"create table datapro(package integer,offset \

integer,lklen integer,base inteher,link integer,err integer);");

rc=sqlite3_exec(db,sql_cmd,0,0,&emsg); //建立表datapro

if(rc==sqlite_ok) //建表成功

printf("create the chn_to_eng table successful!\n");

else

printf("%s\n",emsg);

新增資料:

sprintf(sql_cmd,"insert into datapro values(%d,%d,%d,%d,%d,%d);",4,2345,268,9,3,3);

rc=sqlite3_exec(pro_db,pro_sqlcmd,0,0,&emsg);

查詢:int nrow=0, ncolumn=0;

char **azresult; //存放結果

sql="select * from datapro";

sqlite3_get_table(db,sql,&azresult,&nrow,&ncolumn,&emsg); 

//其中nrow為行數,ncolum為列數

printf("\nthe result of querying is : \n");

for(int i=1;i

刪除:sprintf(sql_cmd,"delete from datapro where package=1;") ;

rc=sqlite3_exec(db,sql,0,0,&emsg);

使用sqlite3 模組操作sqlite3資料庫

python內建了sqlite3模組,可以操作流行的嵌入式資料庫sqlite3。如果看了我前面的使用 pymysql 操作mysql資料庫這篇文章就更簡單了。因為它們都遵循pep 249,所以操作方法幾乎相同。廢話就不多說了,直接看 吧。都差不多,首先匯入模組,然後建立連線,然後獲取游標物件,之後利...

SQLite3 使用教學

os x自從10.4後把sqlite這套相當出名的資料庫軟體,放進了作業系統工具集裡。os x包裝的是第三版的sqlite,又稱sqlite3。這套軟體有幾個特色 支援大多數的sql指令 下面會簡單介紹 乙個檔案就是乙個資料庫。不需要安裝資料庫伺服器軟體。完整的unicode支援 因此沒有跨語系的問...

sqlite3使用簡介

一 使用流程 使用的過程根據使用的函式大致分為如下幾個過程 這幾個過程是概念上的說法,而不完全是程式執行的過程,如sqlite3 column 表示的是對查詢獲得一行裡面的資料的列的各個操作統稱,實際上在sqlite中並不存在這個函式。1 sqlite3 open 開啟資料庫 在運算元據庫之前,首先...