Python Forum
sqlite3 Conn Insert Value Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sqlite3 Conn Insert Value Error
#4
There are three quotes in python, single ', double " and triple""". Single and double quotes can be used interchangeably, but they must be paired. If a string literal starts with a double quote, it ends at the next double quote. In your example, you want the SQL command to be a single string, but the way you use quotes results in multiple strings.
Output:
"INSERT INTO SIMPSON_INFO(NAME, GENDER, AGE, OCCUPATION) VALUES (" ", " ", 10, " ");"
To use quotes within a string you have two choices. Disable the special meaining of the quote character, or use a different quote character to mark the start and stop of the string.

To stop a quote from being interpreted as the end of a string, preceed the quote with a backslash. This is your code using that technique.
conn.execute("INSERT INTO SIMPSON_INFO(NAME, GENDER, AGE, OCCUPATION) VALUES (\"Bart\", \"Male\", 10, \"Student\");")
The example you posted uses the second approach. It uses single quotes inside the string and double quotes to mark the start and end of the string.
conn.execute("INSERT INTO SIMPSON_INFO(NAME, GENDER, AGE, OCCUPATION) VALUES ('Bart', 'Male', 10, 'Student');")
Python ignores the single quotes because it looking for the a quote that matches the start of the string. This is were triple quotes come in handy.

Triple quotes are used to make a string literal that spans multiple lines.
""" I am
a string that spans
over multiple lines."""
But triple quotes can also be used for single line strings. The advantage of using triple quotes is you are free to use single and double quotes inside the string.

One last thing. Are you learning Python or C? Why are you using semicolons? A semicolon can be used to write multiple statements on a single line.
a = 1; b = 2 * a; c = a + b; print(a, b, c)
HOWEVER, you should not write multiple statements on one line, so there should be almost no semicolons used in a python program.
Reply


Messages In This Thread
sqlite3 Conn Insert Value Error - by TylerDunbar - Sep-04-2023, 04:10 PM
RE: sqlite3 Conn Insert Value Error - by DPaul - Sep-04-2023, 04:57 PM
RE: sqlite3 Conn Insert Value Error - by deanhystad - Sep-04-2023, 06:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Ldap3 Python print(conn.entries) doesnt work ilknurg 15 5,890 Dec-28-2022, 11:22 AM
Last Post: shad
  pymysql: insert query throws error wardancer84 12 4,688 Jan-28-2022, 06:48 AM
Last Post: wardancer84
  sqlite3.OperationalError: near "=": syntax error Maryan 1 5,678 Oct-31-2020, 12:09 AM
Last Post: Maryan
  Python to Oracle Conn Issue chvsnarayana 2 42,640 Sep-06-2020, 04:33 PM
Last Post: Larz60+
  sqlite3.OperationalError: near "%": syntax error Linuxdesire 2 18,107 Oct-13-2019, 02:54 AM
Last Post: Linuxdesire
  psycopg2 insert error Wonder_women 0 2,691 Jun-10-2019, 11:56 AM
Last Post: Wonder_women
  Where is the error with this db creation code & the 'conn' variable? pcsailor 6 3,506 Nov-11-2018, 10:25 AM
Last Post: pcsailor
  sqlite3 operational error on insert query jonesin1974 5 4,347 Jun-26-2018, 03:31 PM
Last Post: Larz60+
  Small sqlite3 program error pythonNoob 5 3,678 May-10-2018, 05:45 PM
Last Post: pythonNoob
  insert list into sqlite3 mepyyeti 3 13,788 Jan-15-2018, 06:35 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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