11月28作业

使用c语言对sqlite数据库进行操作
func.h

#ifndef __FUN_H__
#define __FUN_H__
#include "headfile.h"
int add(sqlite3 *db);
int change(sqlite3 *db);
int delete (sqlite3 *db);
int find(sqlite3 *db);

#endif

func.c

#include "func.h"
int add(sqlite3 *db)
{
    char *err = NULL;
    char name[128] = "";
    char str[500] = "";
    int id = 1;
    int passwd = 0;
    printf("请输入id号:\n");
    scanf("%d", &id);
    while (getchar() != '\n')
        ;
    printf("请输入用户名:\n");
    fgets(name, sizeof(name), stdin);
    name[strlen(name) - 1] = 0;
    printf("请输入密码:\n");
    scanf("%d", &passwd);
    while (getchar() != '\n')
        ;
    sprintf(str, "insert into key values(%d,'%s',%d)", id, name, passwd);
    if (sqlite3_exec(db, str, NULL, NULL, &err) != SQLITE_OK)
    {
        fprintf(stderr, "errline:%d\nerrmsg:%s\n", __LINE__, err);
        return -1;
    }
    printf("添加成功\n");
}
int change(sqlite3 *db)
{
    //根据id
    int id = 0;
    int flag = 0;
    char order[500] = "";
    char changename[128] = "";
    int changepasswd = 0;
    char *err = NULL;
    printf("请输入你要修改哪个id对应的数据\n"); //应该判断一下是不是有这个id
    scanf("%d", &id);
    while (getchar() != '\n')
        ;
    printf("***********你要修改什么?***********\n");
    printf("****** 1.name 2.passwd 3.all ******\n");
    printf("***********************************\n");
    scanf("%d", &flag);
    while (getchar() != '\n')
        ;
    switch (flag)
    {
    case 1:
        printf("请输入你要修改的名称:\n");
        fgets(changename, sizeof(changename), stdin);
        changename[strlen(changename) - 1] = 0;
        sprintf(order, "update key set usrname='%s' where id='%d'", changename, id);
        if (sqlite3_exec(db, order, NULL, NULL, &err) != SQLITE_OK)
        {
            fprintf(stderr, "errline:%d\nerrmsg:%s\n", __LINE__, err);
            return -1;
        };
        break;
    case 2:
        printf("请输入你要修改的密码:\n");
        scanf("%d", &changepasswd);
        while (getchar() != '\n')
            ;
        sprintf(order, "update key set passwd='%d' where id='%d'", changepasswd, id);
        if (sqlite3_exec(db, order, NULL, NULL, &err) != SQLITE_OK)
        {
            fprintf(stderr, "errline:%d\nerrmsg:%s\n", __LINE__, err);
            return -1;
        };
        break;
    case 3:
        printf("请输入你要修改的名称:\n");
        fgets(changename, sizeof(changename), stdin);
        changename[strlen(changename) - 1] = 0;
        printf("请输入你要修改的密码:\n");
        scanf("%d", &changepasswd);
        while (getchar() != '\n')
            ;
        sprintf(order, "update key set usrname='%s' passwd='%d' where id='%d'", changename, changepasswd, id);
        if (sqlite3_exec(db, order, NULL, NULL, &err) != SQLITE_OK)
        {
            fprintf(stderr, "errline:%d\nerrmsg:%s\n", __LINE__, err);
            return -1;
        };
        break;
    }
}
int delete (sqlite3 *db)
{
    int id = 0;
    char *err = NULL;
    char order[128] = "";
    printf("请输入您要删除的对应的id号\n");
    scanf("%d", &id);
    while (getchar() != '\n');
    sprintf(order, "delete from key where id='%d'", id);
    if (sqlite3_exec(db, order, NULL, NULL, &err) != SQLITE_OK)
    {
        fprintf(stderr, "errline:%d\nerrmsg:%s\n", __LINE__, err);
        return -1;
    }
    printf("删除成功\n");
}
int find(sqlite3 *db)
{
    /*int id = 0;
    char *err = NULL;
    char order[128]="select * from key";
    //printf("请输入要查找的数据对应的id:\n");
    /*scanf("%d",&id);
    while(getchar()!='\n');*/
    //sprintf(order,"select * from key");
    if (sqlite3_exec(db, order, NULL, NULL, &err) != SQLITE_OK)
    {
        fprintf(stderr, "errline:%d\nerrmsg:%s\n", __LINE__, err);
        return -1;
    }*/
}

main.c

#include "func.h"
int main(int argc, const char *argv[])
{
    sqlite3 *db = NULL;
    if (sqlite3_open("./01.db", &db) != SQLITE_OK)
    {
        fprintf(stderr, "errline:%d\nerrmsg:%s\n", __LINE__, sqlite3_errmsg(db));
        return -1;
    }
    //创建一个表格
    //指针指向的产量常量字符串不可以通过指针进行修改
    char sql[128] = "create table if not exists key(id int PRIMARY KEY,usrname char,passwd int);"; //创建表格
    char *err = NULL;                                                                  //储存错误信息
    if (sqlite3_exec(db, sql, NULL, NULL, &err) < 0)                                   //第四个参数是回调函数的参数,由于不需要回调函数所以可以直接填NULL
    {
        fprintf(stderr, "errline:%d\nerrmsg:%s\n", __LINE__, err);
        return -1;
    }
    int flag = -1;
    int k = 1;
    while (k)
    {
        system("clear");
        printf("***********************************\n");
        printf("*****1.增 2.删 3.改 4.查 5.退出*****\n");
        printf("***********************************\n");
        printf("You Choise:");
        scanf("%d", &flag);
        while (getchar() != '\n')
            ;
        switch (flag)
        {
        case 1: //增加
            add(db);
            break;
        case 2: //删除
            delete (db);
            break;
        case 3: //改变
            change(db);
            break;
        case 4: //查找
            find(db);
            break;
        case 5:
            k = 0;
            break; //退出
        default:
            printf("输入有误\n");
            break;
        }
        printf("按任意键继续\n");
        while(getchar()!=10);
    }
    if (sqlite3_close(db) != SQLITE_OK)
    {
        fprintf(stderr, "errline:%d\nerrmsg:%s\n", __LINE__, sqlite3_errmsg(db));
        return -1;
    }
    return 0;
}

你可能感兴趣的:(c语言,sqlite)