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 Write blob to file
You find this example project in your Plugins Download as a Xojo (Real Studio) project file within the examples folder: /SQL/MySQL Write blob to file
This example is the version from Fri, 4th Aug 2016.
Project "MySQL Write blob to file.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(con, "Select fblob from test_tbl") // create command object // connect to database (mySQL in our example) // server: // port: 3306 // database: test // name: root // no password con.Connect(",3306@test","root","",SQLConnectionMBS.kMySQLClient) // associate a command with connection // connection can also be specified in SACommand constructor // Usage 1. Read whole BLob(s) into internal buffers // Select BLob from our test table cmd.Execute // fetch results row by row and print results while cmd.FetchNext // after fetching a row all Long/Lob fields are automatically read into internal buffers // just like other data types dim s as SQLStringMBS = cmd.Field("fblob").asBLob MsgBox "Size of BLob is "+str(s.GetLength)+" bytes" wend // Usage 2. Read BLob in pieces providing user callback for BLob data processing // Select blob from our test table cmd.Execute // do not automatically read this field into internal buffer (into corresponding SAField object) // we will provide a consumer for BLob fetching after FetchNext cmd.Field("fblob").LongOrLobReaderMode=SQLValueReadMBS.kLongOrLobReaderModeManual // fetch results row by row and print results dim Filename as string dim i as integer=0 while cmd.FetchNext i=i+1 Filename="fblob"+str(i)+".bin" dim f as FolderItem=SpecialFolder.Desktop.Child(Filename) dim d as new MyDataConsumer(f) // at that moment all fields are fetched except // those that set for manual retrieving // read them (fblob in our example) now if not cmd.Field("fblob").isNull then cmd.Field("fblob").ReadLongOrLob(d, 10*1024) // read 10 KB per block end if wend // commit changes on success con.Commit 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
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 MyDataConsumer Inherits SQLDataConsumerMBS
EventHandler Sub Write(PieceType as integer, data as string, Length as UInt32, BlobSize as UInt32) if PieceType = kFirstPiece or PieceType = kOnePiece then size = 0 b=file.CreateBinaryFile("") if b=nil then MsgBox "Failed to create the file!" Return end if end if b.Write data size=size+Length // show progress MsgBox str(size)+" bytes of "+str(BlobSize)+" read." if PieceType = kLastPiece or PieceType = kOnePiece then b.Close b=nil end if 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.

MBS FileMaker tutorial videos