SQLite3 -- C 编程

来源:互联网 发布:sql语句中的时间格式 编辑:程序博客网 时间:2024/05/17 06:18

http://jianlee.ylinux.org/Computer/C%E5%92%8CGNU%E5%BC%80%E5%8F%91/sqlite3.html

 

以 SQLite 3 为基础。版本 2 有很多区别。

介绍

SQLite 3.0 包含 83 个独立的接口函数,当时常用的并不多,甚至只要三个函数就能实现常见功能: sqlite3_open() ,sqlite3_exec() , 和 sqlite3_close() 。 很多数据库引擎的实现用sqlite3_prepare() 将 SQLite 语句编译为 "byte code" , 然后使用 sqlite3_step() 执行这个 "byte code"。使用sqlite3_column_ 字符串看头的接口函数通常用来抽取查询结果中信息。有些成对接口函数,带有 UTF-8 和 UTF-16 版本。还有些是用户自定义的 SQL 函数。

打开和关闭数据库

   typedef struct sqlite3 sqlite3;   int sqlite3_open (const char*, sqlite3**);   int sqlite3_open16 (const void*, sqlite3**);   int sqlite3_close (sqlite3*);   const char *sqlite3_errmsg (sqlite3*);   const void *sqlite3_errmsg16 (sqlite3*);   int sqlite3_errcode (sqlite3*);

sqlite3_errcode() 返回上次执行的 SQLite3 API 调用的返回值。 sqlite3_errmsg() 返回上次执行的 SQlite3 API 调用产生的出错信息。

error codes

    #define SQLITE_OK           0   /* 执行成功 */    #define SQLITE_ERROR        1   /* SQL error or missing database */    #define SQLITE_INTERNAL     2   /* An 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 sqlite_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   /* (Internal Only) Table or record not found */    #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   /* (Internal Only) Database table is empty */    #define SQLITE_SCHEMA      17   /* The database schema changed */    #define SQLITE_TOOBIG      18   /* Too much data for one row of a table */    #define SQLITE_CONSTRAINT  19   /* Abort due to contraint 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_ROW         100  /* sqlite_step() has another row ready */    #define SQLITE_DONE        101  /* sqlite_step() has finished executing */

执行 SQL 语句

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

这里的 sqlite3_exec() 函数与 SQLite 2 中的工作情况差不多。实际上, SQLite 3 中,这个函数是一堆函数的封装:

   typedef struct sqlite3_stmt sqlite3_stmt;   int sqlite3_prepare (sqlite3*, const char*, int, sqlite3_stmt**, const char**);   int sqlite3_prepare16 (sqlite3*, const void*, int, sqlite3_stmt**, const void**);   int sqlite3_finalize (sqlite3_stmt*);   int sqlite3_reset (sqlite3_stmt*);

sqlite3_prepare() 将 SQL 语句编译为 "byte code", sqlite3_reset() 重置一条 SQL 语句,一遍再次执行它。

SQL 语句也可以包括 "?" , "?nnn" , 或者 ":aaa" ,其中 "nnn" 代表整数 , "aaa" 代表标识符。这些是通配符,用以 sqlite3_bind 看头的一些接口函数填写。

   int sqlite3_bind_blob (sqlite3_stmt*, int, const void*, int n, void(*)(void*));   int sqlite3_bind_double (sqlite3_stmt*, int, double);   int sqlite3_bind_int (sqlite3_stmt*, int, int);   int sqlite3_bind_int64 (sqlite3_stmt*, int, long long int);   int sqlite3_bind_null (sqlite3_stmt*, int);   int sqlite3_bind_text (sqlite3_stmt*, int, const char*, int n, void(*)(void*));   int sqlite3_bind_text16 (sqlite3_stmt*, int, const void*, int n, void(*)(void*));   int sqlite3_bind_value (sqlite3_stmt*, int, const sqlite3_value*);

SQL 语句准备好后(prepared, and optionally bound),使用下面函数执行它:

   int sqlite3_step (sqlite3_stmt*);

sqlite3_step() 函数返回 SQLITE_ROW 表明返回一行查询结果。返回 SQLITE_DONE 表明执行完毕。返回 SQLITE_BUSY 表明不能打开数据库文件。使用下面函数处理返回 SQLITE_ROW 的情况:

   const void *sqlite3_column_blob (sqlite3_stmt*, int iCol);   int sqlite3_column_bytes (sqlite3_stmt*, int iCol);   int sqlite3_column_bytes16 (sqlite3_stmt*, int iCol);   int sqlite3_column_count (sqlite3_stmt*);   const char *sqlite3_column_decltype (sqlite3_stmt *, int iCol);   const void *sqlite3_column_decltype16 (sqlite3_stmt *, int iCol);   double sqlite3_column_double (sqlite3_stmt*, int iCol);   int sqlite3_column_int (sqlite3_stmt*, int iCol);   long long int sqlite3_column_int64 (sqlite3_stmt*, int iCol);   const char *sqlite3_column_name (sqlite3_stmt*, int iCol);   const void *sqlite3_column_name16 (sqlite3_stmt*, int iCol);   const unsigned char *sqlite3_column_text (sqlite3_stmt*, int iCol);   const void *sqlite3_column_text16 (sqlite3_stmt*, int iCol);   int sqlite3_column_type (sqlite3_stmt*, int iCol);

sqlite3_column_count() 返回查询结果的列数,可以在 sqlite3_prepare() 后的任何时间调用。sqlite3_data_count()sqlite3_column_count() 相似,只不过它在sqlite3_step() 后执行。如果先前的调用返回 SQLITE_DONE 者出错, sqlite3_data_count() 返回 0 ,而sqlite3_column_count() 会继续返回查询结果的列数。

返回值使用其他的 sqlite3_column_XXX() 函数解释,这些函数使用列数(column number)作为他们的第二个参数,列数从左到右从0开始计数。

sqlite3_column_type() 返回第 N 列值的数据类型,返回值是:

   #define SQLITE_INTEGER  1   #define SQLITE_FLOAT    2   #define SQLITE_TEXT     3   #define SQLITE_BLOB     4   #define SQLITE_NULL     5

sqlite3_column_decltype() 返回 text, 这个 text 是对应列在使用 "CREATE TABLE" 语句常见表时的申明类型。对于表达式,返回类型是空字符串。

sqlite3_column_name() 返回第 N 列的名字。

sqlite3_column_bytes() 返回 BLOB 类型列的字节数,或者 UTF-8 编码 text 类型字符串的字节数。sqlite3_column_bytes16() 针对 UTF-16.

sqlite3_column_blob() 返回 BLOB 数据, sqlite3_column_text() 返回 TEXT 数据,sqlite3_column_text16() 针对 UTF-16, sqlite3_column_int() 返回 INTERGER 数据,sqlite3_column_int64() 支队 64 位, sqlite3_column_double() 返回 floating point 数据。

使用 sqlite3_column_type() 匹配取回值的数据类型很有必要,如果数据类型不一致,可以自动转换。

数据类型转换可以使上次调用 sqlite3_column_blob(), sqlite3_column_text(), sqlite3_column_text16() 返回的指针失效。 下面三种情况指针都会失效:

  1. 初始值是 BLOB ,调用 sqlite3_column_text() 或 sqlite3_column_text16() ,A zero-terminator might need to be added to the string.
  2. 初始值是 UTF-8 text ,调用 sqlite3_column_bytes16() 或 sqlite3_column_text16() ,内容会被转换为 UTF-16
  3. 初始值是 UTF-16 text,调用 sqlite3_column_bytes() 或 sqlite3_column_text() , 内容转换为 UTF-8

The safest and easiest to remember policy is this: assume that any result from

  • sqlite3_column_blob(),
  • sqlite3_column_text(), or
  • sqlite3_column_text16()

is invalided by subsequent calls to

  • sqlite3_column_bytes(),
  • sqlite3_column_bytes16(),
  • sqlite3_column_text(), or
  • sqlite3_column_text16().

This means that you should always call sqlite3_column_bytes() or sqlite3_column_bytes16() before calling sqlite3_column_blob(), sqlite3_column_text(), or sqlite3_column_text16().

User-defined 函数

使用下面函数可以定义用户自己的函数:

   typedef struct sqlite3_value sqlite3_value;   int sqlite3_create_function (     sqlite3 *,     const char *zFunctionName,     int nArg,     int eTextRep,     void*,     void (*xFunc) (sqlite3_context*,int,sqlite3_value**),     void (*xStep) (sqlite3_context*,int,sqlite3_value**),     void (*xFinal) (sqlite3_context*)   );   int sqlite3_create_function16 (     sqlite3*,     const void *zFunctionName,     int nArg,     int eTextRep,     void*,     void (*xFunc) (sqlite3_context*,int,sqlite3_value**),     void (*xStep) (sqlite3_context*,int,sqlite3_value**),     void (*xFinal) (sqlite3_context*)   );   #define SQLITE_UTF8     1   #define SQLITE_UTF16    2   #define SQLITE_UTF16BE  3   #define SQLITE_UTF16LE  4   #define SQLITE_ANY      5