sqlite3_open()
Opening A New Database Connection
These routines open an SQLite database file as specified by the filename argument. The filename argument is interpreted as UTF-8 for sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte order for sqlite3_open16(). Adatabase connectionhandle is usually returned in *ppDb,even if an error occurs. The only exception is that if SQLite is unable to allocate memory to hold thesqlite3object,a NULL will be written into *ppDb instead of a pointer to thesqlite3object. If the database is opened (and/or created) successfully,thenSQLITE_OKis returned. Otherwise anerror codeis returned. Thesqlite3_errmsg()orsqlite3_errmsg16()routines can be used to obtain an English language description of the error following a failure of any of the sqlite3_open() routines. The default encoding for the database will be UTF-8 if sqlite3_open() or sqlite3_open_v2() is called and UTF-16 in the native byte order if sqlite3_open16() is used. Whether or not an error occurs when it is opened,resources associated with thedatabase connectionhandle should be released by passing it tosqlite3_close()when it is no longer required. The sqlite3_open_v2() interface works like sqlite3_open() except that it accepts two additional parameters for additional control over the new database connection. The flags parameter to sqlite3_open_v2() can take one of the following three values,optionally combined with theSQLITE_OPEN_NOMUTEX,SQLITE_OPEN_FULLMUTEX,100)" rel="nofollow" target="_blank">SQLITE_OPEN_SHAREDCACHE,100)" rel="nofollow" target="_blank">SQLITE_OPEN_PRIVATECACHE,and/orSQLITE_OPEN_URIflags: SQLITE_OPEN_READONLY The database is opened in read-only mode. If the database does not already exist,an error is returned. SQLITE_OPEN_READWRITE The database is opened for reading and writing if possible,or reading only if the file is write protected by the operating system. In either case the database must already exist,otherwise an error is returned. SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE The database is opened for reading and writing,and is created if it does not already exist. This is the behavior that is always used for sqlite3_open() and sqlite3_open16().If the 3rd parameter to sqlite3_open_v2() is not one of the combinations shown above optionally combined with otherSQLITE_OPEN_* bitsthen the behavior is undefined. If theSQLITE_OPEN_NOMUTEXflag is set,then the database connection opens in the multi-threadthreading modeas long as the single-thread mode has not been set at compile-time or start-time. If theSQLITE_OPEN_FULLMUTEXflag is set then the database connection opens in the serializedthreading modeunless single-thread was previously selected at compile-time or start-time. TheSQLITE_OPEN_SHAREDCACHEflag causes the database connection to be eligible to useshared cache mode,regardless of whether or not shared cache is enabled usingsqlite3_enable_shared_cache(). TheSQLITE_OPEN_PRIVATECACHEflag causes the database connection to not participate inshared cache modeeven if it is enabled. The fourth parameter to sqlite3_open_v2() is the name of thesqlite3_vfsobject that defines the operating system interface that the new database connection should use. If the fourth parameter is a NULL pointer then the defaultsqlite3_vfsobject is used. If the filename is ":memory:",then a private,temporary in-memory database is created for the connection. This in-memory database will vanish when the database connection is closed. Future versions of SQLite might make use of additional special filenames that begin with the ":" character. It is recommended that when a database filename actually does begin with a ":" character you should prefix the filename with a pathname such as "./" to avoid ambiguity. If the filename is an empty string,temporary on-disk database will be created. This private database will be automatically deleted as soon as the database connection is closed.
URI FilenamesIfURI filenameinterpretation is enabled,and the filename argument begins with "file:",then the filename is interpreted as a URI. URI filename interpretation is enabled if theSQLITE_OPEN_URIflag is set in the fourth argument to sqlite3_open_v2(),or if it has been enabled globally using theSQLITE_CONFIG_URIoption with thesqlite3_config()method or by theSQLITE_USE_URIcompile-time option. As of SQLite version 3.7.7,URI filename interpretation is turned off by default,but future releases of SQLite might enable URI filename interpretation by default. See "URI filenames" for additional information. URI filenames are parsed according to RFC 3986. If the URI contains an authority,then it must be either an empty string or the string "localhost". If the authority is not an empty string or "localhost",an error is returned to the caller. The fragment component of a URI,if present,is ignored. SQLite uses the path component of the URI as the name of the disk file which contains the database. If the path begins with a '/' character,then it is interpreted as an absolute path. If the path does not begin with a '/' (meaning that the authority section is omitted from the URI) then the path is interpreted as a relative path. On windows,the first component of an absolute path is a drive specification (e.g. "C:"). The query component of a URI may contain parameters that are interpreted either by SQLite itself,or by acustom VFS implementation. SQLite interprets the following three query parameters:
Specifying an unknown parameter in the query component of a URI is not an error. Future versions of SQLite might understand additional query parameters. See "query parameters with special meaning to SQLite" for additional information.
URI filename examples | URI filenames | Results | file:data.db Open the file "data.db" in the current directory. file:/home/fred/data.db
---|