DarkBoost Posted July 18, 2009 Share Posted July 18, 2009 I have been trying various methods of converting a JPG image to Binary/ASCII/HEX to then be used in a SQLite Table however when it is extracted out of the SQLite database it is not in its original format. I have searched the forums which touches on this topic but could not find a very simple solution eg. StringToBinary and BinaryToString looked promising however due to the conversion the file is not readable as a JPG. Any simple ideas here? Link to comment Share on other sites More sharing options...
Mobius Posted July 18, 2009 Share Posted July 18, 2009 Would it be possible for you to attach a before and after file? by that I mean the original jpg plus the content that has been pulled from the database and dumped to a file? Possibly some code. It might make troubleshooting a little easier. Link to comment Share on other sites More sharing options...
DarkBoost Posted July 18, 2009 Author Share Posted July 18, 2009 (edited) Here is an example using StringToBinary which allows the image to be included in the SQL table (without this it fails due to the unsupported characters). I am not sure how to convert this back to the JPG format. #include <SQLite.au3> #include <SQLite.dll.au3> Dim $fRead, $fOpen, $hQuery, $hRow $fRead = StringToBinary(FileRead(@ScriptDir & "\photo.jpg")) _SQLite_Startup() _SQLite_Open() _SQLite_Exec(-1, "CREATE TABLE test (photo);") _SQLite_Exec(-1, "INSERT INTO test VALUES ('" & $fRead &"');") _SQLite_Query(-1, "SELECT rowid, * FROM test WHERE rowid = 1;", $hQuery) _SQLite_FetchData($hQuery, $hRow) $fOpen = FileOpen(@ScriptDir & "\photo_new.jpg", 2) FileWrite($fOpen, BinaryToString($hRow[1])) _SQLite_ShutDown() updated example to include output. Edited July 18, 2009 by DarkBoost Link to comment Share on other sites More sharing options...
Mobius Posted July 18, 2009 Share Posted July 18, 2009 (edited) Really could have done with those before and after files... any way try this minor mod. #include <SQLite.au3> #include <SQLite.dll.au3> Dim $fRead, $fOpen, $hQuery, $hRow $fRead = Binary(FileRead(@ScriptDir & "\photo.jpg")) _SQLite_Startup() _SQLite_Open() _SQLite_Exec(-1, "CREATE TABLE test (photo);") _SQLite_Exec(-1, "INSERT INTO test VALUES ('" & $fRead &"');") _SQLite_Query(-1, "SELECT rowid, * FROM test WHERE rowid = 1;", $hQuery) _SQLite_FetchData($hQuery, $hRow) $fOpen = FileOpen(@ScriptDir & "\photo_new.jpg", 2) FileWrite($fOpen, Binary($hRow[1])) _SQLite_ShutDown() Ed: scratch this checked it and its fubar. Edited July 18, 2009 by Mobius Link to comment Share on other sites More sharing options...
Zedna Posted July 18, 2009 Share Posted July 18, 2009 You can also use _SQLite_Encode() Resources UDF Â ResourcesEx UDF Â AutoIt Forum Search Link to comment Share on other sites More sharing options...
DarkBoost Posted July 18, 2009 Author Share Posted July 18, 2009 (edited) @Zedna unless i am using the function incorrectly it does the same as StringToBinary and then there is the same issue of converting it back to JPG@Mobius it shouldn't matter which JPG you use however here is a BEFORE (left) and AFTER (right) Edited July 18, 2009 by DarkBoost Link to comment Share on other sites More sharing options...
DarkBoost Posted July 19, 2009 Author Share Posted July 19, 2009 I still have not found a solution to this, has anyone else? Link to comment Share on other sites More sharing options...
NELyon Posted July 19, 2009 Share Posted July 19, 2009 (edited) Try this. #include <SQLite.au3> #include <SQLite.dll.au3> Dim $fRead, $fOpen, $hQuery, $hRow $hHandle = FileOpen(@ScriptDir & "\photo.jpg", 16) $fRead = Binary(FileRead($hHandle)) _SQLite_Startup() _SQLite_Open() _SQLite_Exec(-1, "CREATE TABLE test (photo);") _SQLite_Exec(-1, "INSERT INTO test VALUES ('" & $fRead &"');") _SQLite_Query(-1, "SELECT rowid, * FROM test WHERE rowid = 1;", $hQuery) _SQLite_FetchData($hQuery, $hRow) $fOpen = FileOpen(@ScriptDir & "\photo_new.jpg", 2) FileWrite($fOpen, Binary($hRow[1])) _SQLite_ShutDown() FileClose($hHandle) FileClose($fOpen) EDIT: Forgot to mention, this works on my end. Edited July 19, 2009 by KentonBomb Link to comment Share on other sites More sharing options...
DarkBoost Posted July 19, 2009 Author Share Posted July 19, 2009 Worked for me too. Thank you! I thought using Binary would only convert the String > Binary which is why I was using the StringToBinary and BinaryToString Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now