嵌入式sqlite3html
1、sqlite3安裝sql
1.本地安裝
數據庫
下載安裝包,官網地址:http://www.sqlite.org/download.html數組
步驟以下: 函數
$tar xvfz sqlite-autoconf-3071502.tar.gz $cd sqlite-autoconf-3071502 $./configure --prefix=/usr/local //指定安裝目錄 $make $make instal
2.在線安裝ui
sudo apt-get install sqlite3spa
查看是否安裝完成 sqlite3指針
在終端輸入sqlite3是否顯示以下內容:code
SQLite version 3.11.0 2016-02-15 17:29:24sqlite
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite>
使用中編譯時出現: fatal error: sqlite3.h: 沒有那個文件或目錄
描述:找不到頭文件
緣由:系統沒有安裝函數庫
解決辦法:sudo apt-get install libsqlite3-dev
2、sqlite3數據庫命令(重點)
一、系統命令,都以'.'開頭
sqlite> help 打開幫助手冊查看各類系統命令
.exit
.quit
.table 查看錶
.schema 查看錶的結構
二、sql語句,都以‘;’結尾
1.打開(建立)一個數據庫(必須先建立一個數據庫才能在數據庫中創建表格)
sqlite3 xxx.db
2-- 在數據庫中建立一張表
create table stuinfo(id integer, name text, age integer, score float);
3-- 插入一條記錄
insert into stuinfo values(1001, 'zhangsan', 18, 80);
insert into stuinfo (id, name, score) values(1002, 'lisi', 90);
4-- 查看數據庫記錄
select * from stuinfo;(*通配表示全部數據)
select name,score from stuinfo;
查詢指定的字段
where表示選取,and表示與(同時知足),or表示或
select * from stuinfo where score = 80;
select * from stuinfo where score = 80 and name= 'zhangsan';
select * from stuinfo where score = 80 or name='wangwu'
select * from stuinfo where score >= 85 and score < 90;
5-- 刪除一條記錄
delete from stuinfo where id=1003 and name='zhangsan';
6-- 更新一條記錄
update stuinfo set age=20 where id=1003;
update stuinfo set age=30, score = 82 where id=1003;
7-- 刪除一張表
刪除整張表而不是某一條記錄與delete區分
drop table stuinfo;
8-- 增長一列
alter table stuinfo add column sex char;
9-- 刪除一列
因sqlite沒有直接刪除一列的功能,因此分三步,先新建一個數據庫複製原來的數據庫
(除去想要刪除的那一列不復制)
而後刪除原來的數據庫,再把新建的數據庫更名爲原來的數據庫,達到刪除一列的目的。
create table stu as select id, name, score from stuinfo; /*建立新表*/
drop table stuinfo;/*刪除原表*/
alter table stu rename to stuinfo;/*更名*/
數據庫設置主鍵:
create table info(id integer primary key autoincrement, name vchar);
主鍵就是限制資料不重複的字段﹐設置爲主鍵的字段(可多個字段一塊兒作主鍵)﹐設了主鍵就限制了資料的惟一性。
例如在人事資料中有一個身份徵號的字段﹐這個就可設爲主鍵(由於身份徵號不會重複),但姓名就 不能夠,由於姓名能夠重複。
另外設置了主鍵有利於提升數據的檢索速度﹐也保證數據的準確性
3.sqlite3 數據庫 C語言 API
1)建立(打開)數據庫函數
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */(字符串或者數組名數據庫的名字xxx.db格式)
sqlite3 **ppDb /* OUT: SQLite db handle */ (sqlite型的二級指針,傳入sqlite *類型的地址)
);
功能:打開數據庫
參數:filename 數據庫名稱
ppdb 數據庫句柄
返回值:成功爲0 (SQLITE_OK) ,出錯 錯誤碼
2)關閉數據庫函數
int sqlite3_close(sqlite3* db);
功能:關閉數據庫
參數:
返回值:成功爲0 SQLITE_OK ,出錯 錯誤碼
3)打印錯誤信息函數
const char *sqlite3_errmsg(sqlite3*db);
功能:經過db指針,獲得錯誤信息的描述,
返回值:錯誤信息首地址(用printf打印,相似於strerror函數)
4)數據庫操做函數(重點)
Int sqlite3_exec(sqlite3 *db, const char *sql, sqlite3_callback callback, void *, char **errmsg);
功能:執行SQL操做
db:數據庫句柄
sql:SQL語句(操做命令的字符串)
callback:回調函數(在查詢功能是才調用回調函數,其餘功能直接傳NULL)
arg : 爲回調函數傳遞參數
errmsg:錯誤信息指針的地址
返回值:成功返回0,失敗返回錯誤碼
回調函數(查詢使用):
Typedef int (*sqlite3_callback)(void *arg, int n, char **f_value, char **f_name);
功能:查詢語句執行以後,會回調此函數,每找到一條記錄自動執行一次回調函數
參數:arg 接收sqlite3_exec 傳遞來的參數,函數指針類型(函數名)
ncolumns 列數
f_value 列的值得地址,f_value[0]~f_value[f_ncolumns
f_name 列的名稱
返回值:0,
5)查詢(查找)數據庫的另外一種方法
不使用回調函數執行SQL語句
int sqlite3_get_table(sqlite3 *db, const char *sql, char ***resultp, int*nrow, int *ncolumn, char **errmsg);
功能:執行SQL操做
db:數據庫句柄
sql:SQL語句
resultp:用來指向sql執行結果的指針(相似於指針數組)
nrow:知足條件的記錄的數目(行數,不包括標題欄)
ncolumn:每條記錄包含的字段數目(列數)
errmsg:錯誤信息指針的地址
返回值:成功返回0,失敗返回錯誤碼
示例代碼:建立一個數據庫,其中保存學生信息(學號id,姓名,年齡,性別,分數等等)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sqlite3.h> #define DATABASE "student.db" #define N 128 int do_insert(sqlite3 *db) { int id; char name[32] = {}; char sex; int score; char sql[N] = {}; char *errmsg; printf("Input id:"); scanf("%d", &id); printf("Input name:"); scanf("%s", name); getchar(); printf("Input sex:"); scanf("%c", &sex); printf("Input score:"); scanf("%d", &score); sprintf(sql, "insert into student values(%d, '%s', '%c', %d)", id, name, sex, score); if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) { printf("%s\n", errmsg); } else { printf("Insert done.\n"); } return 0; } int do_delete(sqlite3 *db) { int id; char sql[N] = {}; char *errmsg; printf("Input id:"); scanf("%d", &id); sprintf(sql, "delete from student where id = %d", id); if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) { printf("%s\n", errmsg); } else { printf("Delete done.\n"); } return 0; } int do_update(sqlite3 *db) { int id; char sql[N] = {}; int score; char *errmsg; printf("Input id:"); scanf("%d", &id); getchar(); printf("Update score:"); scanf("%d",&score); getchar(); sprintf(sql, "update student set score =%d where id=%d",score,id); if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) { printf("%s\n", errmsg); } else { printf("update done.\n"); } return 0; } /*f_value f_name 傳出參數,確定定義在了數據庫的其餘地方*/ int callback(void *arg, int f_num, char ** f_value, char ** f_name) { int i = 0; for(i = 0; i < f_num; i++) { // printf("%-8s %s", f_value[i], f_name[i]); printf("%-8s", f_value[i]); } printf("\n++++++++++++++++++++++"); putchar(10); return 0; } int do_query(sqlite3 *db) { char *errmsg; //char sql[N] = "select count(*) from stu where name='zhangsan';"; char sql[N]; sprintf(sql,"select *from student"); if(sqlite3_exec(db, sql, callback,NULL,&errmsg) != SQLITE_OK) { printf("%s", errmsg); } else { printf("select done.\n"); } return 0; } int do_query1(sqlite3 *db) { char *errmsg; char ** resultp; int nrow; int ncolumn; if(sqlite3_get_table(db, "select * from student", &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK) { printf("%s\n", errmsg); return -1; } else { printf("query done.\n"); } int i = 0; int j = 0; int index = ncolumn;//跳過標題直接從數據開始打印 for(j = 0; j < ncolumn; j++) { printf("%-10s ", resultp[j]); } putchar(10); for(i = 0; i < nrow; i++) { for(j = 0; j < ncolumn; j++) { printf("%-10s ", resultp[index++]); } putchar(10); } return 0; } int main(int argc, const char *argv[]) { sqlite3 *db; char *errmsg; int n; if(sqlite3_open(DATABASE, &db) != SQLITE_OK) { printf("%s\n", sqlite3_errmsg(db)); return -1; } else { printf("open DATABASE success.\n"); } if(sqlite3_exec(db, "create table if not exists student(id int, name char , sex char , score int);", NULL, NULL, &errmsg) != SQLITE_OK) { printf("%s\n", errmsg); } else { printf("Create or open table success.\n"); } while(1) { printf("********************************************\n"); printf("1: insert 2:query 3:delete 4:update 5:quit\n"); printf("********************************************\n"); printf("Please select:"); scanf("%d", &n); switch(n) { case 1: do_insert(db); break; case 2: do_query(db); //do_query1(db); break; case 3: do_delete(db); break; case 4: do_update(db); break; case 5: printf("main exit.\n"); sqlite3_close(db); exit(0); break; default : printf("Invalid data n.\n"); } } return 0; }