Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deleting data in sqlite3
#1
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.
Reply
#2
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
Reply
#3
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()		
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,304 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Data is deleting korotkovap 1 1,688 Aug-16-2020, 06:57 AM
Last Post: buran
  Adding data to a table in SQLite3 djwilson0495 2 3,001 Aug-15-2020, 02:48 PM
Last Post: djwilson0495
  Find data using a period of time in SQLITE3 SmukasPlays 2 2,144 Jul-30-2020, 02:02 PM
Last Post: SmukasPlays
  Deleting data in SQlite3 SmukasPlays 0 1,429 Jul-25-2020, 02:07 PM
Last Post: SmukasPlays
  sqlite3 database does not save data across restarting the program SheeppOSU 1 3,405 Jul-24-2020, 05:53 AM
Last Post: SheeppOSU
  Deleting a row in SQlite3 SmukasPlays 3 2,003 Jul-23-2020, 05:30 AM
Last Post: ndc85430
  How can I add some data from file.txt to db.sqlite3 ? Mike Ru 1 3,043 Nov-19-2018, 05:35 AM
Last Post: Mike Ru

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020