sql error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: sql error (/thread-35638.html) |
sql error - arkiboys - Nov-25-2021 Hello, Do you see why I get the error: ------------------------------------------------------------------------------------------------------------------------------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() RE: sql error - ibreeden - Nov-25-2021 (Nov-25-2021, 03:55 AM)arkiboys Wrote: sqlite3.OperationalError: table Books has no column named DatePublishedThat 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.
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. RE: sql error - arkiboys - Nov-25-2021 I had to drop the table first. Thank you |