sqlite資料庫操作例程:
[plain]view plain
copy
#include
#include
#include
int main()
memset(sql,'\0',128);
strcpy(sql,"create table student(id integer,name varchar(10),f float);");//建立表
sqlite3_exec(db,sql,0,0,&zerrmsg); //執行sqlite命令語句
memset(sql,'\0',128);
strcpy(sql,"insert into student values(1,'xiaoming',1.1);"); //插入資料
rc = sqlite3_exec(db,sql,0,0,&zerrmsg);
if(rc != sqlite_ok)
memset(sql,'\0',128);
strcpy(sql,"insert into student values(2,'xiaohong',1.2);");
rc = sqlite3_exec(db,sql,0,0,&zerrmsg);
if(rc != sqlite_ok)
int nrow = 0,ncolnum = 0;
char **azresult; //存放查詢結果
memset(sql,'\0',128);
strcpy(sql,"select * from student;");
rc =sqlite3_get_table(db,sql,&azresult,&nrow,&ncolnum,&zerrmsg); //查詢資料
if(rc != sqlite_ok)
int i = 0;
printf("row:%d colnum=%d\n",nrow,ncolnum);
for(i = ncolnum;i
for(i = 0;i
printf("\n");
memset(sql,'\0',128);
strcpy(sql,"delete from student;"); //刪除
sqlite3_exec(db,sql,0,0,&zerrmsg);
sqlite3_free(zerrmsg);
sqlite3_free_table(azresult); //釋放空間
sqlite3_close(db); //關閉資料庫
return 0;
} 編譯:gcc test.c -o test -lsqlite3
執行結果:
另一套查詢介面:
[objc]view plain
copy
#include
#include
#include
#include
intmain()
sprintf(sql,
"create table student(id integer,name varchar(10),f float,occur datetime default(datetime('now','localtime')));"
);
sqlite
3_exec(db,sql,0,
0,&zerrmsg);
sprintf(sql,
"insert into student(id,name,f) values(%d,'%s',%f);",1
,"小明",3
.33);
if(sqlite
3_exec(db,sql,0,
0,&zerrmsg)!=sqlite_ok)
sqlite3_stmt
*stmt;
intncols;
sprintf(sql,"select * from student"
);
rc =
sqlite3
_prepare
(db,sql,strlen(sql),&stmt,
null
);
if(rc !=sqlite_ok)
while
(sqlite
3
_step
(stmt) == sqlite_row)
sqlite3
_finalize(stmt);
sqlite3_close(db);
}
c語言程式設計(3)
上課前首先是對作業的複習鞏固,完成這些作業最重要的還是要勤於思考,有錯誤及時修改,並搞明白每個字元以及每一步的含義,我真正感到自己學的不夠紮實,或許是理解不透徹,因此不斷改錯並牢記這些易錯點顯得尤為重要。選擇結構與條件判斷 兩選擇語句 1 if語句,實現兩分支的選擇結構 2 switch語句,來實現...
C語言封裝sqlite3 API
執行類似insert,update,create,drop這些不需要結果的sql語句。int executenoquery sqlite3 db,const char sql if sqlite3 step pstmt sqlite done if pstmt null sqlite3 finali...
go語言使用sqlite3
1 在執行命令視窗建立資料庫和sql表d go src mytest sqlite3 建立資料表,使用.open命令,如果有這個資料庫開啟,若沒有則建立 sqlite open userdb.db 檢視當前使用的資料庫 sqlite database main d go src mytest use...