执行许多混乱

发布于 2024-10-01 01:37:00 字数 916 浏览 8 评论 0原文

好的,我有一个函数可以根据插件的输入选择 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕梦 2024-10-08 01:37:00

看看 http://www.python.org/dev/peps/pep-0249/

使用此方法进行操作
它产生一个或
更多结果集构成未定义的行为,并且
允许实施

因此,executemany 可用于 INSERT 和 UPDATE,但不能用于 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])

Look at 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

So, executemany can be used for INSERT's and UPDATE's but not for SELECT

You can try following code:

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])
懷念過去 2024-10-08 01:37:00

我认为您不需要在这里执行executemany
请尝试这样的操作:

self.memcursor.execute('''SELECT id, matbefore, matafter, name, date 
                            FROM main 
                           WHERE name IN (%s)''' % 
                       ','.join('?'*len(offset)), (offset,))

请注意,字符串插值是为了将多个占位符放入查询中而完成的。

I don't think you need executemany here.
Try something like this instead:

self.memcursor.execute('''SELECT id, matbefore, matafter, name, date 
                            FROM main 
                           WHERE name IN (%s)''' % 
                       ','.join('?'*len(offset)), (offset,))

Note that the string interpolation is done to place multiple placeholders into the query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文