Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1016529
  • 博文数量: 244
  • 博客积分: 6820
  • 博客等级: 准将
  • 技术积分: 3020
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 21:33
文章分类

全部博文(244)

文章存档

2013年(1)

2012年(16)

2011年(132)

2010年(3)

2009年(12)

2008年(80)

我的朋友

分类: 数据库开发技术

2011-04-30 08:42:11

以 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 #define SQLITE_INTERNAL 2 #define SQLITE_PERM 3 #define SQLITE_ABORT 4 #define SQLITE_BUSY 5 #define SQLITE_LOCKED 6 #define SQLITE_NOMEM 7 #define SQLITE_READONLY 8 #define SQLITE_INTERRUPT 9 #define SQLITE_IOERR 10 #define SQLITE_CORRUPT 11 #define SQLITE_NOTFOUND 12 #define SQLITE_FULL 13 #define SQLITE_CANTOPEN 14 #define SQLITE_PROTOCOL 15 #define SQLITE_EMPTY 16 #define SQLITE_SCHEMA 17 #define SQLITE_TOOBIG 18 #define SQLITE_CONSTRAINT 19 #define SQLITE_MISMATCH 20 #define SQLITE_MISUSE 21 #define SQLITE_NOLFS 22 #define SQLITE_AUTH 23 #define SQLITE_ROW 100 #define SQLITE_DONE 101

执行 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*));
                   

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
阅读(1615) | 评论(0) | 转发(1) |
0

上一篇:sqlite3_prepare

下一篇:SQLite学习笔记2

给主人留下些什么吧!~~