简体   繁体   中英

Python sqlite3 error -executemany function

I am importing data from text file,

1, A1, chicago.

2, B1, NY.

3, K2, LA.

d=open('file.txt','r')
data=d.readlines()
data1=[]
for items in data:
    data1=items.split()
    #print(data1)
    stmt = "INSERT INTO Student1 VALUES (?,?,?)"
    cu.executemany(stmt, data1)```


I am getting this error.
cu.executemany(stmt, data1)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 4, and there are 2 supplied.

To use "executemany" you need to pass list of tuples:

conn = sqlite3.connect('test.db')
cu = conn.cursor()
d = open('file.txt', 'r')
data = d.readlines()
data1 = []
for items in data:
    data1 += [items.split()]
stmt = "INSERT INTO Student1 VALUES (?,?,?,?)"
cu.executemany(stmt, data1)
conn.commit()

Use executemany to insert list of rows. Or change to "execute"

for items in data:
    data1=items.split()
    #print(data1)
    stmt = "INSERT INTO Student1 VALUES (?,?,?)"
    cu.execute(stmt, data1)

executemany() this function needs second parameter as list of tuples in python. data1 variable in your code is list without tuples. you can use something like following.

import sqlite3
d=open('file.txt','r')
data=d.readlines()
data1=[]
data1_in_list_of_tuple = []
for items in data:
    data1=items.split()
    data1_in_list_of_tuple.append(tuple(data1))
stmt = "INSERT INTO Student1 VALUES (?,?,?)"
cu.executemany(stmt, data1_in_list_of_tuple)

Since you are only inserting one student at a time, use cursor.execute instead.

Create the data (basically equivalent to reading the file):

In [1]: txt = '''1, A1, chicago.
   ...: 2, B1, NY.
   ...: 3, K2, LA.'''
Out[1]: '1, A1, chicago.\n2, B1, NY.\n3, K2, LA.'

In [2]: data = txt.splitlines()
Out[2]: ['1, A1, chicago.', '2, B1, NY.', '3, K2, LA.']

For correct data handling;

  • Remove the . at the end of each line.
  • split on the , ,
  • strip all the parts.

Let's test how to do that:

In [4]: item = [j.strip() for j in data[0][:-1].split(",")]
Out[4]: ['1', 'A1', 'chicago']

This looks OK.

Create database and table (note that I've made up field names):

In [5]: import sqlite3

In [6]: con = sqlite3.connect(':memory:')
Out[6]: <sqlite3.Connection at 0x80414cd50>

In [7]: cur = con.cursor()
Out[7]: <sqlite3.Cursor at 0x8041aab20>

In [8]: cur.execute("CREATE TABLE Student1 (num INTEGER, grade TEXT, location TEXT)")
Out[8]: <sqlite3.Cursor at 0x8041aab20>

Now we can insert all the students:

In [9]: for line in data:
   ...:     items = tuple(j.strip() for j in line[:-1].split(","))
   ...:     cur.execute("INSERT INTO Student1 VALUES (?,?,?)", items)
   ...:    

Check that it worked:

In [10]: for row in cur.execute("SELECT * FROM Student1"):
    ...:     print(row)
    ...:     
(1, 'A1', 'chicago')
(2, 'B1', 'NY')
(3, 'K2', 'LA')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM