目錄
- SQLite簡介(面試)
- SQLite資料庫安裝
- SQLite指令用法
- SQLite的程式設計操作
-
- 打開/建立資料庫的C接口
- 建立表的接口
SQLite簡介(面試)
輕量化,易用的嵌入式資料庫,用于裝置端的資料管理,可以了解成單點的資料庫。傳統伺服器型資料庫用于管理多段裝置,更加複雜
SQLite是一個無伺服器的資料庫,是自包含的。這也稱為嵌入式資料庫,,這意味着資料庫引擎作為應用程式的一部分運作。
MySQL需要運作服務區,MySQL将需要用戶端和伺服器架構通過網絡進行互動
SQLite的優點 | SQLite的缺點 |
---|---|
基于檔案,易于設定和使用 适合基礎開發和測試 輕松攜帶 使用标準SQL文法進行微小更改 使用友善 | 缺乏使用者管理和安全功能 不容易擴充 不适合大資料庫 無法定制 |
MySQL的優點 | MySQL的缺點 |
---|---|
使用友善 提供了許多資料庫相關功能 良好的安全功能 易于擴充,适用大型資料庫 提供良好的速度和性能 提供良好的使用者管理和多種通路機制 | 需要一些專業技術來設定 與傳統SQL相比,文法略有不同 |
基于嵌入式的資料庫主要有:SQLite,Firebird,Berkeley DB,eXtremDB
Firebird
是關系型資料庫,功能強大,支援存儲過程,SQL相容等
SQLite
是關系型資料庫,體積小,支援ACID事務
Berkeley DB
并沒有資料庫服務的概念,他的程式直接連接配接到應用程式中
eXtremDB
是記憶體資料庫,運作效率高
SQLite資料庫安裝
安裝方式一:
sudo apt-get install sqlite
安裝方式二:
https://www.sqlite.org/download.html

1. 把下載下傳的檔案sqlite-autoconf-3390000.tar.gz 上傳到開發闆
2. tar vxf sqlite-autoconf-3390100.tar.gz 解壓
3. cd sqlite-autoconf-3390000 進入檔案夾
4. ./configure prefix=/usr/local 配置安裝路徑在/usr/local
5. make //編譯,比較久10分鐘
6. sudo make install //安裝
如上圖,安裝成功,運作sqlite3 進入SQL指令操作流程
SQLite指令用法
建立一個資料庫
方式1:
1. sqlite3 進入資料庫
2. .open test.db
3. .quit
資料庫退出後在指令目前路徑建立資料可test.db
方式2:
sqlite3 test.db 在指令運作目前視窗建立資料庫test.db
在資料庫指令下
.databases 列出目前打開的資料庫
.quit //退出
建立一張表格
create table stu(id Integer,name char,score Integer);
插入一條記錄
insert into stu values(001,'huang',99);
insert into stu values(002,'gang',100); ''和""都行
insert into stu(name,score) values("huanggang",98); 插入部分字段内容
檢視資料庫的記錄
select * from stu; 查詢所有字段的結果
select name,score from stu; 查詢資料庫中部分字段的内容
删除一條記錄
delete from stu where id = 001;
更新一條記錄
update stu set name = 'huangg' where id = 004;
删除一張表
drop table stu;
增加一列
alter table stu add column sex char;
SQLite的程式設計操作
打開/建立資料庫的C接口
下面的C代碼段顯示了如何連接配接到一個現有的資料庫。如果資料庫不存在,那麼它就會被建立,最後将傳回一個資料庫對象。
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: SQLite db handle */
);
該例程打開一個指向 SQLite 資料庫檔案的連接配接,傳回一個用于其他 SQLite 程式的資料連接配接對象
int sqlite3_close(sqlite3*);
該例程關閉之前調用 sqlite3_open()打開資料庫連接配接。所有與連接配接相關的語句都應該在連接配接關閉之前完成。
如果查詢還沒有完成,sqlite3_close() 将傳回 SQLITE_BUSY 禁止關閉的錯誤資訊。
const char *sqlite3_errcode(sqlite3*);
sqlite3_errcode()通常用來擷取最近調用的API接口傳回的錯誤代碼。
#define SQLITE_OK 0 /* 成功 | Successful result */
/* 錯誤碼開始 */
#define SQLITE_ERROR 1 /* SQL錯誤 或 丢失資料庫 | SQL error or missing database */
#define SQLITE_INTERNAL 2 /* SQLite 内部邏輯錯誤 | 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 /* 某次 malloc() 函數調用失敗 | A malloc() failed */
#define SQLITE_READONLY 8 /* 嘗試寫入一個隻讀資料庫 | Attempt to write a readonly database */
#define SQLITE_INTERRUPT 9 /* 操作被 sqlite3_interupt() 函數中斷 | Operation terminated by sqlite3_interrupt() */
#define SQLITE_IOERR 10 /* 發生某些磁盤 I/O 錯誤 | Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT 11 /* 資料庫磁盤映像不正确 | The database disk image is malformed */
#define SQLITE_NOTFOUND 12 /* sqlite3_file_control() 中出現未知操作數 | Unknown opcode in sqlite3_file_control() */
#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 /* 資料庫為空 | Database is empty */
#define SQLITE_SCHEMA 17 /* 資料結構發生改變 | The database schema changed */
#define SQLITE_TOOBIG 18 /* 字元串或二進制資料超過大小限制 | String or BLOB exceeds size limit */
#define SQLITE_CONSTRAINT 19 /* 由于限制違例而取消 | Abort due to constraint 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_FORMAT 24 /* 附加資料庫格式錯誤 | Auxiliary database format error */
#define SQLITE_RANGE 25 /* 傳遞給sqlite3_bind()的第二個參數超出範圍 | 2nd parameter to sqlite3_bind out of range */
#define SQLITE_NOTADB 26 /* 被打開的檔案不是一個資料庫檔案 | File opened that is not a database file */
#define SQLITE_ROW 100 /* sqlite3_step() 已經産生一個行結果 | sqlite3_step() has another row ready */
#define SQLITE_DONE 101 /* sqlite3_step() 完成執行操作 | sqlite3_step() has finished executing */
/* 錯誤碼結束 */
打開建立關閉錯誤示例代碼
#include <stdio.h>
#include <sqlite3.h>
int main(int argc, char **argv)
{
sqlite3 *db;
int ret;
if(argc < 2){
printf("Usage: %s xxx.db\n",argv[0]);
return -1;
}
if((ret = sqlite3_open(argv[1],&db)) == SQLITE_OK){
printf("open %s success\n",argv[1]);
}else{
printf("error:%s %d\n",sqlite3_errmsg(db),ret);
if(ret == 14){
printf("permission deny\n");
}
return -1;
}
sqlite3_close(db);
printf("done\n");
return 0;
}
建立表的接口
下面的C代碼段用于在先前建立的資料庫中建立一個表:
int sqlite3_exec(
sqlite3, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
sqlite3_exec用于執行一條sql語句,參數如下:
參數1:打開資料庫得到的指針;
參數2:一條sql語句,是一個以“\0”結尾的字元串;
參數3:使用者提供的回調函數,如果不需要回調函數,可以填 NULL。比如做insert 操作,做delete操作,就沒有必要使用回調。而當你做select 時,就要使用回調;
參數4:回調函數需要傳遞的參數,如果不需要,可以填 NULL;
參數5:錯誤資訊
sqlite3_exec()程式解析并執行由 sql 參數所給的每個指令,直到字元串結束或遇到錯誤為止。
int callback(void *arg, int column_size, char *column_value[], char *column_name[])
參數分析:void *arg:是sqlite3_exec函數的第四個參數 column_size:資料庫的字段數
column_value[]:列的值 column_name:字段名字
C代碼建表和插入資料
#include <stdio.h>
#include <sqlite3.h>
int callback(void *arg, int column_size, char *column_value[], char *column_name[])
{
int i;
printf("arg=%s\n",(char *)arg);
for(i=0;i<column_size;i++){
printf("%s = %s\n",column_name[i],column_value[i]);
}
printf("===============================\n");
return 0;//必須傳回0,這樣資料庫中由多少條資料,這個回調函數就會被調用多少次
}
int main(int argc, char **argv)
{
sqlite3 *db;
char *errorMes = NULL;
int ret;
if(argc < 2){
printf("Usage: %s xxx.db\n",argv[0]);
return -1;
}
if((ret = sqlite3_open(argv[1],&db)) == SQLITE_OK){
printf("open %s success\n",argv[1]);
}else{
printf("error:%s %d\n",sqlite3_errmsg(db),ret);
if(ret == 14){
printf("permission deny\n");
}
return -1;
}
// int sqlite3_exec(sqlite3, const char *sql, int (*callback)(void*,int,char**,char**), void *, char **errmsg);
ret = sqlite3_exec(db, "create table class01(id Integer,name char,score Integer);", \
callback, "content of sql:", &errorMes);//errorMes may sigment error!
if(ret != SQLITE_OK){
printf("create tables error: %s\n",errorMes);
}
}
ret = sqlite3_exec(db, "insert into class01 values(001,'huang',99);", \
callback, "content of sql:", &errorMes);//errorMes may sigment error!
printf("insert: %d,%s\n",ret,errorMes);
ret = sqlite3_exec(db, "select * from class01", \
callback, "content of sql:", &errorMes);//errorMes may sigment error!
printf("select: %d,%s\n",ret,errorMes);
sqlite3_close(db);
printf("done\n");
return 0;
}