Platforms to show: All Mac Windows Linux Cross-Platform

/SQL/SQLDatabaseMBS SQLite Test


Required plugins for this example: MBS SQL Plugin

Last modified Thu, 11th Jun 2025.

You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /SQL/SQLDatabaseMBS SQLite Test

Download this example: SQLDatabaseMBS SQLite Test.zip

Project "SQLDatabaseMBS SQLite Test.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open() // use internal sqlite library call InternalSQLiteLibraryMBS.Use var db as new SQLDatabaseMBS // where is the library? 'db.Option(SQLConnectionMBS.kOptionLibrarySQLite) = "/usr/lib/libsqlite3.0.dylib" // connect to database // in this example it is SQLite, // but can also be Sybase, Oracle, Informix, DB2, SQLServer, InterBase, MySQL, SQLBase and ODBC var path as string if TargetMacOS then path = "/tmp/test.db" // put the database in the temporary folder else path = "test.db" // for Windows and Linux in the current folder the application is inside. End If // or in-memory? path = ":memory:" db.DatabaseName = "sqlite:"+path if db.Connect then System.DebugLog "We are connected!" // check SQLite version if true then Dim r As RecordSet = db.SQLSelect("select sqlite_version()") If r = Nil Or r.eof Then System.DebugLog "Failed to query version." Else System.DebugLog "Version: "+r.IdxField(1).StringValue End If end if // create table db.SQLExecute "CREATE TABLE IF NOT EXISTS test_tbl(fid integer, fvarchar20 varchar(20), fblob blob)" // insert value db.SQLExecute "Insert into test_tbl(fid, fvarchar20) values (1, 'Some string (1)')" // insert with prepared statements by index Dim ps As SQLPreparedStatementMBS = db.Prepare("Insert into test_tbl(fid, fvarchar20) values(:1, :2)") ps.BindType(0, ps.kTypeLong) ps.BindType(1, ps.kTypeString) ps.SQLExecute 12345, "Hello World by index" // by name Dim sql As String = "Insert into test_tbl(fid, fvarchar20) values(:fid, :fvarchar20)" Dim p As SQLPreparedStatementMBS = db.Prepare(sql) p.BindType("fid", p.kTypeLong) p.BindType("fvarchar20", p.kTypeString) p.Bind("fid", 2345) p.Bind("fvarchar20", "Hello World by name") p.SQLExecute // insert with DatabaseRecord var d As New DatabaseRecord d.IntegerColumn("fid")=2 d.Column("fvarchar20")="test insert" d.BlobColumn("fblob")="Just a test" db.InsertRecord("test_tbl", d) If db.Error Then MsgBox db.ErrorMessage End If // insert with DatabaseRow var NewRow As New DatabaseRow NewRow.Column("fid")=3 NewRow.Column("fvarchar20")="another insert" NewRow.Column("fblob")="Hello" db.AddRow("test_tbl", NewRow) If True Then // now query with RowSet Var r As RowSet = db.SelectSQL("Select fid, fvarchar20 from test_tbl") // fetch results row by row and print results Var list As listbox = MainWindow.List While Not r.AfterLastRow Var fid As Integer = r.Column("fid").IntegerValue Var fvarchar20 As String = r.Column("fvarchar20").StringValue list.AddRow Str(fid), fvarchar20 r.MoveToNextRow Wend else // now query with RecordSet var r As RecordSet = db.SQLSelect("Select fid, fvarchar20 from test_tbl") // fetch results row by row and print results var list as listbox = MainWindow.List While Not r.EOF var fid As Integer = r.Field("fid").IntegerValue var fvarchar20 As String = r.Field("fvarchar20").StringValue list.AddRow Str(fid), fvarchar20 r.MoveNext Wend End If // Disconnect is optional // autodisconnect will ocur in destructor if needed db.close System.DebugLog "We are disconnected!" end if End EventHandler
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Sign
End Sign
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
End Class
End Project

See also:

Download this example: SQLDatabaseMBS SQLite Test.zip

The items on this page are in the following plugins: MBS SQL Plugin.


The biggest plugin in space...