|
| 1 | +--- |
| 2 | +title: DB_Open |
| 3 | +sidebar_label: DB_Open |
| 4 | +description: The function is used to open a connection to a SQLite database file, which is inside the `/scriptfiles` folder. |
| 5 | +tags: ["sqlite"] |
| 6 | +--- |
| 7 | + |
| 8 | +## Description |
| 9 | + |
| 10 | +The function is used to open a connection to a SQLite database, which is inside the "/scriptfiles" folder. |
| 11 | + |
| 12 | +| Name | Description | |
| 13 | +| ------------------------------------------------------------------- | ----------------------------------------------------- | |
| 14 | +| const name[] | File name of the database | |
| 15 | +| SQLITE_OPEN:flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | [Permissions / Flags](../resources/sqlite-open-flags) | |
| 16 | + |
| 17 | +## Returns |
| 18 | + |
| 19 | +Returns index (starting at 1) of the database connection. |
| 20 | + |
| 21 | +## Examples |
| 22 | + |
| 23 | +```c |
| 24 | +static DB:gDBConnectionHandle; |
| 25 | + |
| 26 | +// ... |
| 27 | + |
| 28 | +public OnGameModeInit() |
| 29 | +{ |
| 30 | + // ... |
| 31 | + |
| 32 | + // Create a connection to a database |
| 33 | + gDBConnectionHandle = DB_Open("example.db"); |
| 34 | + |
| 35 | + // If connection to the database exists |
| 36 | + if (gDBConnectionHandle) |
| 37 | + { |
| 38 | + // Successfully created a connection to the database |
| 39 | + print("Successfully created a connection to database \"example.db\"."); |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + // Failed to create a connection to the database |
| 44 | + print("Failed to open a connection to database \"example.db\"."); |
| 45 | + } |
| 46 | + |
| 47 | + // ... |
| 48 | + |
| 49 | + return 1; |
| 50 | +} |
| 51 | + |
| 52 | +public OnGameModeExit() |
| 53 | +{ |
| 54 | + // Close the connection to the database if connection is open |
| 55 | + if (DB_Close(gDBConnectionHandle)) |
| 56 | + { |
| 57 | + // Extra cleanup |
| 58 | + gDBConnectionHandle = DB:0; |
| 59 | + } |
| 60 | + |
| 61 | + // ... |
| 62 | + |
| 63 | + return 1; |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Notes |
| 68 | + |
| 69 | +:::warning |
| 70 | + |
| 71 | +It will create a new SQLite database file, if there is no SQLite database file with the same file name available. Close your SQLite database connection with [DB_Close](DB_Close)! |
| 72 | + |
| 73 | +::: |
| 74 | + |
| 75 | +## Related Functions |
| 76 | + |
| 77 | +- [DB_Open](DB_Open): Open a connection to an SQLite database |
| 78 | +- [DB_Close](DB_Close): Close the connection to an SQLite database |
| 79 | +- [DB_ExecuteQuery](DB_ExecuteQuery): Query an SQLite database |
| 80 | +- [DB_FreeResultSet](DB_FreeResultSet): Free result memory from a DB_ExecuteQuery |
| 81 | +- [DB_GetRowCount](DB_GetRowCount): Get the number of rows in a result |
| 82 | +- [DB_SelectNextRow](DB_SelectNextRow): Move to the next row |
| 83 | +- [DB_GetFieldCount](DB_GetFieldCount): Get the number of fields in a result |
| 84 | +- [DB_GetFieldName](DB_GetFieldName): Returns the name of a field at a particular index |
| 85 | +- [DB_GetFieldString](DB_GetFieldString): Get content of field with specified ID from current result row |
| 86 | +- [DB_GetFieldStringByName](DB_GetFieldStringByName): Get content of field with specified name from current result row |
| 87 | +- [DB_GetFieldInt](DB_GetFieldInt): Get content of field as an integer with specified ID from current result row |
| 88 | +- [DB_GetFieldIntByName](DB_GetFieldIntByName): Get content of field as an integer with specified name from current result row |
| 89 | +- [DB_GetFieldFloat](DB_GetFieldFloat): Get content of field as a float with specified ID from current result row |
| 90 | +- [DB_GetFieldFloatByName](DB_GetFieldFloatByName): Get content of field as a float with specified name from current result row |
| 91 | +- [DB_GetMemHandle](DB_GetMemHandle): Get memory handle for an SQLite database that was opened with DB_Open. |
| 92 | +- [DB_GetLegacyDBResult](DB_GetLegacyDBResult): Get memory handle for an SQLite query that was executed with DB_ExecuteQuery. |
| 93 | +- [DB_GetDatabaseConnectionCount](DB_GetDatabaseConnectionCount): The function gets the number of open database connections for debugging purposes. |
| 94 | +- [DB_GetDatabaseResultSetCount](DB_GetDatabaseResultSetCount): The function gets the number of open database results. |
| 95 | + |
| 96 | +## Related Resources |
| 97 | + |
| 98 | +- [SQLite Open Flags](../resources/sqlite-open-flags) |
0 commit comments