zoukankan      html  css  js  c++  java
  • [转载]c操作sqlite3基本方法

    [ 2010-05-09 20:08:47 | 作者: yuhen ]
    相关 API 信息可参考官方 帮助手册

    测试代码:
    #include <sqlite3.h>
    
    void exec(sqlite3* db, const char* sql)
    {
        sqlite3_stmt* stmt;
    
        if (sqlite3_prepare(db, sql, strlen(sql), &stmt, NULL) == SQLITE_OK)
        {
            sqlite3_step(stmt);
        }
        else
        {
            printf("%s\n", sqlite3_errmsg(db));
        }
    
        sqlite3_finalize(stmt);
    }
    
    void exec2(sqlite3* db, const char* sql)
    {
        sqlite3_stmt* stmt;
    
        if (sqlite3_prepare(db, sql, strlen(sql), &stmt, NULL) == SQLITE_OK)
        {
            while (sqlite3_step(stmt) == SQLITE_ROW)
            {
                int id = sqlite3_column_int(stmt, 0);
                unsigned char* name = sqlite3_column_text(stmt, 1);
    
                printf("%d, %s\n", id, name);
            }
        }
        else
        {
            printf("%s\n", sqlite3_errmsg(db));
        }
    
        sqlite3_finalize(stmt);
    }
    
    int main(int argc, char* argv[])
    {
        sqlite3* db;
    
        if (sqlite3_open("./test.db", &db) == SQLITE_OK)
        {
            exec(db, "CREATE TABLE [user]([id] INTEGER PRIMARY KEY ASC AUTOINCREMENT, [name])");
            exec(db, "INSERT INTO [user] ([name]) VALUES ('user1')");    
            exec(db, "INSERT INTO [user] ([name]) VALUES ('user2')");    
            exec(db, "INSERT INTO [user] ([name]) VALUES ('user3')");    
            exec2(db, "SELECT * FROM [user]");    
    
            sqlite3_close(db);
        }
        else
        {
            printf("%s\n", sqlite3_errmsg(db));
        }
    
        return EXIT_SUCCESS;
    }
  • 相关阅读:
    2018_10_15 堆栈内存
    2018_10_14 java 环境 win10
    2018_09_21 ftp 安装
    2018_09_21 myeclipse git插件
    转 iOS开发中一些常见的并行处理
    转【iOS设计】一款APP从设计稿到切图过程概述
    算法题
    iOS -copy-mutableCopy- NSHashTable -NSMapTable-Keychain
    转--iOS下JS与原生OC互相调用
    ReactiveCocoa
  • 原文地址:https://www.cnblogs.com/fx2008/p/2212507.html
Copyright © 2011-2022 走看看