Auslesen und darstellen von JPG aus mySQL BLOB Spalten

  • Hallo zusammen,
    als absoluter SQL - Newbie möchte ich aus einer mySQL Datenbank - besser aus einem mySQL BLOB - FELD ein JPG (richtig) auslesen und wieder als jpg darstellen.
    Das Selbe hab ich auch mit PDF ´s und mit Office - Dateien vor.
    Ich nutze dazu die UDF von Progandy und hab was Datensäze anbelangt auch schon super Erfolge erzielt - nur halt eben nicht mit binary - Files auslesen.

    Zum Reinschreiben nehm ich den Querystring:

    $qstring = "insert into docs (idnamen, docstype, doc, docsize, language, docname) values ('19','jpg', load_file('/SQLproj/lesson.jpg'),'46277','spanisch','lesson.jpg')
    und schicke ihn an die DB_Query($qstring) Funktion.
    So weit so gut, das funktioniert prächtig und ich kann auch mit der mysql Workbench die BLOB - Zelle (doc) exakt auslesen und im Editor darstellen.
    Lediglich im Autoitscript versagen alle Versuche.
    Im Forum hab ich bisher leider nur einen Beitrag von jemandem gefunden, der besagt hat dass er es kann - aber leider halt nicht wie er das macht ?( .

    Wenn ich den Befehl :
    select * from docs where idnamen = '19' abschicke, bekomme ich zwar ein $array zurück habe aber z. B. mit FileWrite immer nur genau 8 kryptische Zeichen in meinem .jpg
    Hat irgendjemand eine Idee - oder besser noch ein Scriptschnipsel mit dem ich das ohne irgendwelche PHP-verrenkungen hinbekommen könnte ??

    LG

    autoitdidakt

    Einmal editiert, zuletzt von autoitdidakt (21. Februar 2014 um 07:48)

  • Ohne Skript können wir dir schlecht helfen.
    Poste es bitte mal.

    There's a joke that C has the speed and efficieny of assembly language combined with readability of....assembly language. In other words, it's just a glorified assembly language. - Teh Interwebz

    C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, you blow off your whole leg. - Bjarne Stroustrup
    Genie zu sein, bedeutet für mich, alles zu tun, was ich will. - Klaus Kinski

  • OK - damit hab ich´s mal versucht.
    Aber meine Frage ging eigentlich in die Richtung, ob es einer grundsätzlich weis, wie so was mit AutoIt realisiert wird

    .

    [autoit]

    #include <Array.au3>
    #include "mysql.au3"
    Global $scIP = "localhost"
    Global $scUser = "root"
    Global $scPass = "root"
    Global $scDB = "autoit"
    Global $Query, $rows, $array, $res, $pic
    $select = "select * from docs where iddocs = '1'"
    DB_Query($select, "select", "lesson")
    $pic = Binary($array[0][3])
    _ArrayDisplay($array)
    dim $jpgfile = FileOpen(@ScriptDir&"\mytestimg.jpg",17)
    FileWrite($jpgfile,$pic)
    FileClose($jpgfile)

    [/autoit][autoit][/autoit][autoit]

    Func DB_Query($qstring, $queryart, $tabelle)
    Local $MysqlConn, $connected, $errno, $Query, $mysqlrow, $lenthsStruct,$length, $fieldPtr, $data, $error="", $plashmsg = 0
    _MySQL_InitLibrary()
    If @error Then Exit MsgBox(0, 'ERROR', @error)
    $MysqlConn = _MySQL_Init()
    $connected = _MySQL_Real_Connect($MysqlConn, $scIP, $scUser, $scPass, $scDB)

    [/autoit][autoit][/autoit][autoit]

    ; ------------------------------------------------Fehlerausgabe, wenn die Verbindung nicht geklappt hat ---------------------------------------
    If $connected = 0 Then
    $errno = _MySQL_errno($MysqlConn)
    MsgBox(0,"Error:",$errno & @LF & _MySQL_error($MysqlConn))
    If $errno = $CR_UNKNOWN_HOST Then MsgBox(0,"Error:","$CR_UNKNOWN_HOST" & @LF & $CR_UNKNOWN_HOST)
    Endif
    $connected = _MySQL_Real_Connect($MysqlConn, $scIP, $scUser, $scPass, $scDB)
    If $connected = 0 Then Exit MsgBox(16, 'Connection Error', _MySQL_Error($MysqlConn))
    ;------------------------------------------------------------------------------------------------------------------------------------------------

    [/autoit][autoit][/autoit][autoit]

    $Query = $qstring
    MsgBox(0,"String","_MySQL_Real_Query("&$MysqlConn&","& $Query&")")
    _MySQL_Real_Query($MysqlConn, $Query)
    $error= _MySQL_Error($MysqlConn)
    If $error <> "" Then
    MsgBox(0,"FEHLER",$errno&": "&$error &" ")
    EndIf
    $res = _MySQL_Store_Result($MysqlConn)
    $fields = _MySQL_Num_Fields($res)
    $rows = _MySQL_Num_Rows($res)
    If $rows > 0 Then
    Dim $array[$rows][$fields]
    For $k = 1 To $rows
    $mysqlrow = _MySQL_Fetch_Row($res,$fields)
    $lenthsStruct = _MySQL_Fetch_Lengths($res)
    For $i = 1 To $fields
    $length = DllStructGetData($lenthsStruct, 1, $i)
    $fieldPtr = DllStructGetData($mysqlrow, 1, $i)
    $data = DllStructGetData(DllStructCreate("char[" & $length & "]", $fieldPtr), 1)
    $array[$k - 1][$i - 1] = $data
    Next
    Next
    EndIf

    [/autoit][autoit][/autoit][autoit]

    _MySQL_Free_Result($res)
    ; Verbindung beenden
    _MySQL_Close($MysqlConn)
    ; MYSQL beenden
    _MySQL_EndLibrary()
    Return
    EndFunc

    [/autoit]

    Ein Pic vom ausgegebenen Array hab ich angehängt.

    LG

    autoitdidakt

  • Guten Morgen liebe Gemeinde,

    mea culpa!
    Nachdem ich in meinem Beispielscript so ein bissl rumgespielt hab, hab ich meinen (Denk-) Fehler entdeckt!
    Er steckt in Zeile 51. Die Struktur darf bei JPG´s & Co natürlich nicht "CHR" sondern muss "BYTE" heißen :thumbup:

    So wird in meinem Script aus

    Zeile 51 -> $data = DllStructGetData(DllStructCreate("char[" & $length & "]", $fieldPtr), 1)
    die Zeile -> $data = DllStructGetData(DllStructCreate("BYTE[" & $length & "]", $fieldPtr), 1)

    schon lässt sich der Bytestrom in ein File schreiben :rock: .

    LG

    autoitdidakt

    Einmal editiert, zuletzt von autoitdidakt (21. Februar 2014 um 08:34)