python-mysqldb insert image int value error

时间:2014-11-14 19:43:29

标签: python mysql raspberry-pi mysql-python

只是尝试用覆盆子pi拍照并将实际的日期时间和图片插入到mysql数据库中......我试图插入日期时间......很好。但插入图像时出错......

          image = open(picPath+filename, 'r')
            pic = image.read()
            mysql = MySQLdb.connect(host="local", user="root", passwd="bla", db="sexyimages")
            cur = mysql.cursor()
            sql = ("insert into test(SampleTime, picture) values(%s, %s)")
            cur.execute(sql, (jetzt, pic))
            mysql.commit()

警告:不正确的整数值:'???? c?Exif'代表第1行的'picture'列   cur.execute(sql,(jetzt,pic))

有人有想法吗?

哦,你使用python-mysqldb

cheeers

1 个答案:

答案 0 :(得分:0)

根据this answer我发现你必须首先关闭文件指针并使用MySQL.escape_string方法转义字符串。试试这个:

image = open(picPath+filename)
pic = image.read()
image.close()

mysql = MySQLdb.connect(host="local", user="root", passwd="bla", db="sexyimages")
cur = mysql.cursor()
cur.execute("insert into test SET SampleTime='%s', picture='%s'") %(jetzt, MySQLdb.escape_string(pic))

mysql.commit()