Platforms to show: All Mac Windows Linux Cross-Platform
SQLDatabaseMBS class
Super class: Database
Type | Topic | Plugin | Version | macOS | Windows | Linux | iOS | Targets |
class | SQL | MBS SQL Plugin | 9.3 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | All |
You can use the SQL plugin without using Xojo built in database classes if you use the SQLConnectionMBS and SQLCommandMBS classes.
Or you use the SQLDatabaseMBS class which is a subclass of the database class and can be used with Xojo's RecordSet class. The current implementation is not complete. You can connect with passing the database URL in the DatabaseName property of the SQLDatabaseMBS class. You prefix this URL with the database type you are using.
You can use Execute and Select to run SQL statements. Errors can be queries with the lasterror properties. For the RecordSet, you can get the column count, the column names and values and move to the next row. All the other methods like deleting a record or updating a value are not implemented and you need to use SQL commands to do this.
Supported databases: CubeSQL, Centura SQLBase, DB2, DuckDB, Firebird, Informix, InterBase, MariaDB, Microsoft Access, Microsoft SQL Server, MySQL, ODBC, Oracle Database Server, PostgreSQL, SQL Anywhere, SQLite, SQLCipher and Sybase.
Connect to Microsoft Access, FileMaker Server (or Pro), Microsoft Visual FoxPro and others via ODBC.
As field and table schema functions are not implemented, you can't use this database with the database browser features in the Xojo IDE.
The plugin does not provide RecordCount on RecordSet class. For that you need to make a extra SELECT count(*) query.
With Xojo 2013r1, you only need a database server license from Xojo, Inc. if you use the SQLDatabaseMBS class. The SQLConnectionMBS class does not require this license. But some features like getting a recordset do need the license as they refer to the SQLDatabaseMBS class.
Please free all RecordSets and SQLCommand objects before you close the SQLConnection or the SQLDatabase. The plugin keeps references from RecordSets and SQLCommand to prevent automatic destruction of the database connection. If you close a database connection while you have RecordSets and SQLCommand in use, things may go wrong.
The plugin can cache the recordset locally. To enable you can call SQLCommandMBS.Cache or use the Option("AutoCache") = "true" on either command or connection or database objects. The plugin will than fetch all records and store them in memory. After this you can walk over the recordset and use FetchPos, FetchFirst, FetchLast, FetchPrev and FetchNext to locate the rows you need. When you call Field() you always get last row, but to read from cached result set, please use Value() function. When using RecordSet, the values are read via Value() functions automatically.
You can use InternalPostgreSQLLibraryMBS or InternalSQLiteLibraryMBS if you like to use our built in SQLite or PostgreSQL database libraries.
The class pings the database every minute by checking whether it's alive and to avoid server dropping connection. This can be disabled by setting Option("Ping") = "false". Ping is not used for SQLite.
MBS Plugin 21.1 adds support for Edit/Update methods in RecordSet and RowSet classes.
MBS Database connections are implemented via SQLConnectionMBS and SQLCommandMBS classes. We provide a thin layer on top with SQLDatabaseMBS class to make it compatible to the Xojo database class. And when you use SQLDatabaseMBS, you can always get the matching SQLConnectionMBS objet via Connection property. Instead of SQLCommandMBS class, you may just use SelectSQL/ExecuteSQL or older SQLSelect/SQLExecute functions.
We have a collection of library files here:
https://www.monkeybreadsoftware.de/xojo/download/plugin/Libs/
Subclass of the Database class.
- 4 events
- event DidConnect
- event PostgresNotification(NotificationName as string, PID as Integer, Extras as String)
- event Trace(traceInfo as Integer, SQL as string, Command as SQLCommandMBS)
- event WillConnect
- 18 properties
- property AutoCommit as Integer
- property Client as Integer
- property ClientVersion as Integer
- Property Connection as SQLConnectionMBS
- property isAlive as boolean
- property isConnected as boolean
- property IsolationLevel as Integer
- property LastStatement as String
- property Options as Dictionary
- property RaiseExceptions as Boolean
- property RowsAffected as Integer
- property Scrollable as Boolean
- property ServerVersion as Integer
- property ServerVersionString as string
- property SQLiteEncryptionKey as String
- property Tag as Variant
- property Option(name as string) as string
- 40 methods
- method BeginTransaction
- method CancelAllCommands
- method Commands as SQLCommandMBS()
- method Connect as boolean
- method ConnectMT as Boolean
- method Constructor(globals as SQLGlobalsMBS = nil)
- method CubeSQLLastInsertID as Int64
- method CubeSQLReceiveData(byref data as String, byref IsEndChunk as Boolean) as Boolean
- method CubeSQLSendData(data as MemoryBlock)
- method CubeSQLSendData(data as String)
- method CubeSQLSendEndData
- method InsertRecord(TableName as String, Record as Dictionary)
- method Listen
- method MySQLInsertID as Int64
- method Prepare(statement as string) as SQLPreparedStatementMBS
- method SetFileOption(name as string, file as folderitem)
- method SQLExecute(ExecuteString as string, CommandType as Integer)
- method SQLExecuteMT(ExecuteString as string, CommandType as Integer = 0)
- method SQLiteBackupFinish(Backup as SQLite3BackupMBS) as integer
- method SQLiteBackupInit(Dest as Variant, DestName as String, Source as Variant, SourceName as String) as SQLite3BackupMBS
- method SQLiteBackupPageCount(Backup as SQLite3BackupMBS) as integer
- method SQLiteBackupRemaining(Backup as SQLite3BackupMBS) as integer
- method SQLiteBackupStep(Backup as SQLite3BackupMBS, Pages as Integer) as integer
- method SQLiteConnectionHandle as Ptr
- method SQLiteEnableLoadExtension(OnOff as boolean)
- method SQLiteLastInsertRowID as Int64
- method SQLiteLibVersion as String
- method SQLiteLoadExtension(file as FolderItem, ByRef ErrorMessage as String) as Integer
- method SQLiteLoadExtension(path as String, ByRef ErrorMessage as String) as Integer
- method SQLiteMemoryHighwater(reset as boolean = false) as Int64
- method SQLiteMemoryUsed as Int64
- method SQLiteReKey(Key as String) as Integer
- method SQLiteSetBusyHandler(MaxAttempts as Integer = 5)
- method SQLiteSetBusyTimeout(TimeOutMS as Integer = 20)
- method SQLiteSetKey(Key as String) as Integer
- method SQLiteTableColumnMetaData(DBName as string, TableName as string, ColumnName as string, byref DataType as string, byref CollationSequence as string, byref NotNull as boolean, byref PrimaryKey as boolean, byref AutoIncrement as Boolean) as integer
- method SQLiteThreadsafe as integer
- method SQLSelect(SelectString as string, CommandType as Integer) as RecordSet
- method SQLSelectMT(SelectString as string, CommandType as Integer = 0) as RecordSet
- method UpdateRecord(TableName as String, Record as Dictionary, Keys as Dictionary)
- 50 constants
Constants
Constant | Value | Description |
---|---|---|
kOptionLibraryCubeSQL | "CUBESQL.LIBS" | One of the option constant to specify the library with the SetFileOption method. Tells the plugin where to find the library for CubeSQL. Only needed if you don't use InternalCubeSQLLibraryMBS module! more |
kOptionLibraryDuckDB | "DUCKDB.LIBS" | ConstantsGroup: Options to specify the library with SetFileOption Var con as SQLDatabaseMBS // your connection more |
SQLiteInMemory | "Sqlite::memory:" |
Connection string for SQLite for a new in-memory database.
Prefixed with SQLite to use directly. |
Isolation Levels
Constant | Value | Description |
---|---|---|
kANSILevel0 | 0 |
ANSI Level 0 |
kANSILevel1 | 1 |
ANSI Level 1 |
kANSILevel2 | 2 |
ANSI Level 2 |
kANSILevel3 | 3 |
ANSI Level 3 |
kLevelUnknown | -1 |
Unknown |
kReadCommitted | 1 |
Read committed. |
kReadUncommitted | 0 |
Read uncommitted. |
kRepeatableRead | 2 |
Repeatable read. |
kSerializable | 3 |
Serializable. |
kSnapshot | 4 |
Changes made in other transactions can not be seen. For Microsoft SQL Server. |
Values for autocommit property
Constant | Value | Description |
---|---|---|
kAutoCommitOff | 0 |
Autocommit is off. Example |
kAutoCommitOn | 1 |
Autocommit is on. Example |
kAutoCommitUnknown | -1 |
Autocommit unknown |
Command Types
Constant | Value | Description |
---|---|---|
kCommandTypeSQLStatement | 1 |
Command is an SQL statement. |
kCommandTypeSQLStatementRaw | 2 |
Command is an SQL statement that mustn't be interpreted by SQLAPI. |
kCommandTypeStoredProcedure | 3 |
Command is a stored procedure or a function. |
kCommandTypeUnknown | 0 |
Used by default. Library detects command type automatically. |
Error Codes
Constant | Value | Description |
---|---|---|
kErrorBindVarNotFound | 7 |
Bind variable not found. |
kErrorClientInitFails | 6 |
Initialization failed for client. |
kErrorClientNotSet | 1 |
Client not set. |
kErrorClientNotSupported | 2 |
Unsupported client type for this platform. |
kErrorClientVersionOld | 5 |
Library file is too old. |
kErrorFieldNotFound | 8 |
Field not found. |
kErrorGetLibraryVersionFails | 4 |
Failed to query library version. |
kErrorLoadLibraryFails | 3 |
Failed to load a library. For example path could be wrong or 32/64bit mismatch. |
kErrorNoMemory | 0 |
Out of memory. |
kErrorUnknownColumnType | 11 |
Unknown column type. |
kErrorUnknownDataType | 9 |
Unknown data type. |
kErrorUnknownParameterType | 10 |
Unknown parameter type. |
kErrorWrongConversion | 12 |
Failed to convert a value, e.g. string to number. |
kErrorWrongDatetime | 13 |
Can't convert text to date. |
Options to specify the library with SetFileOption
Constant | Value | Description |
---|---|---|
kOptionLibraryDB2 | "DB2CLI.LIBS" | Tells the plugin where to find the library for DB2. more |
kOptionLibraryFirebird | "IBASE.LIBS" | Tells the plugin where to find the library for FireBird (or Interbase). more |
kOptionLibraryInformix | "INFCLI.LIBS" | Tells the plugin where to find the library for Informix. more |
kOptionLibraryInterbase | "IBASE.LIBS" | Tells the plugin where to find the library for FireBird (or Interbase). more |
kOptionLibraryMySQL | "MYSQL.LIBS" | Tells the plugin where to find the library for MySQL (or MariaDB). more |
kOptionLibraryODBC | "ODBC.LIBS" | Tells the plugin where to find the library for ODBC. more |
kOptionLibraryOracle | "OCI8.LIBS" | Tells the plugin where to find the library for Oracle. more |
kOptionLibraryPostgreSQL | "LIBPQ.LIBS" | Tells the plugin where to find the library for PostgreSQL more |
kOptionLibrarySQLanywhere | "SQLANY.LIBS" | Tells the plugin where to find the library for SQL Anywhere. more |
kOptionLibrarySQLBase | "SQLBASE.LIBS" | Tells the plugin where to find the library for SQLBase. more |
kOptionLibrarySQLite | "SQLITE.LIBS" | Tells the plugin where to find the library for SQLite. Not needed if you use InternalSQLiteLibraryMBS module! more |
kOptionLibrarySybaseComn | "SYBCOMN.LIBS" | Tells the plugin where to find the library for Sybase. more |
kOptionLibrarySybaseCS | "SYBCS.LIBS" | Tells the plugin where to find the library for Sybase. more |
kOptionLibrarySybaseCT | "SYBCT.LIBS" | Tells the plugin where to find the library for Sybase. more |
kOptionLibrarySybaseIntl | "SYBINTL.LIBS" | Tells the plugin where to find the library for Sybase. more |
kOptionLibrarySybaseTCL | "SYBTCL.LIBS" | Tells the plugin where to find the library for Sybase. more |
Super class Database
- 7 properties
- property DatabaseName as String
- property Error as Boolean
- property ErrorCode as Integer
- property ErrorMessage as String
- property Host as String
- property Password as String
- property UserName as String
- 18 methods
- method AddRow(TableName as String, row as DatabaseRow)
- method AddRow(TableName as String, row as DatabaseRow, idColumnName as string = "") as Integer
- method BeginTransaction
- method Close
- method Commit
- method CommitTransaction
- method Connect
- method Connect as boolean
- method ExecuteSQL(sql As String, ParamArray values As Variant)
- method ExecuteSQL(sql As String, values() As Variant)
- method InsertRecord(TableName as String, Data as DatabaseRecord)
- method Prepare(statement as String) as PreparedSQLStatement
- method Rollback
- method RollbackTransaction
- method SelectSQL(sql As String, ParamArray values As Variant) as RowSet
- method SelectSQL(sql As String, values() As Variant) as RowSet
- method SQLExecute(ExecuteString as string)
- method SQLSelect(SelectString as string) as RecordSet
This class has no sub classes.
Some examples using this class:
- /SQL/SQLDatabaseMBS CubeSQL select version
- /SQL/SQLDatabaseMBS DuckDB
- /SQL/SQLDatabaseMBS MSSQL Connect
- /SQL/SQLDatabaseMBS MySQL Connect
- /SQL/SQLDatabaseMBS PostgreSQL
- /SQL/SQLDatabaseMBS PostgreSQL Notify and Listen
- /SQL/SQLDatabaseMBS SQLite Connect in Memory Database
- /SQL/SQLDatabaseMBS SQLite Fetch values
- /SQL/SQLDatabaseMBS SQLite insert record
- /SQL/SQLDatabaseMBS SQLite select version
Blog Entries
- 15th birthday of MBS SQL Plugin
- Using MBS SQL Plugin with PostgreSQL
- News from the MBS Xojo Plugins Version 21.1
- MonkeyBread Software Releases the MBS Xojo Plugins in version 21.1
- RowSet in MBS Xojo SQL Plugin
- Converting Xojo project to use MBS SQL Plugin
- Problems with killing Xojo threads with plugin calls.
- MBS Real Studio SQL Plugin and Oracle
- MBS Releases the MBS Real Studio Web Starter Kit in version 1.0
- Tip of the day: SQLite Cache
Xojo Developer Magazine
- 21.1, page 9: News
- 21.1, page 26: News from MBS Xojo Plugins, What's up with MonkeyBread Software by Stefanie Juchmes
- 18.5, page 10: News
- 14.1, pages 24 to 30: The MBS SQL Plugin, An alternative way to connect to databases by Christian Schmitz
- 13.4, page 11: News
- 12.3, page 10: News
- 11.6, page 40: ChartPart 3.0, Create charts and graphs within your Xojo applications by Kevin Cully
- 11.3, page 11: News
- 11.2, page 33: Windows Installing, Using Inno Setup to Create a Windows Installer App by Marc Zeedar
- 11.1, page 9: News
Videos
Release notes
- Version 24.5
- Added MYSQL_OPT_LOCAL_INFILE option for SQLConnectionMBS and SQLDatabaseMBS classes for MySQL connections.
- Version 24.3
- Added AddRow() variant to SQLDatabaseMBS with returning insert id.
- Version 24.0
- Added checks for SQLDatabaseMBS class to raise an exception if you try to close, SQLExecute or SQLSelect while a background thread runs a SQL query.
- Version 23.5
- Added RowsAffected property to SQLConnectionMBS and SQLDatabaseMBS classes.
- Version 23.0
- Fixed a problem with an OutOfBoundsException in Connect method of SQLDatabaseMBS class.
- Version 22.3
- Added "MariaDB:" prefix for SQLDatabaseMBS class.
- Version 22.2
- Added option for SQLDatabaseMBS and SQLConnectionMBS to enable quotes around automatic generated INSERT statements: db.Option("Quotes") = "yes".
- Added SQLiteConnectionHandle method to SQLConnectionMBS and SQLDatabaseMBS classes.
- Version 22.1
- Added BeginTransaction method for SQLDatabaseMBS to avoid SQL errors.
- Version 21.5
- Added CacheInsertStatement flag for SQLDatabaseMBS to cache insert statements for InsertRecord to speed those up.
The items on this page are in the following plugins: MBS SQL Plugin.
SQLConnectionMBS - SQLDataConsumerMBS