Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

Platforms to show: All Mac Windows Linux Cross-Platform

/SQL/MySQL Fetch rows bulk
Function:
Required plugins for this example: MBS SQL Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /SQL/MySQL Fetch rows bulk
This example is the version from Fri, 4th Aug 2016.
Project "MySQL Fetch rows bulk.rbp"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open() // now add that file to database dim con as SQLConnectionMBS dim cmd as SQLCommandMBS try con = new SQLConnectionMBS // connection object // where is the library? // you can place the database client library files where you want. // example code just has some convenient location for testing. con.SetFileOption con.kOptionLibraryMySQL, SpecialFolder.UserHome.Child("libmysqlclient.dylib") cmd = new SQLCommandMBS // create command object // connect to database (mySQL in our example) // server: 192.168.1.80 // port: 3306 // database: test // name: root // no password con.Connect("192.168.1.80,3306@test","root","",SQLConnectionMBS.kMySQLClient) // associate a command with connection // connection can also be specified in SACommand constructor cmd.Connection=con // Create and Insert test table if HaveSampleData(cmd)=False then CreateSampleData(cmd, 200000) con.Commit end if dim start, finish as integer MsgBox "Reading data without bulk support (default)..." start=ticks cmd.setCommandText "Select * from TEST_BULK" cmd.Execute while cmd.FetchNext // go over all rows wend finish=ticks MsgBox "Time in seconds: "+str((finish-start)/60.0)+" seconds." dim nBulkSize as integer = 1000 msgbox "Reading data with bulk support (Size="+str(nBulkSize)+")..." start=ticks cmd.setCommandText "Select * from TEST_BULK" cmd.Option("PreFetchRows") = str(nBulkSize) cmd.Execute while cmd.FetchNext // go over all rows wend finish=ticks MsgBox "Time in seconds: "+str((finish-start)/60.0)+" seconds." catch r as SQLErrorExceptionMBS // SAConnection::Rollback() // can also throw an exception // (if a network error for example), // we will be ready try // on error rollback changes if con<>nil then con.rollback end if catch x as SQLErrorExceptionMBS // ignore end try // show error message MsgBox r.message end try End EventHandler
Sub CreateSampleData(cmd as SQLCommandMBS, nRows as integer) MsgBox "Creating test table..." cmd.setCommandText("CREATE TABLE TEST_BULK (FINTEGER INTEGER NOT NULL, FVARCHAR20 VARCHAR(20), PRIMARY KEY (FINTEGER))") cmd.Execute MsgBox "Populating test table (rows="+str(nrows)+")..." cmd.setCommandText "BEGIN" cmd.Execute cmd.setCommandText("Insert into TEST_BULK (FINTEGER, FVARCHAR20) values (:1, :2)") for i as integer = 1 to nRows cmd.Param(1).setAsLong i cmd.Param(2).setAsString "VC"+str(i) cmd.Execute next cmd.setCommandText "COMMIT" cmd.Execute MsgBox str(nRows)+" inserted." End Sub
Function HaveSampleData(cmd as SQLCommandMBS) As Boolean try cmd.setCommandText "Select * from TEST_BULK" cmd.Execute if cmd.FetchNext then Return true end if catch s as SQLErrorExceptionMBS Return false // no table TEST_BULK end try End Function
End Class
Class Window1 Inherits Window
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
End Project

See also:

Feedback, Comments & Corrections

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




Links
MBS Xojo Plugins