I executed the python file in the first try & it worked. But when I included the code “IF NOT EXISTS” in the line cur.execute("CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)")
& cur.execute("INSERT INTO store VALUES ('Wine Glass,8,10.5')")
I am getting an error.
here is my code:
JavaScript
x
9
1
import sqlite3
2
3
conn=sqlite3.connect("lite.db")
4
cur=conn.cursor()
5
cur.execute("CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)")
6
cur.execute("INSERT INTO store VALUES ('Wine Glass,8,10.5')")
7
conn.commit()
8
conn.close()
9
here is the error:
JavaScript
1
6
1
PS D:mysiteInteracting with Databases> python 1.py
2
Traceback (most recent call last):
3
File "D:mysiteInteracting with Databases1.py", line 5, in <module>
4
cur.execute("CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)")
5
sqlite3.DatabaseError: malformed database schema (?)
6
Advertisement
Answer
You have an error in the code:
cur.execute("INSERT INTO store VALUES ('Wine Glass,8,10.5')")
You are providing only single value to three-column table. Replace it with:
cur.execute("INSERT INTO store VALUES ('Wine Glass','8','10.5')")
and your code should work fine.