Platforms to show: All Mac Windows Linux Cross-Platform
Back to Database class.
Database.AddRow(TableName as String, row as DatabaseRow)
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 20.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
See also:
Database.AddRow(TableName as String, row as DatabaseRow, idColumnName as string = "") as Integer
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 24.3 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
Returns insert ID if possible or -1 if not found.
Implemented directly in SQLDatabaseMBS for CubeSQL, SQLite, mariaDB and MySQL. Other databases need column name for ID to run a select to get the value.
For PostgreSQL we use RETURNING, so you need to pass name of the ID column.
See also:
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 20.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
Changes to the database made after this call can be saved with CommitTransaction or undone with RollbackTransaction.
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 1.0 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
Calling Close does not issue a Commit, but some databases will automatically Commit changes in a transaction when you Close the connection and some database will automatically Rollback changes in a transaction when the connection is closed. Refer to the documentation for your database to check what its behavior is.
For desktop applications, you will often Connect to the database when the app starts and Close it when the app quits.
For web applications, you usually Connect to the database when the Session starts and Close it when the Session quits.
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 1.0 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
You have to have an open transaction to be able to use Commit. On SQLite (and other databases), you can start a transaction with this command:
BEGIN TRANSACTION
It can be sent using SQLExecute:
DB.SQLExecute("BEGIN TRANSACTION")
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 20.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 20.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
See also:
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 1.0 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
Before proceeding with database operations, test to be sure that Connect returns True.
If Connect returns False, you should also check the ErrorCode and ErrorMessage.
See Option() for various options you can set before connecting.
e.g. c.Option("SQLiteVFSFlags") = "1" for SQLite for read only access.
See also:
Database.ExecuteSQL(sql As String, ParamArray values As Variant)
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 20.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
Use this for commands that do not return any data, such as CREATE TABLE or INSERT.
See also:
Database.ExecuteSQL(sql As String, values() As Variant)
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 20.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
Use this for commands that do not return any data, such as CREATE TABLE or INSERT.
See also:
Database.InsertRecord(TableName as String, Data as DatabaseRecord)
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 1.0 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
Always check the Error property to verity that the data was added.
Database.Prepare(statement as String) as PreparedSQLStatement
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 1.0 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
A prepared statement is an SQL statement with parameters that has been pre-processed by the database so that it can be executed more quickly if it is re-used with different parameters. Prepared statements also mitigate the risk of SQL injection in web apps.
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 1.0 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
You will generally want to rollback database changes if a database error occurs within the transaction.
You have to have an open transaction to be able to use Rollback. On SQLite (and other databases), you can start a transaction with this command:
BEGIN TRANSACTION
It can be sent using SQLExecute:
DB.SQLExecute("BEGIN TRANSACTION")
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 20.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
Database.SelectSQL(sql As String, ParamArray values As Variant) as RowSet
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 20.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
See also:
Database.SelectSQL(sql As String, values() As Variant) as RowSet
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 20.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
See also:
Database.SQLExecute(ExecuteString as string)
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 1.0 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
To avoid SQL Injection, be sure to use Prepared SQL Statements.
Database.SQLSelect(SelectString as string) as RecordSet
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
method | SQL | MBS SQL Plugin | 1.0 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
SelectString contains the SQL statement.
Typically only SQL SELECT statements return a RecordSet, but some databases return a RecordSet for SQL commands such as INSERT, UPDATE or stored procedures.
If the SQL does not return data then Nil is returned. Nil is also usually returned if there is an error in the SQL statement, but you should instead check Database.Error to check if an error occurred.
To avoid SQL Injection, be sure to use Prepared SQL Statements.
The items on this page are in the following plugins: MBS SQL Plugin.
