Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sql error
#1
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()
Yoriz write Nov-25-2021, 07:22 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
(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.
Reply
#3
I had to drop the table first.
Thank you
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020