执行许多混乱
好的,我有一个函数可以根据插件的输入选择 sqlite 数据库中的某些行。我让插件在只涉及一个语句时选择和获取行,但由于我想为此添加一些灵活性,所以我尝试让该函数在遇到列表或元组时使用executemany。然而,尽管我摆弄和改变了所有的事情,我仍然无法让它工作,要么因为 sqlite 语句将字符串中的每个字符视为绑定,要么因为元组中的绑定太多。这是我到目前为止的代码:
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看 http://www.python.org/dev/peps/pep-0249/
因此,executemany 可用于 INSERT 和 UPDATE,但不能用于 SELECT
您可以尝试以下代码:
Look at http://www.python.org/dev/peps/pep-0249/
So, executemany can be used for INSERT's and UPDATE's but not for SELECT
You can try following code:
我认为您不需要在这里执行
executemany
。请尝试这样的操作:
请注意,字符串插值是为了将多个占位符放入查询中而完成的。
I don't think you need
executemany
here.Try something like this instead:
Note that the string interpolation is done to place multiple placeholders into the query.