Hi, I need to store som PDF uploaded to a db. They will be in a BLOB field.
I set mb as memoryblock
for each file as webuploadedFile in files…
mb = file.data
then I do a sqlExecute with this…
but I get undefined operator…
sql = “INSERT INTO skjema (navn, skjemaPDF) VALUES ( ‘test’, '”+mb+"’)"
any ideas…?
What is the best way to do this…
I write and read files to and from databases. I use binarystream to read the file into a string and then EncodeBase64 on the string before sending it to the database, and vice versa.
[code]Function GetStringFromFolderItem(f As FolderItem) As String
//open the folderitem as a binary file without write privileges
//To open with write privileges, pass true instead of false
Const readOnly = False
Dim oFile As String
Try
Dim bs As BinaryStream = BinaryStream.Open(f, readOnly)
//read the whole binaryStream
oFile = bs.read(bs.length)
Catch e As IOException
// Your error handling here
End Try
If oFile <> “” Then
Return EncodeBase64(oFile,0)
Else
Return “”
End If
Exception err
If err IsA OutOfBoundsException Then
// Your error handling here
Return “”
End If
End Function
[/code]
And the convert the string back into folderitem:
[code]Function GetFolderitemFromString(oFile As String, fTarget As FolderItem) As Boolean
// Purpose: Write a file back to disk, which was
// stored in a database beforehand
// (using GetStringFromFolderItem)
If fTarget = Nil Then Return False
If oFile <> “” Then
// Decode and Decompress the file to a string
Dim fStr As String
fStr=DecodeBase64(oFile)
//create a binary file
Dim bs As BinaryStream = BinaryStream.Create(fTarget, True) // True = overwrites existing file
//make sure we have a binary stream to write to it
If bs <> Nil Then
Try
// write the whole binaryStream
bs.write(fStr)
//close the binaryStream
bs.close
Return True
Catch exc as IOException
// Your error handling here
Return False
End Try
Else
Return False
End If
End If
Exception err
If err IsA OutOfBoundsException Then
// Your error handling here
Return False
ElseIf err IsA IOException Then
// Your error handling here
Return False
End If
End Function[/code]
You don’t want to do it this way with with binary MemoryBlock data.
To insert a BLOB value, you have these choices:
- Use a PreparedStatement
- Use DatabaseRecord.BlobColumn
- Use the SQLiteBlob class
Hi paul, you’re right. I’ll not use an insert with a memoryblock…
I’ll use the direct API for the valentina db. then I can write a blob field directly. More or less the same as your 2nd option.
Thanks.