Python Forum
Deleting data in sqlite3 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Deleting data in sqlite3 (/thread-19324.html)



Deleting data in sqlite3 - JJ39 - Jun-23-2019

Do not delete the rows from the table. There are no errors.
dsh11 = dsh[0]
all_table = var.get()
cursor.execute('DELETE FROM {} WHERE ? = 1'.format(all_table), (dsh11,))
dsh 11 gets the name of the first column of each table.


RE: Deleting data in sqlite3 - noisefloor - Jun-23-2019

Hi,

first, do not use String-formatting on SQL statements. It's a safety concern and bad style. Except this, in a well-written program this shouldn't be necessary anyway.
Second, as long as your database is not in auto-commit mode, you need to call commit on the connection object.

Regards, noisefloor


RE: Deleting data in sqlite3 - JJ39 - Jun-23-2019

noisefloor,
How can I replace the formatting?
Adding commit did not bring results.
    dsh11 = dsh[0]
	all_table = var.get()
	cursor.execute('DELETE FROM {} WHERE ? = 1'.format(all_table), (dsh11,))
	print (all_table,dsh11)
	conn.commit()		



RE: Deleting data in sqlite3 - noisefloor - Jun-23-2019

Hi,

Quote:How can I replace the formatting?
Hardcode the table name into the SQL statement.

What I've overseen initially is another mistake: you can _not_ have the column name as a variable as well. The replacement acc. to the Python DB API 2.0 is only for values. like c.execute('DELETE FROM foo wHERE bar = ?', ('spamegg', ))

In case you feel you need this "flexibility" on queries, your database / table design is most likely "not too good" aka need improvement.

Regards, noisefloor