1. 程式人生 > >SQLite - C/C++介面 API(二)

SQLite - C/C++介面 API(二)

 

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 #include <stdio.h>
 2 #include <string.h>
 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 }

執行結果: