1

Basically I want to be able to choose an amount of numbers using for x...in range(y amount of numbers) and inject them into an SQLite database. However I receive the following error:

line 17, in <module>
values (?)""") (number)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. 
The current statement uses 1, and there are 0 supplied.

Here is my non functioning code. All input is appreciated.:

import sqlite3
conn = sqlite3.connect("usernames.sqlite")
c = conn.cursor()
c.execute('''create table numbers (number)''')

for number in range(21):
# Insert a row of data
c.execute("""insert into numbers
values (?)"""),(number)

# Save (commit) the changes
conn.commit()
# We can also close the cursor if we are done with it
c.close()

2 Answers 2

1

execute takes a tuple of values in the case of "?" parameters, so you need to write:

c.execute("""insert into numbers values (?)""", (number,))

(number,) is a way to define a tuple with one element.

And by the way, you can do this more efficiently using the executemany method:

c.executemany(
    """insert into numbers values (?)""",
    [(number,) for number in range(21)]
)
Sign up to request clarification or add additional context in comments.

Comments

0

You've got a misplaced closed paren in values (?)"""),(number).

Not sure why this wasn't a syntax error, though. Is this the exact code that you were using?

Comments

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.