码迷,mamicode.com
首页 > 数据库 > 详细

SQLite - C/C++接口 API(二)

时间:2018-11-06 22:36:52      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:sqli   二进制   utf-8   def   alt   过程   from   creat   ring   

 

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 }

运行结果:

技术分享图片

 

SQLite - C/C++接口 API(二)

标签:sqli   二进制   utf-8   def   alt   过程   from   creat   ring   

原文地址:https://www.cnblogs.com/Toney-01-22/p/9918312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!