以下是一个使用PyQt5实现的界面输入kingbase数据库连接信息,连接数据库,查询table里的信息,并打印在界面上的示例代码:

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout
from PyQt5.QtGui import QFont
import psycopg2

class DatabaseConnector(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Database Connector")
        self.setFont(QFont("Arial", 12))

        self.host_label = QLabel("Host:")
        self.host_input = QLineEdit()
        self.port_label = QLabel("Port:")
        self.port_input = QLineEdit()
        self.database_label = QLabel("Database:")
        self.database_input = QLineEdit()
        self.username_label = QLabel("Username:")
        self.username_input = QLineEdit()
        self.password_label = QLabel("Password:")
        self.password_input = QLineEdit()
        self.table_label = QLabel("Table:")
        self.table_input = QLineEdit()
        self.connect_button = QPushButton("Connect")
        self.query_button = QPushButton("Query")
        self.result_label = QLabel("Result:")
        self.result_text = QLabel()

        layout = QVBoxLayout()
        layout.addWidget(self.host_label)
        layout.addWidget(self.host_input)
        layout.addWidget(self.port_label)
        layout.addWidget(self.port_input)
        layout.addWidget(self.database_label)
        layout.addWidget(self.database_input)
        layout.addWidget(self.username_label)
        layout.addWidget(self.username_input)
        layout.addWidget(self.password_label)
        layout.addWidget(self.password_input)
        layout.addWidget(self.table_label)
        layout.addWidget(self.table_input)
        layout.addWidget(self.connect_button)
        layout.addWidget(self.query_button)
        layout.addWidget(self.result_label)
        layout.addWidget(self.result_text)

        self.setLayout(layout)

        self.connect_button.clicked.connect(self.connect_to_database)
        self.query_button.clicked.connect(self.query_table)

    def connect_to_database(self):
        host = self.host_input.text()
        port = self.port_input.text()
        database = self.database_input.text()
        username = self.username_input.text()
        password = self.password_input.text()

        try:
            self.connection = psycopg2.connect(
                host=host,
                port=port,
                database=database,
                user=username,
                password=password
            )
            self.cursor = self.connection.cursor()
            self.result_text.setText("Connected to database.")
        except psycopg2.Error as e:
            self.result_text.setText(str(e))

    def query_table(self):
        table = self.table_input.text()

        try:
            self.cursor.execute(f"SELECT * FROM {table}")
            result = self.cursor.fetchall()
            self.result_text.setText(str(result))
        except psycopg2.Error as e:
            self.result_text.setText(str(e))

if __name__ == "__main__":
    app = QApplication([])
    window = DatabaseConnector()
    window.show()
    app.exec_()

在这个示例中,我们创建了一个继承自QWidget的DatabaseConnector类,它包含了用于输入kingbase数据库连接信息的文本框和标签,以及用于连接和查询的按钮。在按钮的点击事件处理函数中,我们使用psycopg2库连接到数据库,并执行查询操作。查询结果将显示在界面上的一个标签中。

请注意,在使用此代码之前,您需要先安装psycopg2库。您可以使用以下命令来安装它:

pip install psycopg2

您还需要根据您的实际情况修改代码中的数据库连接信息

pyqt5实现界面输入kingbase数据库连接信息连接数据库查询table里的信息并打印在界面上

原文地址: https://www.cveoy.top/t/topic/h1CN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录