sqlite嵌入式数据库C语言基本操作(1)


sqlite嵌入式数据库C语言基本操作(1)

sqlite是应用最广泛的嵌入式数据库,没有之一,其详细的介绍参见官方网站(http://sqlite.org).最新的版本是3.12.2.
C语言的API函数列表见官网SQLite C Interface.这里对一些基本的函数做一个简单的说明。
sqlite3_open: 打开数据库文件.
sqlite3_close: 关闭数据库。
sqlite3_prepare : 编译SQL,准备语句。
sqlite3_step: sqlite3_stmt的方式执行SQL语句。
sqlite3_exec: 执行SQL语句。
sqlite3_finalize: 释放 sqlite3_stmt资源。
sqlite3_bind_int: 绑定SQL语句中int参数。
sqlite3_bind_text: 绑定SQL语句中的字符串参数。
sqlite3_bind_blob: 绑定SQL语句中的二进制参数。
sqlite3_column_int: 遍历数据集,得到一行数据中某列的int值。
sqlite3_column_text: 遍历数据集,得到一行数据某列的字符串值。
sqlite3_column_blob: 遍历数据集,得到一行数据某列的二进制值。
……

以下这个例子做一个简单的insert,和 select 的例子,来讲sqlite3数据库的简单数据操作。
首先建一张简单的表,并插入初始化数据。

create table userinfo ( language-php
 userid integer,
 username char(32),
 PRIMARY KEY (userid)
 );
 insert into userinfo (userid,username) values (1,"Alex");
 insert into userinfo (userid,username) values (2,"Allan");
 insert into userinfo (userid,username) values (3,"Blizzard");
 insert into userinfo (userid,username) values (4,"Bob");
"> create table userinfo (
 userid integer,
 username char(32),
 PRIMARY KEY (userid)
 );
 insert into userinfo (userid,username) values (1,"Alex");
 insert into userinfo (userid,username) values (2,"Allan");
 insert into userinfo (userid,username) values (3,"Blizzard");
 insert into userinfo (userid,username) values (4,"Bob");

建立对应的数据模型.

typedef struct userinfo_s { language-php
    int userid;     //用户编号
    char username[33];        //用户姓名
    struct userinfo_s * next; //下一个用户
} userinfo_t;
">typedef struct userinfo_s {
    int userid;     //用户编号
    char username[33];        //用户姓名
    struct userinfo_s * next; //下一个用户
} userinfo_t;

得到所有Users的函数

int get_all_userinfos(userinfo_t u){ language-php
     char sql[512] = "select * from userinfo;"; //sql语句
    sqlite3 * db = NULL;
    sqlite3_stmt * stmt = NULL;
    int ret_open = sqlite3_open("test.db",&db); //打开数据库文件
    if (ret_open != SQLITE_OK){
        printf("open test.db fail\n");
        return -1;
    }
     int ret = sqlite3_prepare(db,sql,-1,&stmt,NULL); //准备好SQL语句
        if (ret != SQLITE_OK){
                printf("prepare fail \n");
                return ret;
        }
        ret = sqlite3_step(stmt); //执行SQL
        int count = 0;
        if(ret != SQLITE_ROW){        //返回有数据
                return 0;
        }else{
                userinfo_t * p = u; //建立链表
                do{
                        userinfo_t * user = calloc(sizeof(userinfo_t),1);
                        user->userid = sqlite3_column_int(stmt,0); //得到USerid   注意get的初始值为index为0
                        const char * name = sqlite3_column_text(stmt,1); //得到用户名
                        if (name){
                                int len = strlen(name);
                                strncpy(user->username,name,len);
                        }
                        user->next = NULL;
                        p->next = user; //串联 建立一个单项链表
                        p = p->next;
                        count ++;
                }while((ret = sqlite3_step(stmt))==SQLITE_ROW); //取值下一个
        }
        sqlite3_finalize(stmt); //释放资源 stmt
        sqlite3_close(db); //关闭数据库句柄
        return count;
 }
"> int get_all_userinfos(userinfo_t u){
     char sql[512] = "select * from userinfo;"; //sql语句
    sqlite3 * db = NULL;
    sqlite3_stmt * stmt = NULL;
    int ret_open = sqlite3_open("test.db",&db); //打开数据库文件
    if (ret_open != SQLITE_OK){
        printf("open test.db fail\n");
        return -1;
    }
     int ret = sqlite3_prepare(db,sql,-1,&stmt,NULL); //准备好SQL语句
        if (ret != SQLITE_OK){
                printf("prepare fail \n");
                return ret;
        }
        ret = sqlite3_step(stmt); //执行SQL
        int count = 0;
        if(ret != SQLITE_ROW){        //返回有数据
                return 0;
        }else{
                userinfo_t * p = u; //建立链表
                do{
                        userinfo_t * user = calloc(sizeof(userinfo_t),1);
                        user->userid = sqlite3_column_int(stmt,0); //得到USerid   注意get的初始值为index为0
                        const char * name = sqlite3_column_text(stmt,1); //得到用户名
                        if (name){
                                int len = strlen(name);
                                strncpy(user->username,name,len);
                        }
                        user->next = NULL;
                        p->next = user; //串联 建立一个单项链表
                        p = p->next;
                        count ++;
                }while((ret = sqlite3_step(stmt))==SQLITE_ROW); //取值下一个
        }
        sqlite3_finalize(stmt); //释放资源 stmt
        sqlite3_close(db); //关闭数据库句柄
        return count;
 }

insert操作函数:

int insert_userinfo_t(userinfo_t * u){ language-php
        char sql[512] = "insert into userinfo (userid,username) values (?,?)"; //插入数据库语句
        sqlite3 *db = NULL;
        sqlite3_stmt * stmt = NULL;
        int ret_open = sqlite3_open("test.db",&db); //打开数据库
        if(ret_open !=SQLITE_OK){
                printf("open test.db fail\n");
                return -1;
        }
        int ret = sqlite3_prepare(db,sql,-1,&stmt,NULL); //准备语句
        if (ret != SQLITE_OK){
                printf("prepare fail \n");
                return ret;
        }
        sqlite3_bind_int(stmt,1,u->userid); //绑定参数, 注意绑定参数的初始index值为1
        sqlite3_bind_text(stmt,2,u->username,32,NULL);
        ret = sqlite3_step(stmt); //执行语句
        if(ret == SQLITE_DONE){   //执行结果
                ret = SQLITE_OK;
        }
        sqlite3_finalize(stmt); //释放资源
        sqlite3_close(db); //关闭数据库
        return ret;
}
">int insert_userinfo_t(userinfo_t * u){
        char sql[512] = "insert into userinfo (userid,username) values (?,?)"; //插入数据库语句
        sqlite3 *db = NULL;
        sqlite3_stmt * stmt = NULL;
        int ret_open = sqlite3_open("test.db",&db); //打开数据库
        if(ret_open !=SQLITE_OK){
                printf("open test.db fail\n");
                return -1;
        }
        int ret = sqlite3_prepare(db,sql,-1,&stmt,NULL); //准备语句
        if (ret != SQLITE_OK){
                printf("prepare fail \n");
                return ret;
        }
        sqlite3_bind_int(stmt,1,u->userid); //绑定参数, 注意绑定参数的初始index值为1
        sqlite3_bind_text(stmt,2,u->username,32,NULL);
        ret = sqlite3_step(stmt); //执行语句
        if(ret == SQLITE_DONE){   //执行结果
                ret = SQLITE_OK;
        }
        sqlite3_finalize(stmt); //释放资源
        sqlite3_close(db); //关闭数据库
        return ret;
}

测试代码:

language-php
void test_get_all_userinfos(){
        userinfo_t u;
        int count = get_all_userinfos(&u);
        userinfo_t * p = u.next;
        userinfo_t * q = p;
        printf("count:%d\n",count);
        while(p){
                printf("userid:%d username:%s \n",p->userid,p->username);
                p = p->next;
        }
        free_userinfo_t(&u);
}

void test_insert_into_userinfo_t(){
        userinfo_t new_user;
        new_user.userid = 5;
        char new_name[33]= "micheal";
        strncpy(new_user.username,new_name,32);
        int ret= insert_userinfo_t(&new_user);
        printf("ret = %d\n",ret);
}
">
void test_get_all_userinfos(){
        userinfo_t u;
        int count = get_all_userinfos(&u);
        userinfo_t * p = u.next;
        userinfo_t * q = p;
        printf("count:%d\n",count);
        while(p){
                printf("userid:%d username:%s \n",p->userid,p->username);
                p = p->next;
        }
        free_userinfo_t(&u);
}

void test_insert_into_userinfo_t(){
        userinfo_t new_user;
        new_user.userid = 5;
        char new_name[33]= "micheal";
        strncpy(new_user.username,new_name,32);
        int ret= insert_userinfo_t(&new_user);
        printf("ret = %d\n",ret);
}

输出

count:4
userid:1 username:Alex
userid:2 username:Allan
userid:3 username:Blizzard
userid:4 username:Bob
ret = 1
count:5
userid:1 username:Alex
userid:2 username:Allan
userid:3 username:Blizzard
userid:4 username:Bob
userid:5 username:micheal

gcc编译最后记得加上 -lsqlite3
本文只是很初略的介绍了sqlite嵌入式数据库的基本操作,初略到甚至例程只讲了两个操作,增加和查询,修改和删除都没有讲到,不过看了增加操作,修改和删除操作就很容易实现了。
写这篇文章目的只是一个想讲一个过程,一个操作数据库的基本过程

一个数据库操作流程

1 打开数据库
2 编译准备SQL语句
3 绑定SQL语句参数
4 执行SQL语句
5 关闭释放Statement
6 关闭数据库

当然也只是一个初略的流程,对于数据库操作失败的流程处理也没有。不过了解上面这个初略的流程就可以了。更深入的讲解请参见下篇:[sqlite嵌入式数据库C语言基本操作(2)]

参考文献:
sqlite官方网站:sqlite.org

generated by haroopad