• [Database]Sqlite中常用数据、方法与Error Codes..


    >> Data Structures

    sqlite3*           // A session of database connection
    sqlite3_stmt*  // SQL Statement Object

    >> Supported Data Types of Column

    似乎SQLite2仅仅支持TEXT类型,在SQLite3中大大丰富了。

    TEXT

    NUMERIC

    INTEGER

    REAL

    NONE

    >> Methods:

    Sqlite3有82个函数,但是常用的也就那么几个:

    // LibraryInitialization/Shutdown
    int sqlite3_initialize(void);
    int sqlite3_shutdown(void);

    // Database Modify/Query
    int sqlite3_open(const char*, sqlite3**);
    int sqlite3_close(sqlite3*);

    int sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void*, char**);

    int sqlite3_prepare( sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **ppStmt, const char **pzTail);
    int sqlite3_step(sqlite3_stmt *pStmt);
    int sqlite3_finalize(sqlite3_stmt *pStmt)
    int sqlite3_reset(sqlite3_stmt *pStmt);

    // Binding
    sqlite3_bind_blob(sqlite3_stmt *pStmt, int i, const void *zData, int nData, void (*xDel)(void*));
    int sqlite3_bind_xxx(sqlite3_stmt *pStmt, int i, xxx rValue)
    int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName);
    int sqlite3_clear_bindings(sqlite3_stmt *pStmt);


    // Get data
    int sqlite3_get_table(sqlite3*, const char *sql,char***result, int *nrow,int *ncolumn ,char **errmsg );
    const unsigned char* sqlite3_column_text(sqlite3_stmt *pStmt, int i);
    const void* sqlite3_column_blob(sqlite3_stmt *pStmt, int i);
    sqlite3_column_int(sqlite3_stmt *pStmt, int i);

    // Error Handling

    int sqlite3_busy_handler(sqlite3 *db, int (*xBusy)(void*,int), void *pArg)
    sqlite3_errcode()
    sqlite3_errmsg()

     

    SQLite3好些函数屁股后面带着16或者v2,前者表示为UTF16编码,后者则为该函数的增强版,颇有些Windows API中EX的味道。

    >> Error Codes:

    ----------------------------------------------------------

    #define SQLITE_OK              0   /* Successful result */
    #define SQLITE_ERROR        1   /* SQL error or missing database */
    #define SQLITE_INTERNAL    2   /* Internal logic error in SQLite */
    #define SQLITE_PERM         3   /* Access permission denied */
    #define SQLITE_ABORT        4   /* Callback routine requested an abort */
    #define SQLITE_BUSY         5   /* The database file is locked */
    #define SQLITE_LOCKED       6   /* A table in the database is locked */
    #define SQLITE_NOMEM        7   /* A malloc() failed */
    #define SQLITE_READONLY     8   /* Attempt to write a readonly database */
    #define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
    #define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
    #define SQLITE_CORRUPT     11   /* The database disk image is malformed */
    #define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
    #define SQLITE_FULL        13   /* Insertion failed because database is full */
    #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
    #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
    #define SQLITE_EMPTY       16   /* Database is empty */
    #define SQLITE_SCHEMA      17   /* The database schema changed */
    #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
    #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
    #define SQLITE_MISMATCH    20   /* Data type mismatch */
    #define SQLITE_MISUSE      21   /* Library used incorrectly */
    #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
    #define SQLITE_AUTH        23   /* Authorization denied */
    #define SQLITE_FORMAT      24   /* Auxiliary database format error */
    #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
    #define SQLITE_NOTADB      26   /* File opened that is not a database file */
    #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
    #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */

    -----------------------------------------------------------------

    >> Transaction(事务处理)

     sqlite支持Transaction,为了提高Performance,有必要尽量把同类操作搞成一个Transaction,一次删除1W条数据与分1W次删除数据的性能绝对不是一样的。这种情况下,做成统一事务吧,而且还便于Rollback。

    >> Thread Safe

    SQLite 是线程安全的,但在编译时必须将 SQLITE_THREADSAFE 预处理宏置为1。

    在 3.3.1 版本之前,一个 sqlite3 结构只能被用于调用 sqlite3_open 创建的同一线程。你不能在一个线程中打开数据库, 然后将数据库句柄传递给另外一个进程使用。

    在3.3.1以后的版本中,只要连接没有持有 fcntl() 锁,在线程间移动句柄是安全的。

    >> SQLite pragma

    The PRAGMA statement is an SQL extension specific to SQLite and used to modify the operation of the SQLite library or to query the SQLite library for internal (non-table) data.

    PRAGMA command 是一个特殊的命令。它用于修改 SQLite 库操作或查询库以取得内部(非表)数据

    •    auto_vacuum
    •    automatic_index
    •    cache_size
    •    case_sensitive_like
    •    checkpoint_fullfsync
    •    collation_list
    •    compile_options    
    •    database_list    
    •    encoding
    •    foreign_key_list
    •    foreign_keys
    •    freelist_count
    •    fullfsync
    •    ignore_check_constraints
    •    incremental_vacuum
    •    index_info
    •    index_list
    •    integrity_check
    •    journal_mode
    •    journal_size_limit
    •    legacy_file_format
    •    locking_mode
    •    max_page_count
    •    page_count
    •    page_size
    •    parser_trace
    •    quick_check
    •    read_uncommitted
    •    recursive_triggers
    •    reverse_unordered_selects
    •    schema_version
    •    secure_delete
    •    synchronous
    •    table_info
    •    temp_store    
    •    user_version
    •    vdbe_listing
    •    vdbe_trace
    •    wal_autocheckpoint
    •    wal_checkpoint
    •    writable_schema

  • 相关阅读:
    前后台验证字符串长度
    接口和抽象类该什么时候用?
    程序员常去网站汇总
    SQLServer复合查询条件(AND,OR,NOT)对NULL值的处理方法
    c#-轮询算法
    常用的SQL语句
    HTTP请求工具类
    asp.net mvc jQuery 城市二级联动
    ibatis动态多条件查询及模糊查询(oracle,mysql,sql)
    iBatis 中 Like 的写法实现模糊查询
  • 原文地址:https://www.cnblogs.com/piaoger/p/2257065.html
Copyright © 2020-2023  润新知