国产成人精品三级麻豆,色综合天天综合高清网,亚洲精品夜夜夜,国产成人综合在线女婷五月99播放,色婷婷色综合激情国产日韩

當(dāng)前位置:首頁 > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 學(xué)習(xí)筆記 > sqlite3數(shù)據(jù)庫詳細(xì)介紹,為sqlite3數(shù)據(jù)庫學(xué)習(xí)加速

sqlite3數(shù)據(jù)庫詳細(xì)介紹,為sqlite3數(shù)據(jù)庫學(xué)習(xí)加速 時間:2018-08-14      來源:未知

sqlite3數(shù)據(jù)庫從sqlite系統(tǒng)命令,sqlite3 的使用,sqlite3 API 函數(shù)接口等這些方面來詳細(xì)了解sqlite3數(shù)據(jù)庫

安裝數(shù)據(jù)庫:sudo apt-get install sqlite3

創(chuàng)建數(shù)據(jù)庫:sqlite3 stu.db必須實(shí)現(xiàn)指定數(shù)據(jù)庫名

【1】sqlite系統(tǒng)命令

系統(tǒng)命令以 "."開頭普通命令,以";"結(jié)束

.schema 查看表的結(jié)構(gòu)

.quit 退出數(shù)據(jù)庫(ctrl + \ 強(qiáng)制退出)

.exit 退出數(shù)據(jù)庫

.help 查看幫助信息

.databases 查看數(shù)據(jù)庫

.table 顯示數(shù)據(jù)庫中所有的表的表名

【2】sqlite3 的使用

1)創(chuàng)建一張表create table 表名(字段名稱1 字段類型,字段名稱2 字段類型, ....);

create table stu(id int, name char[20], sex char , score int);

2)向表中插入一條記insert into 表名 values (字段值1,字段值2,...);

錄insert into stu values(1001, 'zhangsan', 'm', 89);

insert into stu (id, name, sex,score) values(1002, 'lisi', 'm', 99);

select * from stu;// 查找所有的記錄

select * from stu where id=1001;// 查找符號條件的記錄

3)查詢記錄select * from stu where name= 'zhangsan';

// 字符串需要加引號(單雙皆可,功能相同)

select * from stu where name = 'zhangsan' or score=92;

4)刪除記錄delete from stu where id=1004;

5)更新記錄update stu set score=98 where id=1003;

6)刪除一張表drop table stu;

7)添加一列 alter table stu add column score int;

sqlite3 不允許直接刪除一列

①先創(chuàng)建一張新表

create table stu1 as select id , name from stu;

8)刪除一列 ②刪除原來的舊表

drop table stu;

③對新表重命名

alter table stu1 rename to stu;

9)數(shù)據(jù)庫主鍵 (既設(shè)置的數(shù)據(jù)將會是唯一存在的)

create table usr(name text primary key , passwd text);

【3】sqlite3 API 函數(shù)接口

gcc 編譯時應(yīng)該連接 sqlite3 庫(靜態(tài)庫)

gcc *.c -o *** -lsqlite3

所需頭文件 #include

int sqlite3_open(

函數(shù)原型 const char *filename,/* Database filename (UTF-8) */

sqlite3 **ppDb/* OUT: SQLite db handle */

);

功能 創(chuàng)建或者打開一個數(shù)據(jù)庫

filename 數(shù)據(jù)庫名字

參數(shù)

ppdb 操作數(shù)據(jù)庫的指針,句柄。

成功 SQLITE_OK

返回值

失敗 error_code

例 sqlite3 *db;

if(sqlite3_open("stu.db", &db) != SQLITE_OK)

{

printf("%s\n", sqlite3_errmsg(db));

}

所需頭文件 #include

int sqlite3_exec(

sqlite3* db,/* An open database */

const char *sql,/* SQL to be evaluated */

int (*callback)(void*,int,char**,char**), /* Callback function */

函數(shù)原型 void * arg,/* 1st argument to

callback */

char **errmsg/* Error msg written here

*/

);

功能執(zhí)行一條sql語句

參數(shù)db 數(shù)據(jù)庫的句柄指針

sql 將要被執(zhí)行sql語句

callback 回調(diào)函數(shù), 只有在查詢語句時,才給回調(diào)函數(shù)傳參

int (*callback)(void* arg ,int ncolumn ,char** f_value,char**

f_name)

功能:得到查詢結(jié)果

參數(shù):arg 為回調(diào)函數(shù)傳遞參數(shù)使用的

ncolumn 記錄中包含的字段的數(shù)目

f_value包含每個字段值的指針數(shù)組

f_name包含每個字段名稱的指針數(shù)組

返回值:成功 0,出錯非0

arg 為callback 傳參的

errmsg 錯誤信息的地址

成功 SQLITE_OK

返回值

失敗 error_code

char *errmsg;

if(sqlite3_exec(db, "create table stu(id int, name char, score int)",

例子NULL, NULL, &errmsg) != SQLITE_OK)

{

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

}

所需頭文件 #include

int sqlite3_get_table(

sqlite3 *db,/* An open database */

const char *zSql,/* SQL to be evaluated */

函數(shù)原型char ***pazResult, /* Results of the query */

int *pnRow,/* Number of result rows written here */

int *pnColumn,/* Number of result columns written here */

char **pzErrmsg/* Error msg written here */

);

功能查詢數(shù)據(jù)庫,它會創(chuàng)建一個新的內(nèi)存區(qū)域來存放查詢的結(jié)果信息

db 數(shù)據(jù)庫操作句柄

sql 數(shù)據(jù)庫的sql語句

azResult 查詢的結(jié)果

行數(shù),nrow的值為查詢到的符合條件的記錄數(shù)(不包括字段名)。

參數(shù) 注意:nrow的值不包括字段名,如果打印時用for (i = 0; i < nrow;

nRowi++)會打印出字段名,但是會少打印出一條符合條件的記錄。因此打

印時要用 for (i = 0; i

印出來。

nColumn列數(shù),ncolumn的值為查詢到的符合條件的字段數(shù)。

errmsg錯誤消息

成功SQLITE_OK

返回值

失敗error_code

const char *sqlite3_errmsg(sqlite3* db);功能:獲取錯誤信息描述

int sqlite3_close(sqlite3* db);功能:關(guān)閉一個數(shù)據(jù)庫

void sqlite3_free_table(char **result);功能:釋放內(nèi)存

推薦書籍:

UNIX高級環(huán)境編程

UNIX網(wǎng)絡(luò)編程 卷一 卷二

TCP/IP詳解 卷一 卷二 卷三

UNIX/LINUX系統(tǒng)編程手冊 上冊 下冊

上一篇:同步與異步的區(qū)別詳解

下一篇:Linux下網(wǎng)絡(luò)編程實(shí)現(xiàn)UDP,還有大量實(shí)例

熱點(diǎn)文章推薦
華清學(xué)員就業(yè)榜單
高薪學(xué)員經(jīng)驗(yàn)分享
熱點(diǎn)新聞推薦
前臺專線:010-82525158 企業(yè)培訓(xùn)洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠(yuǎn)見科技集團(tuán)有限公司 版權(quán)所有 ,京ICP備16055225號-5,京公海網(wǎng)安備11010802025203號

回到頂部