sqlite3数据库简单使用demo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_33232390/article/details/68943575

demo:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "evsql/sqlite3.h"


struct test_t{
    char * username;
    int  password;
};

int print_record(void *params,int n_column,char **column_value,char **column_name)
{
    int i;
        /*传入的参数即为回调函数第一个参数params*/
        struct test_t *test = params;
        /*打印出搜索到的数据,数据均以字符串形式存入column_value中*/
    for(i=0;i<n_column;i++){
        printf("\t%s",column_value[i]);
    }
    printf("\n");
        /*搜索到的数据个数存入n_column中*/
        if(n_column )
        {
                int password = atoi(column_value[1]);
                /*传入数据与表中搜索到数据相同,代表成功*/
                if(strcmp(column_value[0],test->username) == 0 && password == test->password)
                    printf("success.....\n");
        }
    return 0;
}

int main()
{
 const char *sql_create_table="create table t(user_id int primary key,username varchar(128) ,time int)";
 char *errmsg = 0;
 int ret = 0;

 struct test_t test;
 test.username = "sky";
 test.password = 152100;

 sqlite3 *db = 0;
 /*读写打开一个数据库文件,无此文件打开失败*/
 ret = sqlite3_open_v2("./sky.db",&db,SQLITE_OPEN_READWRITE,NULL);
 if(ret != SQLITE_OK){
         /*无此文件,创建一个文件*/
         ret = sqlite3_open("./sky.db",&db);
         if(ret != SQLITE_OK){
                 fprintf(stderr,"Cannot open db: %s\n",sqlite3_errmsg(db));
         }
         /*创建一个表*/
         ret = sqlite3_exec(db,sql_create_table,NULL,NULL,&errmsg);
         if(ret != SQLITE_OK){
                 fprintf(stderr,"create table fail: %s\n",errmsg);
         }
 }
 printf("Open database\n");

 char sql[1024];
 char *user_name = "sky";
 sprintf(sql,"select username,time from t where user_id='%d';",2);

/*在表中插入数据*/
 ret = sqlite3_exec(db,"insert into t(user_id,username,time) values(1,'bboy',151900);insert into t(user_id,username,time)
 \ values(2,'sky',152100)",NULL,NULL,&errmsg);
     printf("Insert a record %s\n",ret == SQLITE_OK ? "OK":"FAIL");
/*更新表中数据*/
     ret = sqlite3_exec(db,"update t set username='born' where user_id=1",NULL,NULL,&errmsg);
            printf("Update a record %s\n",ret == SQLITE_OK ? "OK":"FAIL");
    /*查找表中数据,调用回调函数print_record,将test结构体传入回调函数*/
    ret = sqlite3_exec(db,sql,print_record,&test,&errmsg);
    if(ret != SQLITE_OK){
            fprintf(stderr,"query SQL error: %s\n",errmsg);
    }

 sqlite3_free(errmsg);
 sqlite3_close(db);

 printf("Close database\n");

 return 0;
}

CMakeLists.txt文件(源程序文件目录):
cmake_minimum_required(VERSION 2.8)

project(sqlite_test)
set(CMAKE_BUILD_TYPE “Debug”)
aux_source_directory(. DIR_SRCS)
add_definitions(-DGOGENIUS_DEBUG)

add_subdirectory(evsql)

add_executable(sqlite_test ${DIR_SRCS})

target_link_libraries(sqlite_test evsqlfunc)
target_link_libraries(sqlite_test pthread)
target_link_libraries(sqlite_test dl)

INSTALL(TARGETS sqlite_test DESTINATION bin)

CMakeLists.txt文件(源程序文件目录下的sqlite3文件目录中):
aux_source_directory(. DIR_LIB_SRCS)
add_library(evsqlfunc ${DIR_LIB_SRCS})

展开阅读全文

没有更多推荐了,返回首页