0

I am trying to insert data from a loop that is similar to this.

import sqlite3
conn = sqlite3.connect('database.db')
c = conn.cursor()

words = ["apple", "banana", "cherry"]
for word in words:
     c.execute("insert into Words (word), values (?)",(word))
     print(word) 
     conn.commit

c.close()
conn.close()

Expected result is similar to this:

enter image description here

I am getting an error. But I am not sure how to correctly format this code right.

The error:

Traceback (most recent call last):
  File "file.py", line 7, in <module>
    c.execute("insert into Words (word), values (?)",(word))
sqlite3.OperationalError: near ",": syntax error
0

3 Answers 3

2

wrong list : commit() not commit because it is a method "insert into Words (word) values (?)",(word,) not "insert into Words (word), values (?)",(word)

right code is:

import sqlite3
conn = sqlite3.connect('database.db')
c = conn.cursor()

words = ["apple", "banana", "cherry"]
for word in words:
    c.execute("insert into Words (word) values (?)",(word,))
    print(word) 
conn.commit()
conn.close()

Don't worry, happy codding

Sign up to request clarification or add additional context in comments.

Comments

0

I'm guessing you're getting an error because you can't place a single item in parenthesis to make it a tuple - so (word) isn't the way to go. It should be one of these (whichever one is more readable to you):

c.execute("insert into Words (word) values (?)",(word,))

Notice the comma inserted after word to make it a tuple. Or:

c.execute("insert into Words (word) values (?)",[word])

Which would make word the only element in a list

You can see for yourself in the console that doing something like ('hello') isn't equal to ('hello',). The first one remains a string, the second one is a tuple (which is what's needed in your command).

Edit: Also, you had a comma in that command that shouldn't be there

2 Comments

Still getting same error on both c.execute("insert into Words (word), values (?)",(word,)) sqlite3.OperationalError: near ",": syntax error
@ned sorry i edited that, that comma isn't needed, try again with the updated code
0

The easiest way is to use f-string:

c.execute(f"INSERT INTO Words (word), values ({word})")

This is much easier to read :) but requires python 3.6+

import sqlite3

with sqlite3.connect('database.db') as conn:
    c = conn.cursor()

    words = ["apple", "banana", "cherry"]
    for word in words:
         c.execute(f"INSERT INTO Words (word), values ({word})")
         print(word) 
         conn.commit()

2 Comments

this will work, but in my experience it's a really bad idea to use f-stings on sqlite commands (not to mention is will be really difficult for large numbers of values)
Not in what I have tried as it leaves SQL code looking SQL. All the ? ? ? are easy to mix up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.