C++操作sqlite3數(shù)據(jù)庫(kù)示例代碼
2018-07-20 來(lái)源:open-open

一個(gè)簡(jiǎn)單的使用sqlite3數(shù)據(jù)庫(kù)的電話薄,很簡(jiǎn)單適合初學(xué)者看看,而且里面并沒(méi)有很多容錯(cuò)處理很容易看懂的。
#include<stdio.h> #include"sqlite3.h" static int callback(void *notused,int l,char **v,char **lname); int seek(sqlite3 *db); int insert(sqlite3 *db); int replace(sqlite3 *db); int del(sqlite3 *db); int option(sqlite3 *db); int quit(sqlite3 *db); int main() { int num; int rc; struct sqlite3 *db; rc=sqlite3_open("sql.db",&db); if(rc) { printf("成功打開(kāi)數(shù)據(jù)庫(kù)"); } else printf("數(shù)據(jù)庫(kù)打開(kāi)失敗"); num=option(db); quit(db); } int option(sqlite3 *db) { int num; int rc; printf("\t1.查詢\n\t2.添加記錄\n\t3.更新記錄\n\t4.刪除記錄\n\t5.清屏\n\t6.創(chuàng)建表\n\t7.退出\n"); printf("請(qǐng)輸入:"); scanf("%d",&num); switch (num) { case 1:seek(db); break; case 2:rc=insert(db); break; case 3:replace(db); break; case 4:del(db); break; case 5: { system("cls"); option(db); } case 6: { rc=sqlite3_exec(db,"CREATE TABLE telbook(name,telnum)",0,0,NULL); printf("%d\n",rc); } break; case 7:{ printf("即將關(guān)閉\n"); quit(db); } break; default: { printf("請(qǐng)選擇?\n"); option(db); } break; } } int seek(sqlite3 *db) { int num; int rc; char *zSQL; char *sql_name[20],*sql_telnum[11]; printf("\t1.根據(jù)名字查找\n\t2.根據(jù)電話查找\n\t3.顯示所有\(zhòng)n\t4.回上級(jí)目錄"); scanf("%d",&num); switch (num) { case 1: { printf("請(qǐng)輸入名字:"); scanf("%s",sql_name); zSQL= sqlite3_mprintf("select * from telbook where name='%q'", sql_name); rc=sqlite3_exec(db,zSQL,callback,0,NULL); sqlite3_free(zSQL); } break; case 2: { printf("請(qǐng)輸入電話"); scanf("%s",sql_telnum); zSQL= sqlite3_mprintf("select * from telbook where telnum='%q'", sql_telnum); rc=sqlite3_exec(db,zSQL,callback,0,NULL); sqlite3_free(zSQL); } break; case 3:rc=sqlite3_exec(db,"select * from telbook",callback,0,NULL); break; case 4:option(db); break; default : { printf("請(qǐng)選擇要執(zhí)行的操作\n"); option(db); } } printf("%d\n",rc); option(db); } int insert(sqlite3 *db) { int rc; char *sql_name[20],*sql_telnum[11]; printf("輸入姓名和電話以空格分隔:"); scanf("%s %s",sql_name,sql_telnum); char *zSQL; zSQL= sqlite3_mprintf("INSERT INTO telbook VALUES('%q','%q')", sql_name,sql_telnum); rc=sqlite3_exec(db,zSQL,0,0,NULL); sqlite3_free(zSQL); printf("%d\n",rc); option(db); return 0; } int replace(sqlite3 *db) { int rc; char *sql_name[20],*sql_telnum[11]; printf("請(qǐng)輸入名字和新電話號(hào)碼:"); scanf("%s %s",sql_name,sql_telnum); char *zSQL; zSQL= sqlite3_mprintf("update telbook set telnum='%q' where name='%q'", sql_telnum,sql_name); rc=sqlite3_exec(db,zSQL,0,0,NULL); sqlite3_free(zSQL); printf("%d\n",rc); option(db); return 0; } int del(sqlite3 *db) { int rc; char *sql_name[20],*sql_telnum[11]; printf("請(qǐng)輸入要?jiǎng)h除的姓名:"); scanf("%s",sql_name); char *zSQL; zSQL= sqlite3_mprintf("DELETE FROM telbook where name='%q'", sql_name); rc=sqlite3_exec(db,zSQL,0,0,NULL); sqlite3_free(zSQL); printf("%d\n",rc); option(db); return 0; } int quit(sqlite3 *db) { int rc; rc=sqlite3_close(db); if (rc) { printf("成功關(guān)閉數(shù)據(jù)庫(kù)\n"); } else printf("關(guān)閉失敗\n"); system("pause"); exit(1); } static int callback(void *notused,int l,char **v,char **lname) { int i; for (i=0;i<l;i++) { printf("%s\t",v[i]); } printf("\n"); return 0; }
標(biāo)簽: 數(shù)據(jù)庫(kù)
版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。
最新資訊
熱門(mén)推薦