执行许多混乱 [英] Executemany confusion
问题描述
好的,所以我有一个函数可以根据插件的输入在 sqlite 数据库中选择某些行.当只涉及一个语句时,我使用插件来选择和获取行,但由于我想为此增加一些灵活性,我尝试让函数在遇到列表或元组时使用 executemany.然而,尽管我已经摆弄和改变了所有的东西,我仍然无法让它工作,要么是因为 sqlite 语句将字符串中的每个字符视为绑定,要么是因为元组中有太多的绑定.这是我到目前为止的代码:
Ok, so I have a function that selects certain rows in a sqlite database based on input from a plugin. I got the plugin to select and fetch rows when just one statement is involved, but since I want to add some flexibility to this, I tried making the function use executemany when encountering lists or tuples. Yet, despite all the things I have fiddled and changed, I a still unable to get this to work, either because the sqlite statement is treating each character in the string as a binding, or because there are too many bindings in the tuple. Here's the code I have so far:
def readoffset(self,offset):
vartype = type(name)
print(vartype)
if vartype == int:
self.memcursor.execute('''select all id,matbefore,matafter,name,date
from main as main where id = ?''',[offset])
undolist = self.memcursor.fetchall()
print(undolist)
return(undolist)
elif vartype == tuple or list:
print(vartype)
self.memcursor.executemany('''select all id,matbefore,matafter,name,date
from main as main where name = (?)''', [offset])
undolist = self.memcursor.fetchall()
return(undolist)
推荐答案
看http://www.python.org/dev/peps/pep-0249/
使用此方法进行操作产生一个或更多的结果集构成未定义的行为,并且允许实施
Use of this method for an operation which produces one or more result sets constitutes undefined behavior, and the implementation is permitted
因此,executemany 可用于 INSERT 和 UPDATE 但不能用于 SELECT
So, executemany can be used for INSERT's and UPDATE's but not for SELECT
您可以尝试以下代码:
elif isinstance(offset, (tuple, list)):
offsets=', '.join(offset)
self.memcursor.execute('''select all id,matbefore,matafter,name,date
from main as main where name IN (?)''', [offsets])
这篇关于执行许多混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!