Xojo Conferences
MBSSep2018MunichDE
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/SQL/SQLDatabaseMBS MySQL Dump
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/SQLDatabaseMBS MySQL Dump
This example is the version from Sun, 4th Apr 2015.
Project "SQLDatabaseMBS MySQL Dump.rbp"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control Thread1 Inherits Thread
ControlInstance Thread1 Inherits Thread
EventHandler Sub Run() self.run End EventHandler
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action() while UBound(ForListbox)>=2 List.AddRow ForListbox(0), ForListbox(1), ForListbox(2) ForListbox.Remove 0 ForListbox.Remove 0 ForListbox.Remove 0 List.ScrollPosition = List.ListCount wend if GotException<>Nil then dim e as SQLErrorExceptionMBS = GotException GotException = nil MsgBox e.Message end if if Thread1.State = Thread.NotRunning then // done Title = "MySQL Dump: "+str(List.ListCount)+" tables." me.Mode = 0 StartButton.Enabled = true end if End EventHandler
End Control
Control StartButton Inherits BevelButton
ControlInstance StartButton Inherits BevelButton
EventHandler Sub Action() List.DeleteAllRows Title = "MySQL Dump..." me.Enabled = false Thread1.run timer1.Mode = 2 End EventHandler
End Control
EventHandler Sub Open() folder = SpecialFolder.Desktop.Child("Dump") folder.CreateAsFolder End EventHandler
Sub FetchDatabase(DatabaseName as string) db.SQLExecute "use "+DatabaseName dim TablesRecordSet as RecordSet = db.SQLSelect("show tables") // create command object while not TablesRecordSet.EOF // fetch results row by row and print results dim TableName as string = TablesRecordSet.IdxField(1).StringValue FetchTable DatabaseName, TableName TablesRecordSet.MoveNext wend End Sub
Sub FetchDatabases() dim DatabasesRecordSet as RecordSet = db.SQLSelect("show databases") // create command object while not DatabasesRecordSet.EOF // fetch results row by row and print results dim DatabaseName as string = DatabasesRecordSet.Field("Database").StringValue FetchDatabase DatabaseName DatabasesRecordSet.MoveNext wend End Sub
Sub FetchTable(DatabaseName as string, TableName as string) dim file as FolderItem = folder.Child(DatabaseName+" "+TableName) dim out as TextOutputStream = TextOutputStream.Create(file) dim tab as string = encodings.UTF8.Chr(9) dim r as RecordSet = db.SQLSelectMT("select * from "+TableName) // create command object dim names() as string dim isBlob(0) as Boolean dim c as integer = r.FieldCount for i as integer = 1 to c dim f as DatabaseField = r.IdxField(i) names.Append f.Name next out.WriteLineUTF8 Join(names, tab) dim n as integer = 0 while not r.EOF // fetch results row by row and print results dim parts() as string for i as integer = 1 to c dim f as DatabaseField = r.IdxField(i) dim s as string = f.StringValue if s.Encoding = nil then parts.Append "Blob "+str(lenb(s))+" bytes." else parts.Append s end if next out.WriteLineUTF8 Join(parts, tab) n = n + 1 r.MoveNext wend ForListbox.Append DatabaseName ForListbox.Append TableName ForListbox.Append str(n) End Sub
Sub Run() db = new SQLDatabaseMBS // where is the library? // you can place the database client library files where you want. // example code just has some convenient location for testing. #if TargetMachO then db.SetFileOption SQLConnectionMBS.kOptionLibraryMySQL, SpecialFolder.UserHome.Child("libmysqlclient.18.dylib") #elseif TargetWin32 then db.SetFileOption SQLConnectionMBS.kOptionLibraryMySQL, SpecialFolder.UserHome.Child("libmysql.dll") #else Break // todo? #endif // connect to database // in this example it is MySQL, // but can also be Sybase, Informix, DB2, SQLServer, InterBase, Oracle, SQLite, SQLBase and ODBC db.DatabaseName="mysql:12.34.56.78@test" db.UserName="xxx" db.Password="xxx" db.RaiseExceptions = true // lets get exceptions to show errors if db.Connect then db.Scrollable = true FetchDatabases end if Exception r as SQLErrorExceptionMBS GotException = r End Sub
Property ForListbox() As string
Property GotException As SQLErrorExceptionMBS
Property db As SQLDatabaseMBS
Property folder As FolderItem
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
Module Module1
Sub WriteLineUTF8(extends t as TextOutputStream, s as string) t.WriteLine ConvertEncoding(s, encodings.UTF8) End Sub
End Module
End Project

See also:

Feedback, Comments & Corrections

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




Links
MBS Xojo PDF Plugins