Platforms to show: All Mac Windows Linux Cross-Platform
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/SQLite Fetch rows bulk
This example is the version from Thu, 6th Apr 2016.
Project "SQLite Fetch rows bulk.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
// now add that file to database
dim con as SQLConnectionMBS
dim cmd as SQLCommandMBS
try
con = new SQLConnectionMBS // connection object
cmd = new SQLCommandMBS // create command object
// where is the library?
'con.Option(con.kOptionLibrarySQLite) = "/usr/lib/libsqlite3.0.dylib"
// connect to database
dim 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
con.Connect(path,"","",SQLConnectionMBS.kSQLiteClient)
// 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:
- /SQL/SQLite Benchmark vs REALSQLDatabase
- /SQL/SQLite Connect in Memory Database
- /SQL/SQLite Display Schema
- /SQL/SQLite Encryption Fetch values
- /SQL/SQLite Fetch values
- /SQL/SQLite Internal Library Compiler Options
- /SQL/SQLite select version with Trace events
- /SQL/SQLite Table Definition
- /SQL/SQLite with ICU
- /SQL/SQLite Write blob to file
The items on this page are in the following plugins: MBS SQL Plugin.