storing binary files to mySQL - SOLUTION ...

steindl fritz python at floSoft.org
Tue Dec 17 14:29:28 EST 2002


... and some strange things

i use for inserting into the mysql DB _mysql.escape_string(picture_file)

the rest works fine

======================================================================

trying to surround the problem ;-) i did several things ...

----------------

i used a file like this (reading binary) -

[00155CC6] Loading Vxd = C:\PROGRA~1\ACER\POWERKEY\POWERKEY.VXD
[00155CC5] LoadSuccess = C:\PROGRA~1\ACER\POWERKEY\POWERKEY.VXD 

----------------

when i wrote it to mySQL it worked
BUT i was missing eight characters !!!
AND the result in the db looked like this -

[00155CC6] Loading Vxd = C:PROGRA~1ACERPOWERKEYPOWERKEY.VXD
[00155CC5] LoadSuccess = C:PROGRA~1ACERPOWERKEYPOWERKEY.VXD

what is missin'  --->  8  times `\`

----------------

so i tried a patch and added a backslash for each backslash -

for c in bin:
        if c == '\\': newBin += '\\'
        newBin += c 

and all goes well

----------------

but unfortunately this worked only for text files (read binary) and not for 
real binaries 

----------------

binaries still crashed at 'INSERT TO' with an mysql error

then i realised (don't ask me why and how) that it doesn't crash, when i 
run those program directly on the server, and not remote from a client as i 
did until then !?!

so i could save binaries to the DB - 
BUT the file was (in the case of my test picture) missing 14 chars and of 
course the picture was corrupted

opening the picture with text editor i counted - yes - 14 times '\'

i tried my patch from above - adding one more backslash for each - what 
solved the problem of missing chars, but the file was still damaged

-----------------

the solution with _mysql.escape_string(picture_file) does well

<btw>
does it make sense to 'escape' strings too ???
who knows, what users get into a string with the keyboard

-----------------

i think i need some rest to fully understand what's the difference between 
working on the server or on a client


again thanks for all hints

fritz
(-:fs)



More information about the Python-list mailing list