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 Update values
Function:
You find this example project in your Plugins Download as a Xojo (Real Studio) project file within the examples folder: /SQL/MySQL Update values
This example is the version from Fri, 4th Aug 2016.
Project "MySQL Update values.rbp"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open() // create a test file dim f as FolderItem = SpecialFolder.Temporary.Child("logo.jpg") MsgBox f.AbsolutePath if not f.Exists then dim p as Picture = LogoMBS(500) f.SaveAsJPEG p end if // 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(con, "Update test_tbl set fblob = :fblob where fid =:1") // 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 // use first method of Long(Lob) binding - as a whole cmd.Param(1).setAsLong(1) // fid cmd.Param("fblob").setAsBLob(ReadWholeFile(f)) // update first row cmd.Execute // use second method of binding - user defined data provider dim data as new MyDataProvider(f) cmd.Param(1).setAsLong(2) cmd.Param("fblob").setAsBLob(data, 10*1024) // our provider to provide blob data from file, 10 K chunks // update second row cmd.Execute // at that moment Library will call user callback when needed // commit changes on success con.Commit MsgBox "Blob parameter bound, rows updated!" 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
Function ReadWholeFile(f as FolderItem) As string dim b as BinaryStream b=f.OpenAsBinaryFile if b=nil then MsgBox "Failed to open file!" Return "" end if dim s as string = B.Read(b.Length) Return s 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
Class MyDataProvider Inherits SQLDataProviderMBS
EventHandler Function Read(byref PieceType as integer, Length as UInt32) As string if (PieceType = kFirstPiece) then b=file.OpenAsBinaryFile size=0 if b=nil then MsgBox "Failed to open file for reading!" end if end if dim buf as string = b.Read(Length) size=size+lenb(buf) // count how much we read already // show progress MsgBox str(size)+" bytes of file read." if b.EOF then PieceType = kLastPiece b.Close b=nil end if return buf End EventHandler
Sub Constructor(f as FolderItem) file=f End Sub
Property Private Size As UInt64
Property Private b As BinaryStream
Property Private file As FolderItem
End Class
End Project

See also:

Feedback, Comments & Corrections

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




Links
MBS Xojo blog