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/SQLite Update values
This example is the version from Thu, 31th Jul 2019.
Project "SQLite Update values.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
// 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
cmd = new SQLCommandMBS(con, "Update test_tbl set fblob = :fblob where fid =:1") // 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
// 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:
- /SQL/SQLite Backup
- /SQL/SQLite Connect in Memory Database
- /SQL/SQLite Display Schema
- /SQL/SQLite Encryption Fetch values
- /SQL/SQLite Fetch rows bulk
- /SQL/SQLite Fetch values with Recordset
- /SQL/SQLite Internal Library Compiler Options
- /SQL/SQLite Lock Test/SQLite Busy Handler
- /SQL/SQLite select version
- /SQL/SQLite Table Definition
The items on this page are in the following plugins: MBS SQL Plugin.