问题描述
我有一个元组,我想存储其元素,我正尝试将其插入为以下内容,并且出现以下错误,我在做什么错呢? records_to_be_inserted是具有8个元素的元组.
I have a tuple that i wanna store its elements, I'm trying to insert it as following and it gives the following error, what am i doing wrong ? records_to_be_inserted is the tuple that has 8 elements.
with self.connection:
cur = self.connection.cursor()
cur.executemany("INSERT INTO rehberim(names, phone, mobile, email, \
photo, address, note, date) VALUES(?, ?, ?, ?, ?, ?, ?, ?)", self.records_to_be_inserTed)
回溯(最近通话最近): 在save_records中的文件"/home/tayfun/workspace/personal_guide/modules/mainwindow.py",第57行 照片,地址,注释,日期)VALUES(?,?,?,?,?,?,?,?,?),self.records_to_be_inserTed)sqlite3.ProgrammingError:提供的绑定数量不正确.当前语句使用8,并且提供了0.
Traceback (most recent call last): File "/home/tayfun/workspace/personal_guide/modules/mainwindow.py", line 57, in save_records photo, address, note, date) VALUES(?, ?, ?, ?, ?, ?, ?, ?)", self.records_to_be_inserTed)sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 8, and there are 0 supplied.
推荐答案
查询必须已准备好插入所有数据.您正在查询中调用一个函数,我想您希望该函数提供数据但无法正常工作.您需要将所有数据传递到变量中或将它们定位在元组索引中(例如:tuple_name [1],tuple_name [4]等)
The query must have all the data ready to be inserted.You are calling a function in the query, which i guess you want that provides the data but that wont work.You need to pass all the data in variables or locate them in the tuple index (like: tuple_name[1], tuple_name[4], etc.)
示例:
myTuple = ['a','b','c','d','e','f','g']
cur.executemany("INSERT INTO rehberim(names, phone, mobile, email, \
photo, address, note, date) VALUES({0}, {1}, {2}, {3}, {4}, {5}, {6}" .format (myTuple[1], myTuple[2], myTuple[3], myTuple[4], myTuple[5], myTuple[6], myTuple[7])
这篇关于将元组的元素插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!