storing binary files to mySQL ???

steindl fritz python at floSoft.org
Mon Dec 16 04:05:11 EST 2002


hi list,

i try to store pictures in a blob field of mySQL


    file = open("c:\\picture.jpg", 'rb')
    picture = file.read()

    SQL ='INSERT INTO tbl_BLOB (attrName, attrValue) VALUES 
("nameOfPicture", ' + picture + ')'

    conn = CompatMysqldb.Connection("host", "username", "password", "db")
    curs = conn.cursor()
    curs.execute(SQL)

===============
Traceback (most recent call last):
  File "C:\Python21\o2db\o2dbBlobTest.py", line 84, in ?
    holeImage()
  File "C:\Python21\o2db\o2dbBlobTest.py", line 28, in holeImage
    print "recs modid = " + str(curs.execute(INS))
  File "C:\Python21\CompatMysqldb.py", line 247, in execute
    raise error, msg
mysqldb.error: (1064, "You have an error in your SQL syntax near '' at line 
1")
==============

there is no way to get this to work for me
it works well with textfiles instead of binaries (pictures)

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

i looked to the source of CompatMysql.py and found an example
where 'execute' takes the SQL string and the parameters extra
which i adapted like this


    SQL = "INSERT INTO tblblob (attrName, attrValue, objID) VALUES ('%s', 
'%?')"

    conn = CompatMysqldb.Connection("host", "username", "password", "db")
    curs = conn.cursor()
    curs.execute(SQL, [('nameOfPicture', picture)])


again this works for strings, integers, but i don't find a correct format 
parameter
either the error says
TypeError - an integer is required

or the error occurs 2 lines later in CompatMysql like this

=================
Traceback (most recent call last):
  File "C:\Python21\o2db\o2dbBlobTest.py", line 84, in ?
    testImage()
  File "C:\Python21\o2db\o2dbBlobTest.py", line 27, in testImage
    curs.execute(SQL, [('nameOfPicture', picture)])
  File "C:\Python21\CompatMysqldb.py", line 227, in execute
    af =af+self.__res.affectedrows()
AttributeError: 'None' object has no attribute 'affectedrows'
=================

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

does someone know how to save binary values to a blob field of mySQL


thx for any ideas and help



fritz
(-:fs)



More information about the Python-list mailing list