C++操作sqlite3数据库示例代码

wn25的头像 wn25 23 2015-01-04 21:01 0

 基本信息

× 1   

浏览数: 4989

分享时间: 4 年 前

一个简单的使用sqlite3数据库的电话薄,很简单适合初学者看看,而且里面并没有很多容错处理很容易看懂的。
#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("成功打开数据库");
    }
    else printf("数据库打开失败");
    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.创建表\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("即将关闭\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.根据名字查找\n\t2.根据电话查找\n\t3.显示所有\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("请选择要执行的操作\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("成功关闭数据库\n");
    }
    else printf("关闭失败\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;
}


12 [下一页]

  • wudangdang的头像 wudangdang 2016-05-23 14:04 代码数:0

    SQLite数据库函数执行成功返回SQLITE_OK,值为0,程序中利用返回值判断有误。

    char *sql_name[20],*sql_telnum[11] 定义指针数组,其实定义数组就可以了。

  • gererik的头像 gererik 2018-07-01 20:36 代码数:0

    Merely  a smiling  visitant here to share the lo e (:, btw  outstanding  style.  Brazilian plastic surgeon

  • ossaa的头像 ossaa 2019-01-09 20:17 代码数:0

    This is a truly good site post. Not too many people would actually, the way you just did. I am really impressed that there is so much information about this subject that have been uncovered and you’ve done your best, with so much class. If wanted to know more about green smoke reviews, than by all means come in and check our stuff. 

    kids placemats

  • ossaa的头像 ossaa 2019-01-14 05:35 代码数:0

    I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well!

    EZ Articles

  • cixexey的头像 cixexey 2018-11-18 18:33 代码数:0

    You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! news

  • gererik的头像 gererik 2018-04-15 18:30 代码数:0

    It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing.www.investhz.com

  • jhoneila的头像 jhoneila 2019-01-16 18:51 代码数:0

    This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information... SEO

  • michael321的头像 michael321 2018-11-28 14:00 代码数:0

    Our security arrangement is exceptionally strict and we never unveiled any close to home or record data of the customer to any outsider. 

    dissertation help

  • michael321的头像 michael321 2018-11-28 14:01 代码数:0

    Our security arrangement is exceptionally strict and we never unveiled any close to home or record data of the customer to any outsider. 

    dissertation help

  • ossaa的头像 ossaa 2018-08-18 18:22 代码数:0

    Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post. 

    havaianas

您的评论: