Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python sqlite3 cursor has no attribute commit

Tags:

python

sqlite

When I run this code:

path = '~/Scripts/wallpapers/single.png'
conn = sqlite3.connect('/Users/Heaven/Library/Application Support/Dock/desktoppicture.db')
cur = conn.cursor();
cur.execute("insert into data values ('" + path + "');")
cur.commit()

I receive the following error

AttributeError: 'sqlite3.Cursor' object has no attribute 'commit'

and I have absolutely no idea why.

like image 680
Kamran224 Avatar asked Dec 31 '13 16:12

Kamran224


People also ask

How do I connect sqlite3 to Python?

Connect To Database#!/usr/bin/python import sqlite3 conn = sqlite3. connect('test. db') print "Opened database successfully"; Here, you can also supply database name as the special name :memory: to create a database in RAM.

What is the correct way to install sqlite3 in Python?

Step 1 − Go to SQLite download page, and download precompiled binaries from Windows section. Step 2 − Download sqlite-shell-win32-*. Step 3 − Create a folder C:\>sqlite and unzip above two zipped files in this folder, which will give you sqlite3.

How fetch data from sqlite3 database in Python?

You can fetch data from MYSQL using the fetch() method provided by the sqlite python module. The sqlite3. Cursor class provides three methods namely fetchall(), fetchmany() and, fetchone() where, The fetchall() method retrieves all the rows in the result set of a query and returns them as list of tuples.


2 Answers

commit() is a member method of sqlite3.Connection not sqlite3.Cursor. Here it is in the docs.

like image 98
Charlie Andrews Avatar answered Oct 19 '22 05:10

Charlie Andrews


From the answer from sir_charles804 (sorry not enough points to add this as comment) follows it should be:

conn.commit()

instead of

cur.commit()
like image 12
NumesSanguis Avatar answered Oct 19 '22 05:10

NumesSanguis