Why Is That For Sqlite3_open We Use Double Pointer ** And For Sqlite3_prepare We Use Pointer *
Solution 1:
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 occuredreturn;
}
// sqlite3_open wrote a database pointer to myDB, use it here
status = sqlite3_prepare_v2(myDB, "SELECT * FROM whatever", -1, NULL, NULL);
// ...
Post a Comment for "Why Is That For Sqlite3_open We Use Double Pointer ** And For Sqlite3_prepare We Use Pointer *"