1

I have a database in python sqlite3 and I'm trying to delete rows from it depending on a value that I selected. After the execution of this code, I got the result that I want when I'm in this function. But the problem is that when I'm out of this function and try to print the database, the deletion query did not work but the one to add values did. Can anyone understand why?

def datamanip():
    selected = SecTree.focus()
    values = SecTree.item(selected, 'text')
    conn = sqlite3.connect('DataStore.db')
    c = conn.cursor()
    query='DELETE FROM Limits WHERE TypeCard=(?)'
    c.execute(query,(values,))
    c.execute("INSERT INTO Limits VALUES (:TypeCard,:CreaseMaxC,:CreaseMinC,:CreaseMaxA,:CreaseMinA,:WidthMaxC,:WidthMinC,:WidthMaxA,:WidthMinA)",
        {'TypeCard':values,
         'CreaseMaxC': w2data,
         'CreaseMinC': wdata,
         'CreaseMaxA': w4data,
         'CreaseMinA': w3data,
         'WidthMaxC': w6data,
         'WidthMinC': w5data,
         'WidthMaxA': w8data,
         'WidthMinA': w7data
         }
        )
    c.execute('SELECT * FROM Limits')
    records= c.fetchall()
    print(records)

EDIT:

The connection must be commited after making the deletion for the database.

  conn.commit()

solved the problem.

fatma cheour
  • 35
  • 1
  • 6
  • 2
    hi, interesting, perhaps the operation needs to be committed? – jspcal May 03 '21 at 17:44
  • @jspcal hey :) what do you mean by the operation must be committed? Can you explain more or have sources where I can read from? – fatma cheour May 03 '21 at 17:47
  • https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.commit, `The underlying sqlite3 library operates in autocommit mode by default, but the Python sqlite3 module by default does not.` – gold_cy May 03 '21 at 17:49
  • Like this: https://stackoverflow.com/questions/36243538/python-sqlite3-how-often-do-i-have-to-commit – JNevill May 03 '21 at 17:49
  • Thanks everyone it worked after the operation was committed :) – fatma cheour May 03 '21 at 17:52
  • @fatmacheour You can answer your own question for future visitors – Lucan May 03 '21 at 17:59

0 Answers0