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.
Nameinto your query and follow the process I listed recently here? You do not follow standards forSQLite3in building the queries and so it might be down to string formatting thatdata1is causing the errors. i.e. placeholder fordata1.data1is surely not causing errors, it is a date in format: yyyy-mm-dd, so there's no error there.("CREATE TABLE table_name(field1 TEXT, field2 REAL,....);")creates a table calledtable_name(Ok, I get that's a generic name) but then all your other queries somehow read and write to a table presumably called "ą" (Name) without any errorprintstatements in the wider picture. If indeed you're able to do everything you say you can do then I'm at a loss on why the last part fails. Your query strings are not standard practice so it might help to get it in line with my original link and stackoverflow.com/questions/39588293/…. I can't be sure that my approach is the best practice but it's closer than your current one