Python Forum
Sqlite 3 Database is not updating after using commit()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sqlite 3 Database is not updating after using commit()
#1
Any help is appreciated.

Table name - Encounters

name   |encounters|PreEnc|PostEnc|
'Name1'|0         |20    |0      |
the values for encounters, PreEnc, PostEnc are default to 0.

Now I want to update the PostEnc with a number.

  import sqlite3

  conn = sqlite3.connect('Encounters.db')
  c = conn.cursor()
   
  c.execute("UPDATE Encounters SET PostEnc=? WHERE name=?", (sumlst,user))
  conn.commit()
  for row in c.execute("SELECT PostEnc FROM Encounters WHERE name = ?", [user]):
    print(row)
  for row in c.execute("SELECT PreEnc FROM Encounters WHERE name = ?", [user]):
    print(row)
sumlst = 12345
user = 'Name'
But PostEnc does not get updated and I get nothing with the print(row)
Reply
#2
it looks like your table is empty. Do you have data in it?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Oct-19-2020, 07:29 AM)buran Wrote: it looks like your table is empty. Do you have data in it?

Yes, I do. When doing this, name and PreEnc is filled. I want to fill PostEnc.
Reply
#4
It seems that the post contains only a small part of your code which makes it impossible to reproduce the bug. I guess the error is in the missing part. Can you write a small complete script that we can try with the samme behavior?
buran likes this post
Reply
#5
There is something with your data. Otherwise your code works for me

import sqlite3

setup_sql = """BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "Encounters" (
	"name"	TEXT,
	"encounters"	INTEGER,
	"PreEnc"	INTEGER,
	"PostEnc"	INTEGER
);
INSERT INTO "Encounters" ("name","encounters","PreEnc","PostEnc") VALUES ('John Doe',0,20,0);
COMMIT;"""

sumlst = 12345
user = 'John Doe'

conn = sqlite3.connect('Encounters_new.db')
c = conn.cursor()
c.executescript(setup_sql)
for row in c.execute("SELECT * FROM Encounters WHERE name = ?", [user]):
  print(row)   
c.execute("UPDATE Encounters SET PostEnc=? WHERE name=?", (sumlst, user))
conn.commit()
for row in c.execute("SELECT PostEnc FROM Encounters WHERE name = ?", [user]):
  print(row)
for row in c.execute("SELECT PreEnc FROM Encounters WHERE name = ?", [user]):
  print(row)
for row in c.execute("SELECT * FROM Encounters WHERE name = ?", [user]):
  print(row)  
Output:
('John Doe', 0, 20, 0) (12345,) (20,) ('John Doe', 0, 20, 12345)
Gribouillis likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
I ran the same lines of code on a seperate file and the same thing happens for me. It must be the database... Do you have any idea what it could be. I don't see anything wrong with it[Database].
Reply


Forum Jump:

User Panel Messages

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