这是为什么对sqlite3_open我们使用双指针**和sqlite3_ prepare我们使用指针* [英] Why is that for sqlite3_open we use double pointer ** and for sqlite3_prepare we use pointer *

查看:401
本文介绍了这是为什么对sqlite3_open我们使用双指针**和sqlite3_ prepare我们使用指针*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在那里我明白错了,请大家指正。

我看了这个答案 http://stackoverflow.com/a/833124/5175709 以及我的理解是,因为对象可能扩展和运行的空间,然后存储位置也是我change.But有两家sqlite3的语法被引用的对象不同。为什么?

sqlite3_open我们有: sqlite3的** PPDB

  SQLITE_API INT SQLITE_STDCALL sqlite3_open(
  为const char *文件名,/ *数据库文件名(UTF-8)* /
  sqlite3的** PPDB / * OUT:SQLite的分贝手柄* /

和为sqlite3_ prepare我们有: sqlite3的*分贝

  SQLITE_API INT SQLITE_STDCALL sqlite3_ prepare_v2(
  * sqlite3的DB,/ *数据库句柄* /
  为const char * ZSQL,/ * SQL语句,UTF-8 EN codeD * /
  INT nByte,/ *字节ZSQL的最大长度。 * /
  sqlite3_stmt ** ppStmt,/ * OUT:语句句柄* /
  为const char ** pzTail / * OUT:指向ZSQL的未使用部分* /


解决方案

sqlite3_open 需要以某种方式给你一个数据库连接对象;即一个的sqlite3 对象。

要prevent人访问, sqlite3.h 声明的内部的sqlite3 为不透明的结构。这意味着,你不能分配一空的sqlite3 ,因为你不知道它持有; SQLite库必须分配为你,给你一个指向它的指针( sqlite3的* )。

所以现在我们有一个假设的函数 sqlite3的* sqlite3_open(...); ,打开一个数据库,并返回一个指向连接对象。但持有;如果出现错误?我们可以返回 NULL ,但如何是应该区分数据库中不存在从错误开发商数据库只读,数据库损坏或其他错误?

所以取而代之, sqlite3_open 返回一个整数返回code和连接指针写入内存的 PPDB 参数指向开盘是否成功。

sqlite3_open 通常采用这样的:

  sqlite3的* MYDB;
INT状态= sqlite3_open(/路径/到/ DB,&安培; MYDB);
如果(状态!= SQLITE_OK){
    //出错
    返回;
}
// sqlite3_open写了一个数据库指针MYDB,在这里使用它
状态= sqlite3_ prepare_v2(MYDB,SELECT * FROM无所谓,-1,NULL,NULL);
// ...

Please correct me where I understood wrong.

I read this answer http://stackoverflow.com/a/833124/5175709 and what I understood was that since the object could expand and run out of space then the memory location my also change.But here two sqlite3 syntaxes are referencing to the object differently. WHY?

sqlite3_open we have: sqlite3 **ppDb

SQLITE_API int SQLITE_STDCALL sqlite3_open(
  const char *filename,   /* Database filename (UTF-8) */
  sqlite3 **ppDb          /* OUT: SQLite db handle */

And for sqlite3_prepare we have: sqlite3 *db

SQLITE_API int SQLITE_STDCALL 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 */

解决方案

sqlite3_open needs to somehow give you a database connection object; i.e. a sqlite3 object.

To prevent people from accessing the internals, sqlite3.h declares sqlite3 as an opaque struct. This means that you cannot allocate space for a sqlite3 since you don't know what it holds; the SQLite library must allocate it for you and give you a pointer to it (sqlite3*).

So now we have a hypothetical function sqlite3* sqlite3_open(...);, which opens a DB and returns a pointer to the connection object. But hold on; what if an error occurs? We could return NULL, but how is the developer supposed to distinguish a "database doesn't exist" error from a "database is read only", "database is corrupted", or other errors?

So instead, sqlite3_open returns an integer return code, and writes the connection pointer to the memory that the ppDB parameter points to if opening succeeded.

sqlite3_open is usually used like this:

sqlite3* myDB;
int status = sqlite3_open("/path/to/db", &myDB);
if(status != SQLITE_OK) {
    // error occured
    return;
}
// sqlite3_open wrote a database pointer to myDB, use it here
status = sqlite3_prepare_v2(myDB, "SELECT * FROM whatever", -1, NULL, NULL);
// ...

这篇关于这是为什么对sqlite3_open我们使用双指针**和sqlite3_ prepare我们使用指针*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆