Sqlite Api

Table of Contents

1. SQlite: API

1.1. sqlite3_open

1.2. sqltie3_prepare_v2

1.3. sqlite3_step

1.4. sqlite3_reset

1.5. sqlite3_bind

1.6. sqlite3_exec

1.7. sqlite3_get_tables

1.8. sqlite3_mprintf

1.9. sqlite3_commit_hook

1.10. sqlite3_rollback_hook

1.11. sqlite3_busy_handler

1.12. sqlite3_busy_timeout

1.13. sqlite3_profile

1.14. sqlite3_trace

1.15. sqlite3_limit

1.16. sqlite3_status

1.17. sqlite3_db_status

1.18. sqlite3_stmt_status

1.19. sqlite3_config

1.20. sqlite3_db_config

  • SQLITE_DBCONFIG_LOOKASIDE
  • SQLITE_DBCONFIG_ENABLE_FKEY
  • SQLITE_DBCONFIG_ENABLE_TRIGGER

1.21. sqlite3_memory_used

1.22. sqlite3_memory_highwater

1.23. sqlite3_last_insert_rowid

1.24. sqlite3_set_authorizer

1.25. sqlite3_changes

1.26. sqlite3_get_tables

1.27. sqlite3_commit_hook

1.28. sqlite3_rollback_hook

1.29. sqlite3_update_hook

1.30. sqlite3_release_memory

1.31. sqlite3_db_release_memory

1.32. sqlite3_soft_heap_limit64

1.33. sqlite3_table_column_metadata

1.34. blob 操作

1.34.1. sqlite3_blob_open

1.34.2. sqlite3_blob_read

1.34.3. sqlite3_blob_write

1.35. mutex

1.35.1. sqlite3_mutex_alloc

1.35.2. sqlite3_mutex_enter

1.35.3. sqlite3_mutex_free

1.35.4. sqlite3_mutex_try

1.35.5. sqlite3_mutex_leave

1.36. wal

1.36.1. sqlite3_wal_hook

1.36.2. sqlite3_wal_autocheckpoint

1.37. sqlite3_file_control

1.38. sqlite3_finalize

在 auto-commit 模式下, sqlite3_finalize 相当于 commit 的作用:

  • 释放锁
  • sync 日志与数据库

1.39. extension api

1.39.1. user defined functions

sqlite3_create_funtion

1.39.2. user defined aggregates

1.39.3. user define collations

sqlite3_create_collation

1.40. sample code

#include <sqlite3.h>
#include <pthread.h>

void query(sqlite3 * db) {
    sqlite3_stmt *stmt;
    const char * tail;
    int rc=sqlite3_prepare_v2(db,"select * from test", -1, &stmt, &tail);
    if (rc!=SQLITE_OK) {
        fprintf("error prepare stmt: %s", sqlite3_errmsg(db));
        exit (1);
    }
    int count=0;
    rc=sqlite3_step(stmt);
    while (rc==SQLITE_ROW) {
        count++;
        rc=sqlite3_step(stmt);
    }
    sqlite3_finalize(stmt);
    printf("query returns %d\n",count);
}

void * fun(void * args) {
    query((sqlite3 *)args);
}


int main(int argc, char *argv[]) {
    sqlite3_config(SQLITE_CONFIG_SERIALIZED);
    /* sqlite3_enable_shared_cache(SQLITE_OPEN_SHAREDCACHE); */
    sqlite3 *db;
    sqlite3 *db2;

    int rc=sqlite3_open_v2("/home/sunway/test.db", &db, SQLITE_OPEN_READWRITE, 0);

    if (rc) {
        printf("can't open db: %s",sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(1);
    }

    pthread_t tid;
    pthread_create(&tid, NULL, fun, db);

    query(db);
    pthread_join(tid, NULL);
    /* char * sql="insert into test values (\"test\",1)"; */
    /* char * zerr; */
    /* rc=sqlite3_exec(db,sql,0,0,&zerr); */
    /* if (rc!=SQLITE_OK) { */
    /*  printf("error when insert: %s", zerr); */
    /*  exit (1); */
    /* } */
    return 0;
}

Author: [email protected]
Date: 2017-09-09 Sat 00:00
Last updated: 2021-07-23 Fri 14:22

知识共享许可协议