SQlite3的了解
SQLite3是一個軟體庫,實作了自給自足的、無伺服器的、零配件的、事務性的SQL資料引擎;在具有和Oracle、MySQL這種大型的資料庫幾乎相同的功能下隻有2W+行的代碼,并且是采用C語言編寫的;它運作所占用的記憶體比較少,是以比較适合于嵌入式系統當中。
同時SQLite3還支援Shell運作,它為C/C++提供了相對簡單的API接口函數,這使得開發中會相對簡單便捷;
移植SQLite3到運作Linux系統的ARM開發闆中
- 先到官網下載下傳SQLite3的源碼,下載下傳位址:https://www.sqlite.org/download.html(記得是第二個)
- 解壓、進入到SQLite3目錄當中,建立一個install目錄(存放生成的可執行檔案以及連結庫等等),修改configure腳本檔案
./configure --host=arm-linux --prefix=$PWD/instasll CC=arm-linux-gcc//使用此指令重新修改configure腳本檔案
- 執行make、make install既可以在剛剛建立的目錄中找到相關的可執行檔案、連結庫等檔案
1、bin下面是可執行檔案sqlite3
2、include下面是一些相關的頭檔案等
3、lib下面是連結動态庫和靜态庫(這些都是我們要用到的)
- 将相關的連結庫以及可執行檔案sqlite3移植到開發闆中即可用指令的形式或者接入函數API接口去處理SQLite資料庫
在指令形式下:
sqlite3檔案的指令是以”.“開頭的,可以使用”.help“檢視具體的資訊,”.quit“是退出;
同樣的sqlite3可以使用sql語句,而sql語句都應該以”;“結尾
- Shell指令下建立資料庫:
- 調用函數的API接口使用,代碼如下:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "sqlite3.h"
#define DATABASE "nilko.db"
char *errmsg;
int do_insert(sqlite3 *db)
{
if(db == NULL)
{
printf("open err\n");
return -1;
}
char sql[100];
char name[20] = {};
int score;
int id;
printf("Input id:");
scanf("%d",&id);
getchar();
printf("Input name:");
scanf("%s",name);
getchar();
printf("Input score:");
scanf("%d",&score);
getchar();
sprintf(sql,"insert into stu values(%d,'%s',%d);",id,name,score);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
printf("%s\n",errmsg);
return -1;
}
printf("insert done\n");
}
int do_delete(sqlite3 *db)
{
if(db == NULL)
{
printf("open err\n");
return -1;
}
char sql[100];
int id;
printf("Input delete id:");
scanf("%d",&id);
sprintf(sql,"delete from stu where id=%d;",id);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
printf("%s\n",errmsg);
return -1;
}
printf("delete done\n");
}
int do_update(sqlite3 *db)
{
if(db == NULL)
{
printf("open err\n");
return -1;
}
char sql[100];
char name[20] = {};
int score;
int id;
printf("Input update id:");
scanf("%d",&id);
printf("Input update score:");
scanf("%d",&score);
sprintf(sql,"update stu set score = %d where id = %d;",score,id);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
printf("%s\n",errmsg);
return -1;
}
printf("update done\n");
}
int callback(void *arg,int f_num,char **f_value,char ** f_name)
{
int i = 0;
for(i = 0; i < f_num; i++)
{
printf("%s\n",f_value[i]);
}
putchar(10);
return 0;
}
int do_query(sqlite3 *db)
{
if(db == NULL)
{
printf("open err\n");
return -1;
}
char sql[100] = {};
sprintf(sql,"select * from stu;");
if(sqlite3_exec(db,sql,callback,NULL,&errmsg) != SQLITE_OK)
{
printf("%s\n",errmsg);
return -1;
}
printf("select ok\n");
}
int main(int argc,char *argv[])
{
int input = 0;
sqlite3 *db;
if(sqlite3_open(DATABASE,&db) != SQLITE_OK)
{
errmsg = sqlite3_errmsg(db);
printf("%s\n", errmsg);
return -1;
}
printf("open nilko.db\n");
if(sqlite3_exec(db,"create table stu(id Integer,name char,score Integer)",NULL,NULL,&errmsg) != SQLITE_OK)
{
printf("%s\n",errmsg);
return -1;
}
printf("create stu ok\n");
while(1)
{
printf("*******************************************************\n");
printf("1:insert;2:delete;3:query;4:updata;5:quit\n");
printf("please input number:\n");
printf("*******************************************************\n");
printf("please input number:");
scanf("%d",&input);
switch(input)
{
case 1:
do_insert(db);
break;
case 2:
do_delete(db);
break;
case 3:
do_query(db);
break;
case 4:
do_update(db);
break;
case 5:
sqlite3_close(db);
exit(0);
default:
printf("please input once again\n");
break;
}
}
return 0;
}
注意:編譯的時候應該連結上剛剛生成的庫以及包含頭檔案 -lsqlite3
- 測試:
注意:API函數接口中有許多函數可以去官網了解具體的函數調用方法,這裡暫時隻用到了幾個函數
sqlite3_open( ):打開資料庫的函數;
sqlite3_close( ):關閉資料庫的函數;
sqlite3_exec( ):執行SQL語句的函數;
sqlite3_errmsg( ):資料庫執行錯誤傳回的語句;
注:調用sqlite3_exec( )進行查詢的時候會使用到回調函數,即每查詢一次就會調用一次回調函數,是以可以在回調函數中進行事務處理;
好了,大概的操作就是這樣~
最後,多多指教~