the dirty dozen: connecting to nearly any database with ... · mbs sql plugin • alternative...
TRANSCRIPT
![Page 1: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/1.jpg)
The Dirty Dozen: Connecting to Nearly Any Database with the MBS SQL Plugin
Christian Schmitz CEO Monkeybread Software
![Page 2: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/2.jpg)
MBS SQL Plugin• Alternative interface to databases for Xojo (Real Studio) • Two interfaces:
• SQLDatabaseMBS • based on Xojo's database class
• SQLConnectionMBS • our native interface for C++ library
• can be mixed.
![Page 3: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/3.jpg)
SQLDatabaseMBS• Based on Xojo's database class • Using RecordSet for query results • Using Prepared Statements • Using DatabaseRecord class for insert • No Edit/Update/Delete on RecordSet
![Page 4: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/4.jpg)
SQLConnectionMBS• Native Interface • SQLCommandMBS for prepared statement & results • SQLAPIMBS classes for API specific functions
• SQLite Backup • SQLite Encryption functions
![Page 5: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/5.jpg)
Supported databases• Centura SQLBase • DB2 • Firebird • Informix • InterBase • MariaDB • Microsoft Access • Microsoft SQL Server
• MySQL • ODBC • Oracle Database Server • PostgreSQL • SQL Anywhere • SQLite • SQLCipher • Sybase
![Page 6: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/6.jpg)
Features• Bring your own native library • MySQL / MariaDB
• Commercial, Open Source or Embedded Library • SQLite
• SQL Cipher Library • SpatiaLite Library • Built-in SQLite inside plug-in • Preinstalled SQLite on Mac/Linux
![Page 7: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/7.jpg)
Prepared Statements• Parameters by ID, by Index or by Name
• p.Bind("FirstName") = Value • With type or auto detect type • :1, :name or ? • Bind all via Dictionary
![Page 8: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/8.jpg)
Prepared Statements// by index dim r as PreparedSQLStatement = db.Prepare( "Insert into test_tbl(fid, fvarchar20) values(:1, :2)")
r.BindType(0, SQLPreparedStatementMBS.kTypeLong) r.BindType(1, SQLPreparedStatementMBS.kTypeString)
r.Bind(0, 12345) r.Bind(1, "Hello World by index")
r.SQLExecute
![Page 9: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/9.jpg)
Prepared Statements// by name dim sql as string = "Insert into test_tbl(fid, fvarchar20) values(:fid, :fvarchar20)" dim v as Variant = db.Prepare(sql) dim p as SQLPreparedStatementMBS = v
p.BindType("fid", SQLPreparedStatementMBS.kTypeLong) p.BindType("fvarchar20", SQLPreparedStatementMBS.kTypeString)
p.Bind("fid", 2345) p.Bind("fvarchar20", "Hello World by name")
p.SQLExecute
![Page 10: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/10.jpg)
Multithreading• Perform SQL Execute & SQL Select multithreaded • Connect multithreaded • In Xojo thread, keep GUI running
![Page 11: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/11.jpg)
BLOB Handling• Set/Get BLOB as Memoryblock or String • Stream data from/to file or stream • DataConsumerMBS
• get chunks of data in event • DataProviderMBS
• provide data in chunks via event
![Page 12: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/12.jpg)
RecordSet movements• Scrollable RecordSets • Optionally local caching of RecordSet • Move First, Next, Previous & Last
![Page 13: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/13.jpg)
Debugging• LastStatment property • Trace Events • See Parameters and Fields in Debugger
![Page 14: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/14.jpg)
Exceptions• SQLDatabaseMBS
• Error Property • Exceptions off by default
• SQLConnectionMBS • Error Property • Exceptions on by default
• Exceptions are better to make sure you get all errors!
![Page 15: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/15.jpg)
SQLite• Internal SQLite library • Supports AES128, AES256 and RC4 encryption. • Updated regularly to latest SQLite +SEE version. • SetBusyTimeout and SetBusyHandler • Added FTS5 and JSON extensions.
![Page 16: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/16.jpg)
PostgreSQL• Internal PostgreSQL library available for Mac/Linux • Use notifications to inform other clients:
• PostgresNotification event • Listen method
![Page 17: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/17.jpg)
Events• Trace event
• Log all SQL commands • DidConnect & WillConnect
• Log connections • Set Parameters for connection
• Working • Called while multithreading • Allows cancellation
![Page 18: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/18.jpg)
Example Connectdim con as new SQLConnectionMBS
// where is the library?con.Option(con.kOptionLibrarySQLite) = "/usr/lib/libsqlite3.0.dylib"
// connect to databasecon.Connect("test.db", "", "", SQLConnectionMBS.kSQLiteClient)
MsgBox "We are connected!"
Exception error as SQLErrorExceptionMBS MsgBox error.message
![Page 19: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/19.jpg)
Example Insertdim cmd as new SQLCommandMBS(con)
// Insert rowcmd.setCommandText("Insert into TestTable(id, Name) values(:1, :2)")
// bind by IDcmd.Param(1).setAsLong(2)cmd.Param(2).setAsString("Bob")
// Insert first rowcmd.Execute
![Page 20: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/20.jpg)
Example Insertdim cmd as new SQLCommandMBS(con)
// Insert rowcmd.setCommandText( _
"Insert into TestTable(id, Name) values(:id, :Name)")
// bind by Namecmd.Param("id").setAsLong(2)cmd.Param("Name").setAsString("Bob")
// Insert first rowcmd.Execute
![Page 21: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/21.jpg)
Example Query// create command objectdim cmd as new SQLCommandMBS(con, "Select id, Name from TestTable")
// Select from our test tablecmd.Execute
// fetch results row by row and print resultswhile cmd.FetchNext
dim fid as integer = cmd.Field("id").asLongdim fName as string = cmd.Field("Name").asStringValue
window1.Listbox1.AddRow str(fid), fNamewend
![Page 22: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/22.jpg)
Example Connectdim db as new SQLDatabaseMBS
// where is the library?con.Option(con.kOptionLibrarySQLite) = "/usr/lib/libsqlite3.0.dylib"
// connect to databasedb.DatabaseName = "sqlite:" + "test.db"
if db.Connect thenMsgBox "We are connected!"
end if
![Page 23: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/23.jpg)
Example Insertdim d as new DatabaseRecord
d.IntegerColumn("id")=2d.Column("someText")="test insert"d.BlobColumn("blob")="Just a test"
db.InsertRecord("TestTable", d)
if db.Error thenMsgBox db.ErrorMessage
end if
![Page 24: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/24.jpg)
Example Querydim r as RecordSet = db.SQLSelect("Select id, Name from TestTable")
// fetch results row by row and print resultswhile not r.EOF
dim fid as integer = r.Field("id").IntegerValuedim fName as string = r.Field("Name").StringValue
window1.Listbox1.AddRow str(fid), fName
r.MoveNextwend
![Page 25: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/25.jpg)
Intermix Interfaces• Query SQLConnectionMBS for SQLDatabaseMBS
• to use native APIs • Convert SQLCommandMBS to RecordSet
• e.g. for Reports • Run SQLSelect on SQLConnectionMBS for RecordSet
![Page 26: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/26.jpg)
Other Features• Bulk Row Fetch • Microsoft SQL from Mac/Linux via FreeTDS • Run Stored Procedures • use SQLite Library with ICU • AutoCommit on/off
![Page 27: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/27.jpg)
SQLite Shelldim arguments() as string
dim f as FolderItem = SpecialFolder.Desktop.Child("test.sqlite") dim o as FolderItem = SpecialFolder.Desktop.Child("out.csv")
arguments.Append "sqlite3" // path to app arguments.Append f.NativePath arguments.Append "-csv" // CSV export arguments.Append "-header" // Include header line arguments.Append "-cmd" // Output file arguments.Append ".output "+o.NativePath arguments.Append "-cmd" // Query arguments.Append "select * from Documentation;"
dim n as integer = InternalSQLiteLibraryMBS.Shell(arguments)
![Page 28: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/28.jpg)
MBS SQL Plugin• Free to Try
• Over 80 Examples • Cost: $149 USD • Platforms
• Desktop, Console & Web • Mac OS X, Windows, Linux, not for iOS • 64-bit, 32-bit and ARM
![Page 30: The Dirty Dozen: Connecting to Nearly Any Database with ... · MBS SQL Plugin • Alternative interface to databases for Xojo (Real Studio) • Two interfaces: • SQLDatabaseMBS](https://reader034.vdocuments.site/reader034/viewer/2022042213/5eb6ae4b4f89f9387157d342/html5/thumbnails/30.jpg)
See you in Berlin!
4/5th May 2017