Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is commit used on the connect or cursor?

The book named "Practical Programming: 2nd Edition" has conflicting code. This is the start of my code:

import sqlite3

con = sqlite3.connect('stackoverflow.db')    
cur = conn.cursor()

To commit, would I use con.commit(), cur.commit() or are there different times to use each? From the book:

con.commit() :

con commit

cur.commit() :

cur.commit()

Documentation shows con.commit() :

enter image description here

like image 839
Yabusa Avatar asked Oct 23 '25 10:10

Yabusa


1 Answers

I took unutbu's advice and tried it myself.

Sample code:

import sqlite3

con = sqlite3.connect('db.db')
cur = con.cursor()

data = [('data', 3), ('data2', 69)]

cur.execute('CREATE TABLE Density(Name TEXT, Number INTEGER)')

for i in data:
    cur.execute('INSERT INTO Density VALUES (?, ?)', (i[0], i[1]))

cur.commit()

PyCharm Run:

Traceback (most recent call last):
  File "/Users/User/Library/Preferences/PyCharmCE2018.1/scratches/scratch_2.py", line 13, in <module>
    cur.commit()
AttributeError: 'sqlite3.Cursor' object has no attribute 'commit'

Error in textbook. cur.commit() does not exist.

Thanks unutbu and s3n0

like image 154
Yabusa Avatar answered Oct 25 '25 22:10

Yabusa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!