Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Main Plugin, MBS SQL Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /SQL/MySQL Update values
This example is the version from Thu, 31th Jul 2019.
Project "MySQL Update values.xojo_binary_project"
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.NativePath
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:
The items on this page are in the following plugins: MBS SQL Plugin.