Sep-04-2023, 04:10 PM
(This post was last modified: Sep-04-2023, 04:15 PM by Yoriz.
Edit Reason: Added code tags
)
Hello - Thank you for reading my post. Quick disclaimer - Teaching myself python late in life (64 yrs). This is to support PhD. I am using "Python Day 2" to get a very very very basic intro to python. The code in the book is throwing an error.
Question: I am attempting to load a single line to a sql database from python. Where is the missing comma? Rather, what is wrong with my syntax. I have included "My Code" and the "Error Message" Below.
I have searched Python Forum, sqlitetutorial.net, geeksforgeeks. Everything that I reference uses the cursor method and a more significant amount of code (see "Example"). I am working through the Example Code currently. I would like to understand where my syntax error is so that I can learn from every effort on my learning journey.
My Code
Question: I am attempting to load a single line to a sql database from python. Where is the missing comma? Rather, what is wrong with my syntax. I have included "My Code" and the "Error Message" Below.
I have searched Python Forum, sqlitetutorial.net, geeksforgeeks. Everything that I reference uses the cursor method and a more significant amount of code (see "Example"). I am working through the Example Code currently. I would like to understand where my syntax error is so that I can learn from every effort on my learning journey.
My Code
import sqlite3
conn = sqlite3.connect("simpsons.db")
#conn.execute("CREATE TABLE SIMPSON_INFO(ID INTEGERPRIMARY KEY, NAME TEXT, GENDER TEXT, AGE INT, OCCUPATION TEXT);")
conn.execute("INSERT INTO SIMPSON_INFO(NAME, GENDER, AGE, OCCUPATION) VALUES ("Bart", "Male", 10, "Student");")
conn.commit()Error MessageError:C:\Python\PyCharmProjects\pythonProject1\venv\Scripts\python.exe C:\Python\PyCharmProjects\pythonProject1\DataBaseInstall.py
File "C:\Python\PyCharmProjects\pythonProject1\DataBaseInstall.py", line 11
conn.execute("INSERT INTO SIMPSON_INFO(NAME, GENDER, AGE, OCCUPATION) VALUES ("Bart", "Male", 10, "Student");")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?Example# importing sqlite3 module
import sqlite3
# create connection by using object
# to connect with hotel_data database
connection = sqlite3.connect('hotel_data.db')
# query to create a table named FOOD1
connection.execute(''' CREATE TABLE hotel
(FIND INT PRIMARY KEY NOT NULL,
FNAME TEXT NOT NULL,
COST INT NOT NULL,
WEIGHT INT);
''')
# insert query to insert food details in
# the above table
connection.execute("INSERT INTO hotel VALUES (1, 'cakes',800,10 )")
connection.execute("INSERT INTO hotel VALUES (2, 'biscuits',100,20 )")
connection.execute("INSERT INTO hotel VALUES (3, 'chocos',1000,30 )")
print("All data in food table\n")
# create a cousor object for select query
cursor = connection.execute("SELECT * from hotel ")
# display all data from hotel table
for row in cursor:
print(row)
