標籤:
1:c中使用sqlite3需要調用函數介面操作: sqlite3 *db; int status=sqlite_open("dbname",&db);//開啟或者建立資料庫 int status=sqlite3_exec(db,yuju,huitiaohanshu,0,cuowuzhizhen);//資料庫所有的操作都得通過這個函數執行 sqlite3_close(db);//使用完後要關閉資料庫資源2:sqlite3語句: 建表: create table pic([picId] integer PRIMARY KEY AUTOINCREMENT, [InsertTime] TimeStamp NOT NULL DEFAULT (datetime(‘now‘,‘localtime‘)), [url] varchar(20)); //建立了一個有三個欄位(picId,url,inserttime)並且這裡的插入時間為自動插入當前當地時間 約束條件: not null: unique:唯一 primary key:主鍵 foreign key:外鍵(建立該表和父表之間的聯絡) check:對該項輸入內容的條件檢查 default:預設值 資料類型:(相似匹配,會自動尋找比較合適的具體資料類型進行匹配) integer: int integer int2 int8 (unsigned big int) (big int) text: character(20) varchar(255) text clob.... none: real: real double float.. numeric: boolean data datetime create table teacher(id integer primary key auto increment); create table stu (id integer primary key autoincrement, name varchar(20) check(length(name)>3), tel varchar(11) not null default ‘13631629322‘, cls integer not null , unique(name,tel),//設定name和tel的組合唯一 foreign key(cls) references teacher(id));//綁定兩個表中的id 插入: insert into pic([picId],[url]) values(null,‘%s‘); //當每個欄位都要插入時可以預設表後面的參數 insert into stu1 select * from stu; //將一個表的所有內容匯入另外一個 查詢: select * from pic; select name from stu where id=0; select id from stu order by id;//由id排序輸出 select * from stu where name like "t%";//找到stu中名字以t開頭的資料 select * from stu group by id having id>2;//查詢id>2的資料並且按照id分組 select * from stu limit 1 offset 2;//從索引2開始輸出後面一個資料 //c語言中查詢一般是使用的回調,在執行sql語句的時候就傳入查詢資料以後應該怎麼處理的函數
例子:
#include <stdio.h>#include<time.h>#include <sqlite3.h>#include<string.h>//查詢的回呼函數聲明int select_callback(void * data, int col_count, char ** col_values, char ** col_Name);int main(int argc, char * argv[]){ const char * sSQL1 = "create table pic([picId] integer PRIMARY KEY AUTOINCREMENT, [InsertTime] TimeStamp NOT NULL DEFAULT (datetime(‘now‘,‘localtime‘)), [url] varchar(20));"; char * pErrMsg = 0; int result = 0; // 串連資料庫 sqlite3 * db = 0; int ret = sqlite3_open("./test9.db", &db); if( ret != SQLITE_OK ){ fprintf(stderr, "無法開啟資料庫: %s", sqlite3_errmsg(db)); return(1); } printf("資料庫連接成功!\n"); // 執行建表SQL sqlite3_exec( db, sSQL1, 0, 0, &pErrMsg ); if( ret!=SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", pErrMsg); sqlite3_free(pErrMsg); return 1; } printf("建表成功!\n"); // 執行插入記錄SQL //result = sqlite3_exec( db, "insert into pic([url]) values(‘/c‘);", 0, 0, &pErrMsg); int i; for(i=0;i<5;i++){ if(sqlite3_exec( db, "insert into pic([picId],[url]) values(null,‘/c‘)", 0, 0, &pErrMsg)!= SQLITE_OK){ fprintf(stderr, "insert SQL error: %s\n", pErrMsg); sqlite3_free(pErrMsg); printf("插入失敗!\n"); }else{ printf("插入資料成功\n"); } } // 查詢資料表 printf("開始查詢資料庫內容\n"); //sqlite3_exec( db, "select * from pic;", select_callback, 0, &pErrMsg); if(sqlite3_exec( db, "select * from pic;", select_callback, 0, &pErrMsg)!=SQLITE_OK){ fprintf(stderr, "insert SQL error: %s\n", pErrMsg); }else{ printf("查詢失敗 \n"); } // 關閉資料庫 sqlite3_close(db); db = 0; printf("資料庫關閉成功!\n"); return 0;}int select_callback(void * data, int col_count, char ** col_values, char ** col_Name){ // 每條記錄回調一次該函數,有多少條就回調多少次 int i; for( i=0; i < col_count; i++){ printf( "%s = %s\n", col_Name[i], col_values[i] == 0 ? "NULL" : col_values[i] ); } return 0;}
linux 使用sqlite3