zoukankan      html  css  js  c++  java
  • SQLite使用(二)

    sqlite3_exec虽然好用,但是一般不推荐直接使用。

    常用的一组操作是:

    关于sqlite3_execsqlite3_prepare_v2的使用场景,建议如下:

    一个小DEMO:

    #include <stdio.h>
    #include <sqlite3.h>
    
    int main(int argc, char **argv)
    {
        sqlite3 *db;
        int rc;
        
        rc = sqlite3_open("test.db", &db);
        
        if (rc) {
            printf("Can't open database: %s
    ", sqlite3_errmsg(db));
            sqlite3_close(db);
            return 1;
        } else {
            printf("Open database successfully
    ");
        }
        
        sqlite3_stmt *stmt = NULL;
        
        const char *sql = "SELECT * FROM COMPANY";
    //    const char *sql = "SELECT * FROM COMPANY WHERE AGE=?;";
        
        sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
        
    //    sqlite3_bind_int(stmt, 1, 25);
    
        int col_count = sqlite3_column_count(stmt);
        printf("该条记录共%d列
    ", col_count);
        
        while (SQLITE_ROW == sqlite3_step(stmt)) {
            int column0_type = sqlite3_column_type(stmt, 0);
            const char *column0_name = sqlite3_column_name(stmt, 0);
            int column0_value = sqlite3_column_int(stmt, 0);
            printf("col: 0 type: %d name: %-10s value: %d
    ", column0_type, column0_name, column0_value);
            
            int column1_type = sqlite3_column_type(stmt, 1);
            const char *column1_name = sqlite3_column_name(stmt, 1);
            const unsigned char *column1_value = sqlite3_column_text(stmt, 1);
            printf("col: 1 type: %d name: %-10s value: %s
    ", column1_type, column1_name, column1_value);
            
            int column2_type = sqlite3_column_type(stmt, 2);
            const char *column2_name = sqlite3_column_name(stmt, 2);
            int column2_value = sqlite3_column_int(stmt, 2);
            printf("col: 2 type: %d name: %-10s value: %d
    ", column2_type, column2_name, column2_value);
            
            int column3_type = sqlite3_column_type(stmt, 3);
            const char *column3_name = sqlite3_column_name(stmt, 3);
            const unsigned char *column3_value = sqlite3_column_text(stmt, 3);
            printf("col: 3 type: %d name: %-10s value: %s
    ", column3_type, column3_name, column3_value);
            
            int column4_type = sqlite3_column_type(stmt, 4);
            const char *column4_name = sqlite3_column_name(stmt, 4);
            double column4_value = sqlite3_column_double(stmt, 4);
            printf("col: 4 type: %d name: %-10s value: %.2f
    ", column4_type, column4_name, column4_value);
            
            printf("
    ");
        }
        sqlite3_finalize(stmt);
        
    //    char *zErrMsg = 0;
    //    sqlite3_stmt *stmt_insert;
    //    const char *sql_insert = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  
    //    "VALUES (5, 'Logan', 26, 'California', 20000.00 );";
    //    sqlite3_prepare_v2(db, sql_insert, -1, &stmt_insert, NULL);
    //    if (sqlite3_step(stmt_insert) != SQLITE_DONE) {
    //        printf("Insert Table Failed
    ");
    //        sqlite3_free(zErrMsg);
    //    }
    //    sqlite3_finalize(stmt_insert);
        
        sqlite3_close(db);
        return 0;
    }

    参考资料:

    sqlite3_prepare_v2 / sqlite3_exec

    [转载] Sqlite c/c++ api学习

  • 相关阅读:
    spring 装配核心笔记
    小明种苹果
    线性分类器
    报数
    编程计划2.0 //不断更新
    MySQL基础之存储过程
    MySQL基础之视图
    指令系统.传送类指令
    MySQL基础之索引
    寻址方式总结
  • 原文地址:https://www.cnblogs.com/gattaca/p/6791663.html
Copyright © 2011-2022 走看看