16

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.

3
  • 5
    That is entirely correct. Did you meant conn.commit() instead? :-) Commented Dec 31, 2013 at 16:25
  • 1
    Please use cur.execute("insert into data values(?)", path), or do really trust that filenames will never contain a ' character? (Users can be tricky…) Commented Jan 1, 2014 at 18:14
  • 1
    This will show you the possible commands for connection and cursor. Check that you did not misspell or mix them: print("Connection functions:",dir(sqlite3.connect('::memory::'))) print("\n\n") print("Cursor functions:",dir(sqlite3.connect('::memory::').cursor())) Commented Mar 19, 2019 at 7:40

3 Answers 3

25

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

Sign up to request clarification or add additional context in comments.

Comments

13

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()

Comments

11

It's

conn.commit()
conn.close() //if you intend to close it afterwards

Explanation: The cursor is only used to pass instructions to sqlite, while you have to commit or close with the instance you've made to connect to your database.

1 Comment

Technically, it's because transactions are a feature of the connection overall, and not a cursor. There are cases where it makes sense to have multiple cursors open at once within a single transaction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.