I have an issue while using sqlite3 with PyQt4 while accessing a database. I have a form, from which I read the string:
Name = str(self.PopupWidget.QLineEdit_field.text().toUtf8()).strip()
The string I entered into the QLineEdit_field is "ą", which translates to 0xc4 0x85 in unicode. Now, I create a table:
db.execute("CREATE TABLE table_name(field1 TEXT, field2 REAL,....);".replace('table_name',Name))
db.commit()
This works perfectly fine. Then I insert the data into the table:
db.execute("INSERT INTO '%s' VALUES ('%s','%f',...);" %(Name,data1,data2...))
This also works fine (it is being displayed in my widget as well as in an external database explorer). When I try to extract a single field from the table:
db.execute("select field1 from '%s';" %(Name))
records = [a[0]for a in db.fetchall()]
It also works perfectly. But then I try to access all data from a single row in the table:
db.execute("SELECT * FROM '%s' WHERE field1 = '%s';" %(Name,data1))
And this leads to the error:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)
I tried different approaches, however nothing worked and it always resulted in this error. What could be the cause of this?
::::SOLVED::::
...kinda. When I tried to use the same syntax as when creating the table:
db.execute("SELECT * FROM table1 WHERE field1 = 'table2';".replace('table1',Name).replace('table2',data1))
it worked properly. I have no idea why, and I don't think that is in any way a valid solution, but at least it worked.