在python中使用sqlite的奇怪结果

wwtsj6pe  于 2023-03-03  发布在  SQLite
关注(0)|答案(1)|浏览(176)

我试图得到一个计数的文件,我的用户的电子邮件出现,我这样做的每一个用户,所以我使用一个循环,采取每一封电子邮件,我的用户得到了一个文件(以避免有0文件的人).
因此,我知道肯定的是,我得到了5行(5电子邮件与文档),在这一行中,我做了选择计数(*),以获得文档的数量,但它停止在第一个没有返回任何错误,当我得到乘坐的选择,它做了整个5行。
我已经在程序的其他部分使用了select in row,所以我不明白为什么它在这里不起作用。
我的代码是这样的:

def fAnalyseProd(self):
    for row in self.cur.execute("SELECT DISTINCT emailContact,idContact FROM contact JOIN REX ON idContact=redacteur"):

        print(row) # Returns the email + it's id

        emailContact = str(row[0])
        emailContact = emailContact.split("@")
        emailContact = emailContact[0].split(".")

        print(str(row[1])) # the id only

        self.cur.execute("SELECT count(*) FROM REX WHERE redacteur = ?", (str(row[1]),))
        totalREX = self.cur.fetchone()[0]
        print(totalREX)

我削减了一些代码,但它都是tkinter GUI,我精确地说,每一个代码,我写后,打印(totalREX)将执行,但它停止在只有一个迭代,而如果我得到了它,我得到了我的5行,我没有任何错误显示或类似的东西。

muk1a3rh

muk1a3rh1#

您使用同一个游标执行查询,同时还在迭代第一个查询的结果。
别这样使用两个单独的游标(或者,如这里所示,使用Connection.execute()实用程序为您执行此操作):

def fAnalyseProd(self):
    for row in self.conn.execute("SELECT DISTINCT emailContact,idContact FROM contact JOIN REX ON idContact=redacteur"):

        print(row) # Returns the email + it's id

        emailContact = str(row[0])
        emailContact = emailContact.split("@")
        emailContact = emailContact[0].split(".")

        print(str(row[1])) # the id only

        res = self.conn.execute("SELECT count(*) FROM REX WHERE redacteur = ?", (str(row[1]),))
        totalREX = res.fetchone()[0]
        print(totalREX)

相关问题