1、打开数据库
SQLITE_API int sqlite3_open16(
const void *filename, /* Database filename (UTF-16) */ sqlite3 **ppDb /* OUT: SQLite db handle */);SQLITE_API int sqlite3_open_v2( const char *filename, /* Database filename (UTF-8) */ sqlite3 **ppDb, /* OUT: SQLite db handle */ int flags, /* Flags */ const char *zVfs /* Name of VFS module to use */);/*
** CAPI3REF: Flags For File Open Operations**** These bit values are intended for use in the** 3rd parameter to the [sqlite3_open_v2()] interface and** in the 4th parameter to the [sqlite3_vfs.xOpen] method.*/#define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */#define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */#define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */#define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */#define SQLITE_OPEN_MEMORY 0x00000080 /* Ok for sqlite3_open_v2() */#define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */#define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */#define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */#define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */#define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */#define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */#define SQLITE_OPEN_WAL 0x00080000 /* VFS only */2、sqlite3_prepare函数
SQLITE_API int sqlite3_prepare(
sqlite3 *db, /* Database handle */ const char *zSql, /* SQL statement, UTF-8 encoded */ int nByte, /* Maximum length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const char **pzTail /* OUT: Pointer to unused portion of zSql */);SQLITE_API int sqlite3_prepare_v2( sqlite3 *db, /* Database handle */ const char *zSql, /* SQL statement, UTF-8 encoded */ int nByte, /* Maximum length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const char **pzTail /* OUT: Pointer to unused portion of zSql */);SQLITE_API int sqlite3_prepare16( sqlite3 *db, /* Database handle */ const void *zSql, /* SQL statement, UTF-16 encoded */ int nByte, /* Maximum length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const void **pzTail /* OUT: Pointer to unused portion of zSql */);SQLITE_API int sqlite3_prepare16_v2( sqlite3 *db, /* Database handle */ const void *zSql, /* SQL statement, UTF-16 encoded */ int nByte, /* Maximum length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const void **pzTail /* OUT: Pointer to unused portion of zSql */);
3、sqlite3_step
SQLITE_API int sqlite3_step(sqlite3_stmt*);
函数的返回值基于创建sqlite3_stmt参数所使用的函数,假如是使用老版本的接口sqlite3_prepare()和sqlite3_prepare16(),返回值会是 SQLITE_BUSY, SQLITE_DONE, SQLITE_ROW, SQLITE_ERROR 或 SQLITE_MISUSE,
而v2版本的接口sqlite3_prepare_v2()和sqlite3_prepare16_v2()则会同时返回这些结果码和扩展结果码。
4、sqlite3_column
这个过程从执行sqlite3_step()执行一个准备语句得到的结果集的当前行中返回一个列。每次sqlite3_step得到一个结果集的列停下后,
这个过程就可以被多次调用去查询这个行的各列的值。对列操作是有多个函数,均以sqlite3_column为前缀
SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
BLOB数据类型是指二进制的数据块,比如要在数据库中存放一张图片,这张图片就会以二进制形式存放,在sqlite中对应的数据类型就是BLOB
SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);5. sqlite3_finalize
int sqlite3_finalize(sqlite3_stmt *pStmt);
这个过程销毁前面被sqlite3_prepare创建的准备语句,每个准备语句都必须使用这个函数去销毁以防止内存泄露。
编程实例:
1 #include2 #include 3 #include "../Sqlite3/sqlite3.h" 4 5 int main (int argc, char *argv[]) 6 { 7 char *p_query = "SELECT * FROM PERSON;"; 8 char *p_name = "mytest.db"; 9 sqlite3_stmt *p_stmt = NULL;10 char *p_err_msg = NULL;11 int rec = 0;12 sqlite3 *p_db = NULL;13 14 if (SQLITE_OK != (rec = sqlite3_open_v2(p_name, &p_db, SQLITE_OPEN_READONLY, NULL) ))15 {16 printf("sqlite3_open_v2 function err %s\n");17 return -1;18 }19 20 if (SQLITE_OK != (rec = sqlite3_prepare_v2(p_db, p_query, -1, &p_stmt, NULL)))21 {22 printf("sqlite3_prepare_v2 function err : %s\n", sqlite3_errmsg(p_db));23 sqlite3_close(p_db);24 return -1;25 }26 27 while (SQLITE_ROW == sqlite3_step(p_stmt))28 {29 printf("ID = %d\n", sqlite3_column_int64(p_stmt, 0));30 printf("NAME = %s\n", sqlite3_column_text(p_stmt,1));31 printf("AGE = %d\n", sqlite3_column_int(p_stmt, 2));32 printf("\n");33 }34 35 sqlite3_close(p_db);36 p_db = NULL;37 38 return 0;39 }
运行结果: