1.Dd January 24, 2024 2.Dt SQLITE3_OPEN 3 3.Os 4.Sh NAME 5.Nm sqlite3_open , 6.Nm sqlite3_open16 , 7.Nm sqlite3_open_v2 8.Nd opening a new database connection 9.Sh SYNOPSIS 10.In sqlite3.h 11.Ft int 12.Fo sqlite3_open 13.Fa "const char *filename" 14.Fa "sqlite3 **ppDb" 15.Fc 16.Ft int 17.Fo sqlite3_open16 18.Fa "const void *filename" 19.Fa "sqlite3 **ppDb" 20.Fc 21.Ft int 22.Fo sqlite3_open_v2 23.Fa "const char *filename" 24.Fa "sqlite3 **ppDb" 25.Fa "int flags" 26.Fa "const char *zVfs" 27.Fc 28.Sh DESCRIPTION 29These routines open an SQLite database file as specified by the filename 30argument. 31The filename argument is interpreted as UTF-8 for sqlite3_open() and 32sqlite3_open_v2() and as UTF-16 in the native byte order for sqlite3_open16(). 33A database connection handle is usually returned 34in *ppDb, even if an error occurs. 35The only exception is that if SQLite is unable to allocate memory to 36hold the sqlite3 object, a NULL will be written into *ppDb instead 37of a pointer to the sqlite3 object. 38If the database is opened (and/or created) successfully, then SQLITE_OK 39is returned. 40Otherwise an error code is returned. 41The 42.Fn sqlite3_errmsg 43or 44.Fn sqlite3_errmsg16 45routines can be used to obtain an English language description of the 46error following a failure of any of the sqlite3_open() routines. 47.Pp 48The default encoding will be UTF-8 for databases created using sqlite3_open() 49or sqlite3_open_v2(). 50The default encoding for databases created using sqlite3_open16() will 51be UTF-16 in the native byte order. 52.Pp 53Whether or not an error occurs when it is opened, resources associated 54with the database connection handle should be released 55by passing it to 56.Fn sqlite3_close 57when it is no longer required. 58.Pp 59The sqlite3_open_v2() interface works like sqlite3_open() except that 60it accepts two additional parameters for additional control over the 61new database connection. 62The flags parameter to sqlite3_open_v2() must include, at a minimum, 63one of the following three flag combinations: 64.Bl -tag -width Ds 65.It SQLITE_OPEN_READONLY 66The database is opened in read-only mode. 67If the database does not already exist, an error is returned. 68.It SQLITE_OPEN_READWRITE 69The database is opened for reading and writing if possible, or reading 70only if the file is write protected by the operating system. 71In either case the database must already exist, otherwise an error 72is returned. 73For historical reasons, if opening in read-write mode fails due to 74OS-level permissions, an attempt is made to open it in read-only mode. 75.Fn sqlite3_db_readonly 76can be used to determine whether the database is actually read-write. 77.It SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE 78The database is opened for reading and writing, and is created if it 79does not already exist. 80This is the behavior that is always used for sqlite3_open() and sqlite3_open16(). 81.El 82.Pp 83In addition to the required flags, the following optional flags are 84also supported: 85.Bl -tag -width Ds 86.It SQLITE_OPEN_URI 87The filename can be interpreted as a URI if this flag is set. 88.It SQLITE_OPEN_MEMORY 89The database will be opened as an in-memory database. 90The database is named by the "filename" argument for the purposes of 91cache-sharing, if shared cache mode is enabled, but the "filename" 92is otherwise ignored. 93.It SQLITE_OPEN_NOMUTEX 94The new database connection will use the "multi-thread" threading mode. 95This means that separate threads are allowed to use SQLite at the same 96time, as long as each thread is using a different database connection. 97.It SQLITE_OPEN_FULLMUTEX 98The new database connection will use the "serialized" threading mode. 99This means the multiple threads can safely attempt to use the same 100database connection at the same time. 101(Mutexes will block any actual concurrency, but in this mode there 102is no harm in trying.) 103.It SQLITE_OPEN_SHAREDCACHE 104The database is opened shared cache enabled, overriding 105the default shared cache setting provided by 106.Fn sqlite3_enable_shared_cache . 107The use of shared cache mode is discouraged 108and hence shared cache capabilities may be omitted from many builds 109of SQLite. 110In such cases, this option is a no-op. 111.It SQLITE_OPEN_PRIVATECACHE 112The database is opened shared cache disabled, overriding 113the default shared cache setting provided by 114.Fn sqlite3_enable_shared_cache . 115.It SQLITE_OPEN_EXRESCODE 116The database connection comes up in "extended result code mode". 117In other words, the database behaves has if sqlite3_extended_result_codes(db,1) 118where called on the database connection as soon as the connection is 119created. 120In addition to setting the extended result code mode, this flag also 121causes 122.Fn sqlite3_open_v2 123to return an extended result code. 124.It SQLITE_OPEN_NOFOLLOW 125The database filename is not allowed to contain a symbolic link 126.El 127.Pp 128If the 3rd parameter to sqlite3_open_v2() is not one of the required 129combinations shown above optionally combined with other SQLITE_OPEN_* bits 130then the behavior is undefined. 131Historic versions of SQLite have silently ignored surplus bits in the 132flags parameter to sqlite3_open_v2(), however that behavior might not 133be carried through into future versions of SQLite and so applications 134should not rely upon it. 135Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op for 136sqlite3_open_v2(). 137The SQLITE_OPEN_EXCLUSIVE does *not* cause the open to fail if the 138database already exists. 139The SQLITE_OPEN_EXCLUSIVE flag is intended for use by the VFS interface 140only, and not by sqlite3_open_v2(). 141.Pp 142The fourth parameter to sqlite3_open_v2() is the name of the sqlite3_vfs 143object that defines the operating system interface that the new database 144connection should use. 145If the fourth parameter is a NULL pointer then the default sqlite3_vfs 146object is used. 147.Pp 148If the filename is ":memory:", then a private, temporary in-memory 149database is created for the connection. 150This in-memory database will vanish when the database connection is 151closed. 152Future versions of SQLite might make use of additional special filenames 153that begin with the ":" character. 154It is recommended that when a database filename actually does begin 155with a ":" character you should prefix the filename with a pathname 156such as "./" to avoid ambiguity. 157.Pp 158If the filename is an empty string, then a private, temporary on-disk 159database will be created. 160This private database will be automatically deleted as soon as the 161database connection is closed. 162.Ss URI Filenames 163If URI filename interpretation is enabled, and the filename 164argument begins with "file:", then the filename is interpreted as a 165URI. 166URI filename interpretation is enabled if the SQLITE_OPEN_URI 167flag is set in the third argument to sqlite3_open_v2(), or if it has 168been enabled globally using the SQLITE_CONFIG_URI 169option with the 170.Fn sqlite3_config 171method or by the SQLITE_USE_URI compile-time option. 172URI filename interpretation is turned off by default, but future releases 173of SQLite might enable URI filename interpretation by default. 174See "URI filenames" for additional information. 175.Pp 176URI filenames are parsed according to RFC 3986. 177If the URI contains an authority, then it must be either an empty string 178or the string "localhost". 179If the authority is not an empty string or "localhost", an error is 180returned to the caller. 181The fragment component of a URI, if present, is ignored. 182.Pp 183SQLite uses the path component of the URI as the name of the disk file 184which contains the database. 185If the path begins with a '/' character, then it is interpreted as 186an absolute path. 187If the path does not begin with a '/' (meaning that the authority section 188is omitted from the URI) then the path is interpreted as a relative 189path. 190On windows, the first component of an absolute path is a drive specification 191(e.g. "C:"). 192.Pp 193The query component of a URI may contain parameters that are interpreted 194either by SQLite itself, or by a custom VFS implementation. 195SQLite and its built-in VFSes interpret the following query parameters: 196.Bl -bullet 197.It 198\fBvfs\fP: The "vfs" parameter may be used to specify the name of a VFS object 199that provides the operating system interface that should be used to 200access the database file on disk. 201If this option is set to an empty string the default VFS object is 202used. 203Specifying an unknown VFS is an error. 204If sqlite3_open_v2() is used and the vfs option is present, then the 205VFS specified by the option takes precedence over the value passed 206as the fourth parameter to sqlite3_open_v2(). 207.It 208\fBmode\fP: The mode parameter may be set to either "ro", "rw", "rwc", or 209"memory". 210Attempting to set it to any other value is an error. 211If "ro" is specified, then the database is opened for read-only access, 212just as if the SQLITE_OPEN_READONLY flag had been 213set in the third argument to sqlite3_open_v2(). 214If the mode option is set to "rw", then the database is opened for 215read-write (but not create) access, as if SQLITE_OPEN_READWRITE (but 216not SQLITE_OPEN_CREATE) had been set. 217Value "rwc" is equivalent to setting both SQLITE_OPEN_READWRITE and 218SQLITE_OPEN_CREATE. 219If the mode option is set to "memory" then a pure in-memory database 220that never reads or writes from disk is used. 221It is an error to specify a value for the mode parameter that is less 222restrictive than that specified by the flags passed in the third parameter 223to sqlite3_open_v2(). 224.It 225\fBcache\fP: The cache parameter may be set to either "shared" or "private". 226Setting it to "shared" is equivalent to setting the SQLITE_OPEN_SHAREDCACHE 227bit in the flags argument passed to sqlite3_open_v2(). 228Setting the cache parameter to "private" is equivalent to setting the 229SQLITE_OPEN_PRIVATECACHE bit. 230If sqlite3_open_v2() is used and the "cache" parameter is present in 231a URI filename, its value overrides any behavior requested by setting 232SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag. 233.It 234\fBpsow\fP: The psow parameter indicates whether or not the powersafe overwrite 235property does or does not apply to the storage media on which the database 236file resides. 237.It 238\fBnolock\fP: The nolock parameter is a boolean query parameter which if 239set disables file locking in rollback journal modes. 240This is useful for accessing a database on a filesystem that does not 241support locking. 242Caution: Database corruption might result if two or more processes 243write to the same database and any one of those processes uses nolock=1. 244.It 245\fBimmutable\fP: The immutable parameter is a boolean query parameter that 246indicates that the database file is stored on read-only media. 247When immutable is set, SQLite assumes that the database file cannot 248be changed, even by a process with higher privilege, and so the database 249is opened read-only and all locking and change detection is disabled. 250Caution: Setting the immutable property on a database file that does 251in fact change can result in incorrect query results and/or SQLITE_CORRUPT 252errors. 253.El 254.Pp 255Specifying an unknown parameter in the query component of a URI is 256not an error. 257Future versions of SQLite might understand additional query parameters. 258See "query parameters with special meaning to SQLite" 259for additional information. 260.Ss URI filename examples 261.Pp 262 URI filenames Results 263 file:data.db Open the file "data.db" in the current directory. 264 file:/home/fred/data.db file:///home/fred/data.db file://localhost/home/fred/data.db 265 Open the database file "/home/fred/data.db". 266 file://darkstar/home/fred/data.db An error. 267"darkstar" is not a recognized authority. 268 file:///C:/Documents%20and%20Settings/fred/Desktop/data.db Windows 269only: Open the file "data.db" on fred's desktop on drive C:. 270Note that the %20 escaping in this example is not strictly necessary 271- space characters can be used literally in URI filenames. 272 file:data.db?mode=ro&cache=private Open file "data.db" in the current 273directory for read-only access. 274Regardless of whether or not shared-cache mode is enabled by default, 275use a private cache. 276 file:/home/fred/data.db?vfs=unix-dotfile Open file "/home/fred/data.db". 277Use the special VFS "unix-dotfile" that uses dot-files in place of 278posix advisory locking. 279 file:data.db?mode=readonly An error. 280"readonly" is not a valid option for the "mode" parameter. 281Use "ro" instead: "file:data.db?mode=ro". 282.Pp 283URI hexadecimal escape sequences (%HH) are supported within the path 284and query components of a URI. 285A hexadecimal escape sequence consists of a percent sign - "%" - followed 286by exactly two hexadecimal digits specifying an octet value. 287Before the path or query components of a URI filename are interpreted, 288they are encoded using UTF-8 and all hexadecimal escape sequences replaced 289by a single byte containing the corresponding octet. 290If this process generates an invalid UTF-8 encoding, the results are 291undefined. 292.Pp 293\fBNote to Windows users:\fP The encoding used for the filename argument 294of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever 295codepage is currently defined. 296Filenames containing international characters must be converted to 297UTF-8 prior to passing them into sqlite3_open() or sqlite3_open_v2(). 298.Pp 299\fBNote to Windows Runtime users:\fP The temporary directory must be set 300prior to calling sqlite3_open() or sqlite3_open_v2(). 301Otherwise, various features that require the use of temporary files 302may fail. 303.Pp 304.Sh IMPLEMENTATION NOTES 305These declarations were extracted from the 306interface documentation at line 3462. 307.Bd -literal 308SQLITE_API int sqlite3_open( 309 const char *filename, /* Database filename (UTF-8) */ 310 sqlite3 **ppDb /* OUT: SQLite db handle */ 311); 312SQLITE_API int sqlite3_open16( 313 const void *filename, /* Database filename (UTF-16) */ 314 sqlite3 **ppDb /* OUT: SQLite db handle */ 315); 316SQLITE_API int sqlite3_open_v2( 317 const char *filename, /* Database filename (UTF-8) */ 318 sqlite3 **ppDb, /* OUT: SQLite db handle */ 319 int flags, /* Flags */ 320 const char *zVfs /* Name of VFS module to use */ 321); 322.Ed 323.Sh SEE ALSO 324.Xr sqlite3 3 , 325.Xr sqlite3_close 3 , 326.Xr sqlite3_config 3 , 327.Xr sqlite3_db_readonly 3 , 328.Xr sqlite3_enable_shared_cache 3 , 329.Xr sqlite3_errcode 3 , 330.Xr sqlite3_temp_directory 3 , 331.Xr sqlite3_vfs 3 , 332.Xr SQLITE_CONFIG_SINGLETHREAD 3 , 333.Xr SQLITE_IOCAP_ATOMIC 3 , 334.Xr SQLITE_OK 3 , 335.Xr SQLITE_OPEN_READONLY 3 336