从数据库输出第二列

发布于 2025-02-10 23:56:00 字数 1333 浏览 1 评论 0 原文

我想返回或从数据库中读取所有列中的所有数据,以便例如检查“电子邮件”列是否包含特定的电子邮件。

如果我这样运行代码,我会正确地获得第一个电子邮件地址,但第二个电子邮件地址没有。

使用我可以正确正确的地方,但是后来我每次都必须查找ID号并调整代码。

如何输出第二列?

    from operator import index
    from PySide6.QtWidgets import QApplication, QMainWindow
    from userpasswort.xy import Ui_MainWindow
    from userpasswort.bu.oo import Ui_alper
    from PySide6 import QtSql
    class fm(QMainWindow, Ui_alper):
    def __init__(self):
    super().__init__()
    self.setupUi(self)
    self.newaccount.clicked.connect(self.weiter)
    def weiter(self):
    main.show()
    class Main(QMainWindow, Ui_MainWindow):
    def __init__(self):
    super().__init__()
    self.setupUi(self)
    self.offene = QtSql.QSqlRelationalTableModel()
    self.offene.setTable('info')
    self.continue_2.clicked.connect(self.datenbank)

    def datenbank(self):
    model = self.offene
    query = QtSql.QSqlQuery()
    query.exec( "SELECT email FROM info ")
    query.first()
    print(query.value(0)) **#here i get the correct email adress**
    print(query.value(1)) **#here i get NON**E
    model.select()
    db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName('userpasswort.sqlite')
    app = QApplication()
    main = Main()
    mf= fm()
    mf.show()
    app.exec()

I would like to get back or read out all the data from a column from a database so that I can check whether, for example, the E-Mail column contains a specific e-mail.

If I run the code like this, I get the first email address back correctly, but not the second.

With WHERE I would get it right, but then I had to look for the ID number and adapt the code every time.

How can I output the second column?

    from operator import index
    from PySide6.QtWidgets import QApplication, QMainWindow
    from userpasswort.xy import Ui_MainWindow
    from userpasswort.bu.oo import Ui_alper
    from PySide6 import QtSql
    class fm(QMainWindow, Ui_alper):
    def __init__(self):
    super().__init__()
    self.setupUi(self)
    self.newaccount.clicked.connect(self.weiter)
    def weiter(self):
    main.show()
    class Main(QMainWindow, Ui_MainWindow):
    def __init__(self):
    super().__init__()
    self.setupUi(self)
    self.offene = QtSql.QSqlRelationalTableModel()
    self.offene.setTable('info')
    self.continue_2.clicked.connect(self.datenbank)

    def datenbank(self):
    model = self.offene
    query = QtSql.QSqlQuery()
    query.exec( "SELECT email FROM info ")
    query.first()
    print(query.value(0)) **#here i get the correct email adress**
    print(query.value(1)) **#here i get NON**E
    model.select()
    db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName('userpasswort.sqlite')
    app = QApplication()
    main = Main()
    mf= fm()
    mf.show()
    app.exec()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

司马昭之心 2025-02-17 23:56:00

如有记录,给您第一个结果。因此,没有其他结果将是此响应的一部分。

我建议用 query.next()在一段时间内:

addresses = []
while query.next():
    addresses.append(query.value(0))

As documented, query.first() gives you the first result. Hence, no other result will be part of this response.

I would suggest replacing query.first() with query.next() in a while loop:

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