1

im trying to insert multiple rows of data into at once into a table in pythong using sqlite3 and this is the code im using:

import sqlite3
import numpy as np
connection = sqlite3.connect("""HR_info.db""")
cursor = connection.cursor()

cursor.execute('''CREATE TABLE IF NOT EXISTS flu_jabs(staff_number integer PRIMARY KEY, flu_jab 
text)''')
def insert(entitiy):
    cursor.execute('''INSERT INTO flu_jab(staff_number,flu_jab) VALUES(?,?)''', entitiy)
x1 = np.zeros([1,2])
x2 = []
xs = []
for i in range(30):
    #print('has staff id ',results[i][0],' recived a flue shot? (Y/N): ')
    #x.append(input())
    xs = 'Y '#input('0/1')
    x1[0][0] = i
    x1[0][1] = 0
    insert(x1[0][0],x1[0][1])
print(x1)

however whatever i put in the argument of Insert() i get either a ProgrammingError:

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 2, and there are 1 supplied.

or a TypeError:

TypeError: insert() takes 1 positional argument but 2 were given

i really dont understand what method i should be using or if there is a much simpler way in insting multiple rows of data into a table in SQL. All of the examples ive seen online about INSERT have been of single rows, if anyone could point me in the direction of an example of mulitple rows at once it would be just as useful

1 Answer 1

1

The error

TypeError: insert() takes 1 positional argument but 2 were given

happens because you are calling your function with 2 arguments whereas it is defined as a function that takes only one argument. In your case, you should call it as

insert((x1[0][0],x1[0][1]))

Ie put these two arguments in a tuple.

Also, you have a typo. You are creating DB table "flu_jabs" (plural) but you are trying to insert into "flu_jab" (singular).

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

1 Comment

how do i turn this into a tuple efficently? when i try to run for i in range(30): x1 = ([],[]) xs = 'Y '#input('0/1') x1[0].append(i) x1[1].append(0) insert(x1) i get unsupported type error

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.