Dova Kin
Dova Kin

Reputation: 65

sqlite3.OperationalError... what is wrong in my sqlite syntax?

Python 3 + tkinter and sqlite3

I'm doing a mock application to save a piece of text in sqlite

here's the function:

 def saveNote(self,note_id):

    conn = db.connect(fname)
    c = conn.cursor()
    safeTitle=self.newNoteTitle.get()
    safeContents=self.newNoteText.get("1.0",tk.END)
    safeLink=self.newNoteLink.get()
    safeRemarks=self.newNoteRemarks.get()
    conn.execute('UPDATE notes SET (title,contents,remarks,link,created,last_modified,notebook_id) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?) WHERE notes_id=5', (safeTitle, safeContents, safeRemarks, safeLink, 1))
    conn.commit()
        self.master.destroy()

When executing the function, I get this error:

 conn.execute('UPDATE notes SET (title,contents,remarks,link,created,last_modified,notebook_id) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?) WHERE notes_id=5', (safeTitle, safeContents, safeRemarks, safeLink, 1))
sqlite3.OperationalError: near "(": syntax error

I don't quite understand why the syntax is wrong...(I'm new to python)...can anyone help me spot the mistake?

Thanks

Upvotes: 1

Views: 949

Answers (1)

idjaw
idjaw

Reputation: 26570

I don't think your SQL statement is correct. From what you explained you are looking to insert data in to your table, so you want to actually use an INSERT statement and not UPDATE. I think you might want to do this instead:

INSERT INTO notes(title,contents,remarks,link,created,last_modified,notebook_id)
VALUES(INSERT_THE_VALUES_YOU_WANT_TO_INSERT_HERE)

If you are in fact looking to update existing data, then your syntax should look like this:

Just fill in the "" with the values you want to set

UPDATE notes
SET 
title = "",
contents = "",
remarkts = "",
link = "",
created = "",
last_modified = "",
notebook_id = ""
WHERE notes_id=5

To remove any confusion with the query I provided, you want to structure your query within your Python like this:

conn.execute('UPDATE notes SET title=?,contents=?,remarks=?,link=?,created=?,last_modified=?,notebook_id=?
WHERE notes_id=5', (safeTitle, safeContents, safeRemarks, safeLink, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 1)) 

Upvotes: 2

Related Questions