pyqt5 - 使用JOIN

时间:2018-04-01 11:07:01

标签: python sqlite pyqt5 qsqlquery qsqldatabase

我可以读取数据库db1.sqlite的player表中的记录(没有JOIN)但是如果我使用连接到第二个 表第二个数据库db2.sqlite表,查询变量不包含任何记录。 如何编写连接的查询以使用2个数据库的2个连接?

感谢您的帮助。

import sys    
from PyQt5.QtWidgets import *    
from PyQt5.QtSql import QSqlDatabase, QSqlQuery

class Read_players():    
    def __init__(self):
        super(Read_players, self).__init__()

        conn_db1 = QSqlDatabase.addDatabase('QSQLITE') #default connection
        conn_db1.setDatabaseName('db1.sqlite')
        conn_db1.open()

        conn_db2 = QSqlDatabase.addDatabase('QSQLITE', 'conn_name_2')
        conn_db2.setDatabaseName('db2.sqlite')
        conn_db2.open()

        self.readDatas()

    def readDatas(self):
        query = QSqlQuery()
        qr = ''' SELECT p.Name, p.FIDE_ID, f.Fed
                 FROM players AS p
                 LEFT JOIN db2.fide AS f ON p.FIDE_ID = f.ID_NUMBER
              '''
        query.exec(qr)

        while query.next():
            players_name = query.value(0)
            players_fide_id = query.value(1)
            fide_fed = query.value(2)

app = QApplication(sys.argv)
read = Read_players()
sys.exit(app.exec_())

0 个答案:

没有答案