day5_C语言调用sqlite3

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>

int insert_sql(sqlite3 *db);
int callback(void*para,int f_num,char**f_value,char**f_name);

int main(int argc, char *argv[])
{
    if(2 > argc){
        printf("Usage: %s <sqlite_name>\n", argv[0]);  //判断输入新建数据库的名字
        return -1;
    }

// SQLITE_API int sqlite3_open(
// 		const char *filename,   /* Database filename (UTF-8) */
// 		sqlite3 **ppDb          /* OUT: SQLite db handle */
// 		);
// 函数说明:该程序会通过指定的文件名参数打开一个数据库文件
// 数据库的连接返回到*ppDb,函数的返回值代表了操作状态,例如成功
// 就返回SQLITE_OK,否则返回错误代码和意外情况下不能分配内存来的得到
// sqlite3就返回NULL

    sqlite3 *db;
    if(sqlite3_open(argv[1], &db) != SQLITE_OK){    //新建数据库
        perror("sqlite3_open");
        return -1;
    }
    char *err;
    char *sql_create = "create table if not exists stu(id int, name char, sex char, score float)";
    if(sqlite3_exec(db, sql_create, NULL, NULL, &err) != SQLITE_OK){   
        printf("errmsg:%s\n", err);
        return -1;
    }
    
    insert_sql(db); //用户输入并创建一条记录


// SQLITE_API int sqlite3_exec(
// sqlite3*,                                  /* An open database */
// const char *sql,                           /* SQL to be evaluated */
// int (*callback)(void*,int,char**,char**),  /* Callback function */
// void *,                                    /* 1st argument to callback */
// char **errmsg                              /* Error msg written here */
// );
// 函数说明:该函数为具体执行SQL语句的函数
// 参数1:打开的数据库连接对象
// 参数2:传入的SQL语句,以‘/0’结尾
// 参数3:是回调函数,如果传入参数不为NULL,则得到一个结果行就会调用该回调函数
// 参数4:自定义的一个指针,目的是传入到回调函数参数以供使用。
// 参数5:错误信息,如果语句执行失败,就可以查阅该指针,这里的参数是输出特性
// (即主调函数将类型变量的地址传入,函数分配内存赋值给该变量)
// 同时在执行完该函数后又不需要错误信息时,为了避免内存泄露,应该调用
// sqlite3_free()来释放这块内存


    
    if(sqlite3_exec(db, "select * from stu", callback, "男", NULL) != SQLITE_OK){  //查询函数
        perror("sqlite3_exec");
        return -1;
    }


// SQLITE_API int sqlite3_close(sqlite3*);
// 函数说明:前面如果用 sqlite3_open 开启了一个数据库,结尾时不要忘了用这个函数关闭数据库
// 同样返回值为操作状态
    

    sqlite3_close(db);
}
int insert_sql(sqlite3 *db)
{
    char sql[100] = {0};
    int id;
    char name[20], sex[10];
    float s;
    while(1){
        printf("please input id name sex score: \n");
        scanf("%d %s %s %f", &id, name, sex, &s);
        if(-1 == id){
            break;
        }
        sprintf(sql, "insert into stu values(%d,'%s', '%s', %0.2f)", id, name, sex, s);  //将字符串写入sql
        if(sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK){   //执行sql语句
            perror("sqlite3_exec");
            return -1;
        }
    }
    
}
int callback(void*para,int f_num,char**f_value,char**f_name)
{
    static int flag = 0;
    int i;
    if(flag == 0){
        printf("select: %s\n", (char *)para);
        for(i = 0; i < f_num; i++){
            printf("%-15s", f_name[i]);
        }
        puts("");
        flag = 1;
    }
    
    for(i = 0; i < f_num; i++){
        if(0 == strncasecmp((char *)para, f_value[2], 3)){
            printf("%-15s", f_value[i]);
        }
    }
    puts("");
    return 0;
}
posted @ 2020-09-04 20:12  盐亭的森林  阅读(141)  评论(0编辑  收藏  举报