Method Maps5
SymbolDescription
DatabaseA Database represents a live connection to a database, either over the wire, through a unix domain socket, or over an open file.
DBDriverA Driver represents a database backend, currently MySQL or SQLite. Driver handles cannot be closed.
DBResultSetRepresents a set of results returned from executing a query.
DBStatementA DBStatement is a pre-compiled SQL query that may be executed multiple times with different parameters. A DBStatement holds a reference to the Database that prepared it.
TransactionA Transaction is a collection of SQL statements that must all execute successfully or not at all.
Enumerations3
SymbolDescription
DBBindTypeDescribes binding types.
DBPriorityThreading priority level.
DBResultDescribes a database field fetch status.
Type Sets1
SymbolDescription
SQLTxnSuccess
Type Definitions4
SymbolDescription
SQLConnectCallbackCallback for receiving asynchronous database connections.
SQLQueryCallbackCallback for receiving asynchronous database query results.
SQLTCallbackGeneral callback for threaded SQL stuff.
SQLTxnFailureCallback for a failed transaction.
Functions46
SymbolDescription
SQL_AddQueryAdds a query to a transaction object.
SQL_BindParamFloatBinds a parameter in a prepared statement to a given float value.
SQL_BindParamIntBinds a parameter in a prepared statement to a given integer value.
SQL_BindParamStringBinds a parameter in a prepared statement to a given string value.
SQL_CheckConfigReturns if a named configuration is present in databases.cfg.
SQL_ConnectCreates an SQL connection from a named configuration.
SQL_ConnectCustomConnects to a database using key value pairs containing the database info. The key/value pairs should match what would be in databases.cfg. I.e. "driver" should be "default" or a driver name (or omitted for the default). For SQLite, only the "database" parameter is needed in addition. For drivers which require external connections, more of the parameters may be needed. In general it is discouraged to use this function. Connections should go through databases.cfg for greatest flexibility on behalf of users.
SQL_ConnectEx
SQL_CreateTransactionCreates a new transaction object. A transaction object is a list of queries that can be sent to the database thread and executed as a single transaction.
SQL_DefConnectCreates a default SQL connection.
SQL_EscapeStringEscapes a database string for literal insertion. This is not needed for binding strings in prepared statements. Generally, database strings are inserted into queries enclosed in single quotes ('). If user input has a single quote in it, the quote needs to be escaped. This function ensures that any unsafe characters are safely escaped according to the database engine and the database's character set. NOTE: SourceMod only guarantees properly escaped strings when the query encloses the string in single quotes. While drivers tend to allow double quotes (") instead, the string may be not be escaped (for example, on SQLite)!
SQL_ExecuteExecutes a prepared statement. All parameters must be bound beforehand.
SQL_ExecuteTransactionSends a transaction to the database thread. The transaction handle is automatically closed. When the transaction completes, the optional callback is invoked.
SQL_FastQueryExecutes a query and ignores the result set.
SQL_FetchFloatFetches a float from a field in the current row of a result set. If the result is NULL, a value of 0.0 will be returned. A NULL check can be done with the result parameter, or SQL_IsFieldNull().
SQL_FetchIntFetches an integer from a field in the current row of a result set. If the result is NULL, a value of 0 will be returned. A NULL check can be done with the result parameter, or SQL_IsFieldNull().
SQL_FetchMoreResultsAdvances to the next set of results. In some SQL implementations, multiple result sets can exist on one query. This is possible in MySQL with simple queries when executing a CALL query. If this is the case, all result sets must be processed before another query is made.
SQL_FetchRowFetches a row from the current result set. This must be successfully called before any results are fetched. If this function fails, SQL_MoreRows() can be used to tell if there was an error or the result set is finished.
SQL_FetchSizeReturns the length of a field's data in the current row of a result set. This only needs to be called for strings to determine how many bytes to use. Note that the return value does not include the null terminator.
SQL_FetchStringFetches a string from a field in the current row of a result set. If the result is NULL, an empty string will be returned. A NULL check can be done with the result parameter, or SQL_IsFieldNull().
SQL_FieldNameToNumRetrieves a field index by name.
SQL_FieldNumToNameRetrieves the name of a field by index.
SQL_FormatQueryFormats a string according to the SourceMod format rules (see documentation). All format specifiers are escaped (see SQL_EscapeString) unless the '!' flag is used.
SQL_GetAffectedRowsReturns the number of affected rows from the last query.
SQL_GetDriverReturns a driver Handle from a name string. If the driver is not found, SourceMod will attempt to load an extension named dbi.<name>.ext.[dll|so].
SQL_GetDriverIdentRetrieves a driver's identification string. Example: "mysql", "sqlite"
SQL_GetDriverProductRetrieves a driver's product string. Example: "MySQL", "SQLite"
SQL_GetErrorReturns the error reported by the last query.
SQL_GetFieldCountRetrieves the number of fields in the last result set.
SQL_GetInsertIdReturns the last query's insertion id.
SQL_GetRowCountRetrieves the number of rows in the last result set.
SQL_HasResultSetReturns whether or not a result set exists. This will return true even if 0 results were returned, but false on queries like UPDATE, INSERT, or DELETE.
SQL_IsFieldNullReturns whether a field's data in the current row of a result set is NULL or not. NULL is an SQL type which means "no data."
SQL_IsSameConnectionTells whether two database handles both point to the same database connection.
SQL_LockDatabaseLocks a database so threading operations will not interrupt. If you are using a database Handle for both threading and non-threading, this MUST be called before doing any set of non-threading DB operations. Otherwise you risk corrupting the database driver's memory or network connection. Leaving a lock on a database and then executing a threaded query results in a dead lock! Make sure to call SQL_UnlockDatabase()! If the lock cannot be acquired, the main thread will pause until the threaded operation has concluded. Care should be taken to not lock an already-locked database. Internally, lock calls are nested recursively and must be paired with an equal amount of unlocks to be undone. This behaviour should not be relied on.
SQL_MoreRowsReturns if there are more rows.
SQL_PrepareQueryCreates a new prepared statement query. Prepared statements can be executed any number of times. They can also have placeholder parameters, similar to variables, which can be bound safely and securely (for example, you do not need to quote bound strings). Statement handles will work in any function that accepts a Query handle.
SQL_QueryExecutes a simple query and returns a new query Handle for receiving the results.
SQL_QuoteString
SQL_ReadDriverReads the driver of an opened database.
SQL_RewindRewinds a result set back to the first result.
SQL_SetCharsetSets the character set of the current connection. Like SET NAMES .. in mysql, but stays after connection problems. Example: "utf8", "latin1"
SQL_TConnectConnects to a database via a thread. This can be used instead of SQL_Connect() if you wish for non-blocking functionality. It is not necessary to use this to use threaded queries. However, if you don't (or you mix threaded/non-threaded queries), you should see SQL_LockDatabase().
SQL_TQueryExecutes a simple query via a thread. The query Handle is passed through the callback. The database Handle returned through the callback is always a new Handle, and if necessary, SQL_IsSameConnection() should be used to test against other connections. The query Handle returned through the callback is temporary and destroyed at the end of the callback. If you need to hold onto it, use CloneHandle().
SQL_UnlockDatabaseUnlocks a database so threading operations may continue.
SQLite_UseDatabaseGrabs a handle to an SQLite database, creating one if it does not exist. Unless there are extenuating circumstances, you should consider using "sourcemod-local" as the database name. This provides some unification between plugins on behalf of users. As a precaution, you should always create some sort of unique prefix to your table names so there are no conflicts, and you should never drop or modify tables that you do not own.