I'm trying to store image data (screenshots) in SQLite database.
now = int(math.floor(time.time())) ba = QByteArray() buff = QBuffer(ba) image.save(buff, format) params = (str(ba.data()), "image/%s"%format, now, url) s_conn = sqlite.connect("cache/screenshots_%s.db"%row['size']) s_curs = s_conn.cursor() s_curs.execute("UPDATE screenshots SET data=?, mime=?, created=? WHERE filename=?", params)This code gives me error "TypeError: not all arguments converted during string formatting" Any manipulation with QByteArray (incl. converting it to Qstring) gives me this error, or ascii to utf-8 conversion error. I've Googled this issue for about 2 days and every advice was incorrect for me. How can I work it around?
preguntado el 09 de enero de 11 a las 03:01
The biggest issue is that you are trying to store binary as a string by calling str(ba.data). If you do this then it will not be a valid string and will cause endless grief for you later. Behind the scenes SQLite uses Unicode for all strings. However it does not check that a provided string is valid unicode (UTF8/16). Consequently you can insert binary garbage pretending it is a string but when trying to retrieve it will fail dismally since it won't convert to Unicode.
SQLite has a binary type (named BLOB) and that is exactly what you should be using. The way you provide a binary/blob binding is dependent on the SQLite wrapper you are using. It looks like you are using PySQLite or SQLite 3. For Python 2 use buffer and for Python 3 use bytes.
# Python 2 params=( buffer(ba.data()), ...) # Python 3 params=( bytes(ba.data()), ...)