标题:关于SQLITE的
只看楼主
kysh
Rank: 1
等 级:新手上路
帖 子:5
专家分:6
注 册:2014-10-19
 问题点数:0 回复次数:0 
关于SQLITE的
急问,我要将下面的代码改成连接sqlite数据库的,然后我首先创建数据库
int main(int argc, char** argv)
    {
        sqlite3* pDB = NULL;                 /* 指向数据库的指针 */
        sqlite3_stmt* pStmt = NULL;    /* 指向编译后的 SQL 语句的指针 */
        int ret = 0;
        ret = sqlite3_open("./vbgk.db", &pDB);
        if(ret == SQLITE_OK)
        {
            printf("成功打开数据库!\n");
            sqlite3_close(pDB);
            pDb = NULL;
        }
        else
        {
            printf("打开数据库失败!\n");
        }

        return 0;
然后插入数据,查询数据是怎么调用的



void menuMemorandum()//备忘录
{
    //FILE *fp;
    char ch;
loop:system("cls"); //调用DOS清屏命令
    printf("                            ☆☆☆万年历查询☆☆☆\n");
    printf("********************************************************************************\n");
    printf("                                    备忘录 \n");
    printf("            =============================================================\n");
    printf("            =      ******************************************           =\n");
    printf("            =      **************1.显示所有信息**************           =\n");
    printf("            =      **************2.添加备忘录****************           =\n");
    printf("            =      **************3.查询备忘录****************           =\n");
    printf("            =      **************4.修改备忘录****************           =\n");
    printf("            =      **************5.删除备忘录****************           =\n");
    printf("            =      **************6.返回主菜单****************           =\n");
    printf("            =      ******************************************           =\n");
    printf("            =============================================================\n");
    printf("\n");
   
    ch=getch();
    switch(ch)
    {
        case A:display();
            goto loop;
        case B:insert();
            goto loop;
        case C:seek();
            goto loop;
        case D:modify();
            goto loop;
        case E:dele();
        case F:
            system("cls");
            printf("\n\n\n\n");
            printf("\t\t\t欢迎您再次使用备忘录任务提醒系统!\n\n\n");
            printf("\n");
            printf("\t\t\t   返回主菜单中   请稍等...\n\n\n");
            k=0;
            buffer(2);
            break;
        default:
            goto loop;
    }
}

void display()//显示信息函数
{
    /*Beep(550,120);//声音函数*/
    FILE *fp;
    int i=0;
    if((fp=fopen("备忘录.dat","r"))==NULL)
    {
        printf("\t\t备忘录不存在!\n\n");
        getchar();
        printf("\t请您按回车键返回主菜单!\n");
        getchar();
        system("cls");
        exit(1);
    }
    while(!feof(fp))
    {
        fread(&memo[i],sizeof(struct memorandum),1,fp);
        i++;
    }
    fclose(fp);
    n=i-1;
    if(n==0)
    {
        printf("\t\t备忘录是空的!\n");
        getchar();
        printf("\t请您按回车键返回主菜单!\n");
        getchar();
        system("cls");
        return;
    }
    printf("\t\t\t备忘录的全部信息:\n\n");
    printf("\t\t年\t月\t日\t序号\t内容     \n\n");
    for(i=0;i<n;i++)
        print(i);
    getchar();
   
    printf("\t此备忘录共有信息为:%d个\n\n",n);
    printf("\t请按回车键返回主菜单!\n");
    getchar();
    system("cls");
}
void insert()//添加信息函数
{
    FILE *fp;
    int i=0,j,flag=1,flag1=0;
    char c;
    if((fp=fopen("备忘录.dat","r"))==NULL)
    {
        printf("\t对不起!!!您还未创建任何文件!\n\n");
        printf("\t您确定要新建一个名为‘备忘录.dat’的二进制文件吗?\n\n");
        printf("\t        Y----创建  N----不创建");
        getchar();
        scanf("%c",&c);
        if(c=='Y'||c=='y')
        {
            if((fp=fopen("备忘录.dat","wb+"))==NULL)
            {
                printf("\t\t打开文件错误!\n");
                exit(0);
            }
            printf("\t\t恭喜您,创建文件成功!\n\n");
        }
        else
        {
            getchar();
            printf("\t请您按回车键返回备忘录菜单!\n\n");
            getchar();
            system("cls");
            return;
        }
    }
    while(feof(fp)==0)
    {
        fread(&memo[i],sizeof(struct memorandum),1,fp);
        i++;
    }
    fclose(fp);
    n=i-1;
    i=n;
    while(1)
    {
        printf("\t现在文件中的信息数目为:%d\n\n",n);
        flag=1;
        while(flag==1)
        {
            flag=0;
            printf("\t请您输入第 %d 个信息的序号(退出请输入Q): ",i+1);
            scanf("%2s",memo[i].num);
            if(memo[i].num[0]=='Q'||memo[i].num[0]=='q')
            {
                if ((fp=fopen("备忘录.dat","wb"))==NULL)
                {
                    printf ("\t\t\t文件不能打开!\n ");
                    exit(0);
                }
                for(i=0;i<n;i++)
                    if(fwrite(&memo[i],sizeof(struct memorandum),1,fp)!=1)
                        printf("\t\t\t文件保存错误!\n\n");
                fclose(fp);
                if(flag1==0){
                    printf("\t您已经退出备忘录信息添加菜单!\n\n");
                    printf("\t现在文件中的信息数目为:%d\n\n",n);
                }
                else{
                    printf("\t\t\t恭喜您,数据保存成功!\n\n");
                    printf("\t现在文件中的信息数目为:%d\n\n",n);
                }
                getchar();
                printf("\t请您按回车键返回主菜单!\n");
                getchar();
                system("cls");
                return;
            }
            for(j=0;j<i;j++)
                if(strcmp(memo[i].num,memo[j].num)==0)
                {
                    printf("\t该序号已存在,请您检查后重新输入!\n\n");
                    flag=1;
                    break;
                }
        }
        printf("\t请您输入第 %d 个信息的年份: ",i+1);
        scanf("%s",memo[i].year);
        
        printf("\t请您输入第 %d 个信息的月份: ",i+1);
        scanf("%s",memo[i].month);
        
        printf("\t请您输入第 %d 个信息的日期: ",i+1);
        scanf("%s",memo[i].day);
        
        printf("\t请您输入第 %d 个信息的内容:",i+1);
        scanf("%s",memo[i].messege);
        
        i++;
        n=i;
        flag1=1;
    }
}
void seek()//查询信息函数
{
    FILE *fp;
    int i=0,flag;
    char ch;
    char s1[10],s2[10],s3[10];
    if((fp=fopen("备忘录.dat","r"))==NULL)
    {
        printf("\t\t备忘录不存在!\n\n");
        getchar();
        printf("\t\t请您按回车键返回主菜单!\n");
        getchar();
        system("cls");
        return;
    }
    while(!feof(fp))
    {
        fread(&memo[i],sizeof(struct memorandum),1,fp);
        i++;
    }
    fclose(fp);
    n=i-1;
    if(n==0){
        printf("\t\t备忘录是空的!\n");
        getchar();
        printf("\t\t请您按回车键返回主菜单!\n");
        getchar();
        system("cls");
        return;
    }
    system("cls");
    printf("                            ☆☆☆万年历查询☆☆☆\n");
    printf("********************************************************************************\n");
    printf("                                   查询菜单\n");
    printf("            =============================================================\n");
    printf("            =      ********************************************         =\n");
    printf("            =      ***************1.按日期查询*****************         =\n");
    printf("            =      ***************2.按序号查询*****************         =\n");
    printf("            =      ***************3.返回主功能菜单*************         =\n");
    printf("            =============================================================\n");
    printf("\t现在文件中的信息数目为:%d\n\n",n);
    while(1){
        printf("\t请您选择查询菜单编号: ");
        scanf("%s",&ch);
        flag=0;
        switch(ch)
        {
            case A:
                printf("\n\t请您输入要查询的信息的年份: ");
                scanf("%s",s1);
                printf("\n\t请您输入要查询的信息的月份: ");
                scanf("%s",s2);
                printf("\n\t请您输入要查询的信息的日期: ");
                scanf("%s",s3);
                for(i=0;i<n;i++)
                    if(strcmp(memo[i].year,s1)==0&&strcmp(memo[i].month,s2)==0&&strcmp(memo[i].day,s3)==0)
                    {
                        flag=1;
                        printf("\t\t年份\t月份\t日期\t序号\t内容\n\n");
                        print(i);
                        getchar();
                        printf("\n");
                        break;
                    }
                if(flag==0)
                    printf("\t您输入的日期不存在,请您重新选择输入!\n\n");
                break;
            case B:
                printf("\n\t请输入要查询的信息的序号: ");
                scanf("%s",s1);
                for(i=0;i<n;i++)
                    if(strcmp(memo[i].num,s1)==0)
                    {
                        flag=1;
                        printf("\t\t年份\t月份\t日期\t序号\t内容\n\n");
                        print(i);
                        getchar();
                        printf("\n");
                        break;
                    }
                if(flag==0)
                    printf("\t您输入的序号不存在,请重新选择输入!\n\n"); break;
            case C:
                getchar();
                printf("\t已经退出备忘录信息查询功能菜单!\n\n");
                printf("\t按回车键返回主菜单!\n");
                getchar();
                system("cls");
                return;
            default:printf("\n                   error!!!      please input again:  ");
        }
    }
}

void modify()//修改信息函数
{
    int i=0,n,flag=0;
    char ch;
    char s1[20];
    FILE *fp;
    if((fp=fopen("备忘录.dat","r"))==NULL)
    {
        printf("\t备忘录不存在!\n\n");
        getchar();
        printf("\t按回车键返回主菜单!\n");
        getchar();
        system("cls");
        return;
    }
    while(!feof(fp))
    {
        fread(&memo[i],sizeof(struct memorandum),1,fp);
        i++;
    }
    fclose(fp);
    n=i-1;
    if(n==0)
    {
        printf("\t备忘录是空的!\n\n");
        getchar();
        printf("\t按回车键返回主菜单!\n");
        getchar();
        system("cls");
        return;
    }
    while(flag==0)
    {
        printf("\t现在文件中的信息数目为:%d\n\n",n);
        printf("\t请输入您要修改的信息的序号(退出请输入Q): ");
        scanf("%s",s1);
        if(s1[0]=='Q'||s1[0]=='q'){
            printf("\t已经退出备忘录信息修改功能菜单!\n\n");
            getchar();
            printf("\t按回车键返回主菜单!\n");
            getchar();
            system("cls");
            return;
        }
        for(i=0;i<n;i++)
            if(strcmp(memo[i].num,s1)==0)
            {
                flag=1;
                printf("\t\t年份\t月份\t日期\t序号\t内容\n\n");
                print(i);
                getchar();
                n=i;
                printf("                        按任意键进入修改菜单");
            }
        if(flag==0)
        {
            printf("\t您输入的序号不存在,请您重新输入!\n\n");
        }
    }
    getchar();
loop:
    system("cls");
    printf("                            ☆☆☆万年历查询☆☆☆\n");
    printf("********************************************************************************\n");
    printf("                                    修改菜单\n");
    printf("            =============================================================\n");
    printf("            =      ******************************************           =\n");
    printf("            =      ****************1.修改年份****************           =\n");
    printf("            =      ****************2.修改月份****************           =\n");
    printf("            =      ****************3.修改日期****************           =\n");
    printf("            =      ****************4.修改序号****************           =\n");
    printf("            =      ****************5.修改内容****************           =\n");
    printf("            =      ****************6.返回上一层**************           =\n");
    printf("            =============================================================\n");
    while(1)
    {
        ch=getch();
        switch(ch)
        {
            case A:
                printf("            请输入新的年份: ");
                scanf("%s",s1);
                strcpy(memo[n].year,s1);
                break;
            case B:
                printf("            请输入新的月份: ");
                scanf("%s",memo[n].month,s1);
                strcpy(memo[n].month,s1);
                break;
            case C:
                printf("            请输入新的日期 : ");
                scanf("%s",s1);
                strcpy(memo[n].day,s1);
                break;
            case D:
                printf("            请输入新的序号: ");
                scanf("%2s",s1);
                strcpy(memo[n].num,s1);
                break;
            case E:
                printf("            请输入新的内容: ");
                scanf("%s",s1);
            case F:
                getchar();
                printf("\t已经退出了备忘录信息修改功能菜单!;\n\n");
                printf("\t按回车键返回主菜单!\n");
                getchar();
                system("cls");
                return;
            default:
                goto loop;
        }
        if ((fp=fopen("备忘录.dat","wb"))==NULL)
        {
            printf ("\t\t\t文件不能打开!\n ");
            exit(0);
        }
        for(i=0;i<n;i++)
            if(fwrite(&memo[i],sizeof(struct memorandum),1,fp)!=1)
                printf("\t\t\t文件保存错误!\n\n");
        fclose(fp);
        printf("            恭喜您,修改成功!\n\n");
    }
}
//------------------------------------------------5.删除信息函数--------------------------------------

void dele()//删除信息函数
{
   
    int i=0,j,flag;
    char s1[10];
    FILE *fp;
    if((fp=fopen("备忘录.dat","r"))==NULL)
    {
        printf("\t备忘录不存在!\n\n");
        getchar();
        printf("\t\t\t请您按回车键返回主菜单!\n");
        getchar();
        system("cls");
        return;
    }
    while(!feof(fp))
    {
        fread(&memo[i],sizeof(struct memorandum),1,fp);
        i++;
    }
    fclose(fp);
    n=i-1;
    if(n==0)
    {
        printf("\t备忘录是空的!\n");
        getchar();
        printf("\t\t\t按回车键返回主菜单!\n");
        getchar();
        system("cls");
        return;
    }
    while(1)
    {
        flag=0;
        printf("\t现在文件中的信息数目为:%d\n\n",n);
        printf("\t请输入将要删除的信息的序号(Q--退出): ");
        scanf("%s",s1);
        if(s1[0]=='Q'||s1[0]=='q')
        {
            printf("\t已经退出信息删除功能菜单!\n\n");
            printf("\t现在文件中的信息数目为:%d\n\n",n);
            getchar();
            printf("            按回车键返回主菜单!\n");
            getchar();
            system("cls");
            return;
        }
        for(i=0;i<n;i++)
            if(strcmp(memo[i].num,s1)==0)
            {
                flag=1;
                printf("\t\t年份\t月份\t日期\t序号\t内容\n\n");
                print(i);
                getchar();
                printf("\t是否确定要删除该条信息?Y----确定  N----取消");
                gets(s1);
                if(s1[0]=='N'||s1[0]=='n')
                {
                    printf("\t已经取消了删除该信息!\n\n");
                    getchar();
                    break;
                }
                for(j=i;j<n-1;j++)
                    memo[j]=memo[j+1];
                n--;
                if((fp=fopen("备忘录.dat","wb"))==NULL)
                {
                    printf ("\t\t\t文件不能打开!\n ");
                    exit(0);
                }
                for(i=0;i<n;i++)
                    if(fwrite(&memo[i],sizeof(struct memorandum),1,fp)!=1)
                        printf("\t\t\t文件保存错误!\n\n");
                fclose(fp);
                printf("恭喜您,删除成功!\n\n");
            }
        if(flag==0)
            printf("               error!!!please input again:  \n");
    }
}
2015-10-22 09:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-458422-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058296 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved