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;
    }
  • 相关阅读:
    HDU 1022 Train Problem I
    HDU 1702 ACboy needs your help again!
    HDU 1294 Rooted Trees Problem
    HDU 1027 Ignatius and the Princess II
    HDU 3398 String
    HDU 1709 The Balance
    HDU 2152 Fruit
    HDU 1398 Square Coins
    HDU 3571 N-dimensional Sphere
    HDU 2451 Simple Addition Expression
  • 原文地址:https://www.cnblogs.com/fx2008/p/2212507.html
Copyright © 2011-2022 走看看