加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite3学习

发布时间:2020-12-12 23:44:51 所属栏目:百科 来源:网络整理
导读:参考: http://www.blogjava.net/xylz/archive/2012/09/25/388519.html 官网:http://www.sqlite.org/cintro.html http://www.cnblogs.com/stephen-liu74/archive/2012/03/05/2340780.html http://www.cnblogs.com/nbsofer/archive/2012/05/02/2479168.html

参考: http://www.blogjava.net/xylz/archive/2012/09/25/388519.html

官网:http://www.sqlite.org/cintro.html

http://www.cnblogs.com/stephen-liu74/archive/2012/03/05/2340780.html

http://www.cnblogs.com/nbsofer/archive/2012/05/02/2479168.html

http://www.cnblogs.com/memset/archive/2012/05/02/2479156.html

例子程序:

#include <stdio.h>
#include <assert.h>
#include <sqlite3.h>

int main()
{
    int ret;
    sqlite3* pDb = NULL;
    char* pErrMsg = NULL;
    printf("Open dbn");
    ret = sqlite3_open("./my.db",&pDb);//Open the db file
    assert(SQLITE_OK == ret);

    printf("Clear datan");
    sqlite3_exec(pDb,"drop table if exists myTablen",NULL,NULL);
    
    printf("Create tablen");
    ret = sqlite3_exec(pDb,"create table if not exists myTable(id integer primary key,name text)",&pErrMsg);//Create a table
    if (SQLITE_OK != ret) printf("%sn",pErrMsg);
    assert(SQLITE_OK == ret);
    sqlite3_free(pErrMsg);//TODO:ê?·?òa??′?sqlite3_exec??free??2?ê???freeò?′??
    
    printf("Insert some linesn");
    ret = sqlite3_exec(pDb,"insert into myTable(id,name) values(2,'2222222')",&pErrMsg);
    ret = sqlite3_exec(pDb,name) values(3,'3333333')",name) values(11,'22222')",&pErrMsg);
    //if (SQLITE_OK != ret) printf("%sn",pErrMsg);
    assert(SQLITE_OK == ret);
    
    printf("Insert a line that contained ' n");
    char* sql = sqlite3_mprintf("insert into myTable(id,name) values(14,'%q')","luo'jiao");
    printf("sql: %sn",sql);
    ret = sqlite3_exec(pDb,sql,NULL);
    assert(SQLITE_OK == ret);
    sqlite3_free(sql);
    
    printf("Delete some linesn");
    //ret = sqlite3_exec(pDb,"delete from myTable where id < 10",&pErrMsg);

    printf("Use prepare&step to insert a linen");
    sqlite3_stmt *stmt;
    ret = sqlite3_prepare_v2(pDb,name) values(?,?)",-1,&stmt,NULL);
    assert(SQLITE_OK == ret);
    for (int i = 30; i < 33; i++)
    {
        sqlite3_bind_int(stmt,1,i);
        char text[50] = {0};
        sprintf(text,"prepare line id%d",i);
        sqlite3_bind_text(stmt,2,text,NULL);
        sqlite3_step(stmt);
        sqlite3_reset(stmt);
    }
    sqlite3_finalize(stmt);
    assert(SQLITE_OK == ret);

    printf("Select some data from table,result:n");
    sqlite3_stmt *stmt1;
    const char* sql1 = "select * from myTable where id > 10";
    printf("Sql: select * from myTable where id > 10nid  namen");
    ret = sqlite3_prepare_v2(pDb,sql1,&stmt1,NULL);
    assert(SQLITE_OK == ret);
    while (SQLITE_ROW == sqlite3_step(stmt1))
    {
        int id = sqlite3_column_int(stmt1,0);
        const unsigned char* pName = sqlite3_column_text(stmt1,1);
        printf("%-4d%sn",id,pName);
    }
    ret = sqlite3_finalize(stmt1);
    assert(SQLITE_OK == ret);

    sqlite3_close(pDb);
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读