Python Forum

Full Version: Deleting data in sqlite3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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
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()		
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