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:
import sqlite3 conn=sqlite3.connect("lite.db") cur=conn.cursor() 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')") conn.commit() conn.close()
here is the error:
PS D:mysiteInteracting with Databases> python 1.py Traceback (most recent call last): File "D:mysiteInteracting with Databases1.py", line 5, in <module> cur.execute("CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)") sqlite3.DatabaseError: malformed database schema (?)
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.