简体   繁体   English

Python sqlite3 错误-executemany function

[英]Python sqlite3 error -executemany function

I am importing data from text file,我正在从文本文件导入数据,

1, A1, chicago. 1,A1,芝加哥。

2, B1, NY. 2,B1,纽约。

3, K2, LA. 3,K2,洛杉矶。

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:要使用“executemany”,您需要传递元组列表:

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.使用 executemany 插入行列表。 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. executemany() 这个 function 需要第二个参数作为 python 中的元组列表。代码中的 data1 变量是没有元组的列表。 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.由于您一次只插入一名学生,因此请改用cursor.execute

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 , , split, ,
  • strip all the parts. strip所有零件。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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