一、linux下sqlite3安裝方法:
1. tar -xvzf sqlite3.....tar.gz
2. cd sqlite3..
3. ./configure -disable-tcl --prefix=/usr/local/sqlite3
4.make;make install
二、基本的增刪改查操作
插入:insert into <表名> values(.....) //表中資料
建表:create table <表名>(.....) //表中所含資料及其類型
查詢:select * from <表名>
C中的回調法:sqlite3_exec(db, sql, select_callback, db, &zErrMsg);
int select_callback(void *Notused, int argc, char *argv, char **azColName)
回呼函數在sqlite3_exec中調用後,argc儲存查詢到的資料條數,argv儲存了資料值,azColName儲存了每項資料的名字;第二個db傳給回呼函數的第一個參數,未使用;
C中的非回調法:sqlite3_get_table(db, sql, &azResult, &nrow, &ncolumn, &zErrMsg)
for(i=0; i < (nrow+1)*ncolumn; i++)
printf("value[%d]=%s\n", i, azResult[i]);
在azResult中儲存的是select的結果,nrow儲存的是取出資料的行數,ncolumn儲存的是取出資料的列數。
修改:update <表名> set <列名>="新值" where 條件 //如果沒有設定條件,這一列都會被更新
刪除:delete from <表名> where <條件> //如果不設定條件,則刪除所用記錄
修改表:sqlite的alter table命令只允許使用者重新命名或添加新的欄位到已有的表中,不能從表中刪除欄位,並且只能在 表的末尾添加欄目;
修改表名:alter table <舊錶名> rename to<新表名>
添加一列:alter table <表名> add column <列名> <資料類型> [限定符]
刪除表:drop table <表名>
三、sqlite3命令列常用命令
進入到sqlite3命令列模式,用"."+"關鍵字"執行命令
.tables——列出當前資料庫中所含表
.schema <表名>——顯示建表時執行的操作
.headers on——讓輸出時顯示表頭資訊
.database——顯示資料庫資訊
.mode []——設定顯示方式:csv、column、html、insert、line、list、tabs、tcl,如.model csv
四、從excel中匯入資料
1.在test.db中建表:create table bookroom(id integer, roomname nvarchar(20), mapfilename nvarchar(20));
2.建立exc,產生csv格式的資料,和上表格式一致,如下:
30001, 數學, 3樓1號
30002, 語文, 2樓18號
將excel另存新檔csv格式:bookroom.csv
3.匯入資料:
①開啟資料庫,進入sqlite3命令列模式,sqlite3 test.db
②.separator ‘,’
③.import bookroom.csv bookroom
或者:
#sqlite3 test.db ".import bookroom.csv bookroom"
4.匯出資料庫中的資料到txt檔案
#sqlite3 -header test.db "select *from bookroom;">license.txt
參考資料:
http://www.2cto.com/database/201112/113683.html
http://it.100xuexi.com/ExtendItem/OTDetail.aspx?id=15123afe-d40f-4439-b14b-f08c034ce295
http://blog.chinaunix.net/uid-1844931-id-2981048.html
http://www.cnblogs.com/nbsofer/archive/2012/05/02/2479168.html