Python Forum

Full Version: sql error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
Do you see why I get the error:
Error:
cursor.execute("""INSERT INTO Books(ID, Title, Author, DatePublished) VALUES("1", "De Profundis", "Oscar Wilde", "1905")""") sqlite3.OperationalError: table Books has no column named DatePublished
------------------------------------------------------------------------------------------------------------------------------
python code is pasted below:

import sqlite3

with sqlite3.connect("BookInfo.db") as db:
    cursor = db.cursor()

cursor.execute(""" CREATE TABLE IF NOT EXISTS Books(ID integer PRIMARY KEY, Title text, Author text, DatePublished integer);""")

cursor.execute("""INSERT INTO Books(ID, Title, Author, DatePublished) VALUES("1", "De Profundis", "Oscar Wilde", "1905")""")
db.commit()
(Nov-25-2021, 03:55 AM)arkiboys Wrote: [ -> ]sqlite3.OperationalError: table Books has no column named DatePublished
That is an error that seems to be not correct, because the column name seems to be correct indeed. But are you sure the table did not exist before you ran your script? Try to drop the table before you create it.
And there are some things to mention about your script. Please correct them first.
  • The indentation is not correct. The two executes and the commit should be aligned with the line "cursor = db.cursor()".
  • String literals should be enclosed by single quotes; not double quotes like you do.
  • Numeric literals should not be enclosed by quotes.
So could you try this:
import sqlite3
 
with sqlite3.connect("BookInfo.db") as db:
    cursor = db.cursor()
    cursor.execute("""CREATE TABLE IF NOT EXISTS Books(ID integer PRIMARY KEY, Title text, Author text, DatePublished integer)""")
    cursor.execute("""INSERT INTO Books(ID, Title, Author, DatePublished) VALUES(1, 'De Profundis', 'Oscar Wilde', 1905)""")
    db.commit()
Please let us know if this works.
I had to drop the table first.
Thank you