中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C++操作sqlite3數(shù)據(jù)庫示例代碼

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
一個(gè)簡單的使用sqlite3數(shù)據(jù)庫的電話薄,很簡單適合初學(xué)者看看,而且里面并沒有很多容錯(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("成功打開數(shù)據(jù)庫");
    }
    else printf("數(shù)據(jù)庫打開失敗");
    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("請輸入:");
    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("請選擇?\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.回上級目錄");
    scanf("%d",&num);
    switch (num)
    {
        case 1:
            {
                printf("請輸入名字:");
                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("請輸入電話");
                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("請選擇要執(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("請輸入名字和新電話號碼:");
    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("請輸入要刪除的姓名:");
    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ù)庫\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ù)庫

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:linux常用命令 grep

下一篇:字符串 java字符串編碼轉(zhuǎn)換處理類