我的Python代码中的SQL语句有什么问题?

2024-10-01 09:20:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个简单的清单应用程序,可以管理硬件和软件清单。现在,我只是想简单地将用户输入到文本框中的数据输入到我的数据库中。程序运行,但当我输入文本并单击按钮输入数据时,光标旋转一秒钟,应用程序关闭。有什么想法吗?我尝试了sql语句的多种格式。我曾经在它输入空白/空行的地方得到过它。打印功能只是为了确保我正在从文本框中检索数据

from PyQt5.QtWidgets import (QLabel, QPushButton, QLineEdit, QApplication, QCheckBox, QMainWindow, QWidget,
                             QVBoxLayout, QTabWidget, QStatusBar)
import pyodbc
import sys


class mainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(385, 323)
        self.setWindowTitle("HARDWARE | SOFTWARE MANAGER")
        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)

        self.tabForm = QTabWidget()
        self.tabForm.addTab(hardwareTab(), "HARDWARE")
        self.tabForm.addTab(softwareTab(), "SOFTWARE")
        self.setCentralWidget(self.tabForm)


class hardwareTab(QWidget):
    def __init__(self):
        super().__init__()
        self.snLabel = QLabel("SERIAL NUMBER")
        self.snTextBox = QLineEdit()
        self.modelLabel = QLabel("MODEL")
        self.modelTextBox = QLineEdit()
        self.userLabel = QLabel("USER")
        self.userTextBox = QLineEdit()
        self.enButton = QPushButton("ENTER NEW HARDWARE")
        self.cfButton = QPushButton("CLEAR FIELDS")
        self.seButton = QPushButton("SEARCH/EDIT HARDWARE")
        self.activeCheckbox = QCheckBox("ACTIVE")
        self.testTextbox = QLineEdit()

        layout = QVBoxLayout(self)
        layout.addWidget(self.snLabel)
        layout.addWidget(self.snTextBox)
        layout.addWidget(self.modelLabel)
        layout.addWidget(self.modelTextBox)
        layout.addWidget(self.userLabel)
        layout.addWidget(self.userTextBox)
        layout.addWidget(self.activeCheckbox)
        layout.addWidget(self.enButton)
        layout.addWidget(self.cfButton)
        layout.addWidget(self.seButton)
        layout.addWidget(self.testTextbox)

        self.enButton.clicked.connect(lambda: enterNewHardware(self))


class softwareTab(QWidget):
    def __init__(self):
        super().__init__()
        self.snLabel = QLabel("SERIAL NUMBER / KEY")
        self.snTextbox = QLineEdit()
        self.nameLabel = QLabel("APPLICATION NAME")
        self.nameTextBox = QLineEdit()
        self.userLabel = QLabel("USER")
        self.userTextBox = QLineEdit()
        self.enButton = QPushButton("ENTER NEW SOFTWARE")
        self.cfButton = QPushButton("CLEAR FIELDS")
        self.seButton = QPushButton("SEARCH/EDIT SOFTWARE")

        layout = QVBoxLayout(self)
        layout.addWidget(self.snLabel)
        layout.addWidget(self.snTextbox)
        layout.addWidget(self.nameLabel)
        layout.addWidget(self.nameTextBox)
        layout.addWidget(self.userLabel)
        layout.addWidget(self.userTextBox)
        layout.addWidget(self.enButton)
        layout.addWidget(self.cfButton)
        layout.addWidget(self.seButton)


def enterNewHardware(textboxes):

    serial_number = textboxes.snTextBox.text()
    model_name = textboxes.modelTextBox.text()
    user_name = textboxes.userTextBox.text()
    test_textbox = textboxes.testTextbox.text()

    print(serial_number)
    print(model_name)
    print(user_name)
    print(test_textbox)

    azureServer = "pythonserver6974.database.windows.net"
    azureDB = "inventoryDatabase"
    userName = "na"
    password = "na"
    driver = "{ODBC Driver 17 for SQL Server}"
    connectionString = f"DRIVER={driver};SERVER={azureServer};PORT=1433;DATABASE={azureDB};UID={userName};PWD={password}"
    conn = pyodbc.connect(connectionString)
    cursor = conn.cursor()

    sql_statement = 'INSERT INTO inventoryDatabase.dbo.Hardware (serialNumber, modelName, userName, machineActive) VALUES (?, ?, ?, ?)'
    data = (serial_number, model_name, user_name, test_textbox)

    cursor.execute(sql_statement, data)
    conn.commit()


if __name__ == "__main__":
    APP = QApplication(sys.argv)
    WINDOW = mainWindow()
    WINDOW.show()
    sys.exit(APP.exec_())

Tags: nameselfinitdefsoftwarehardwarelayoutqpushbutton
2条回答

我的IP已添加,防火墙设置正确。我之所以知道这一点,是因为我获取了enterNewHardware函数代码并将其放入自己的py文件中,从数据变量中删除了textbox变量,只添加了文字字符串。它工作并输入数据。在我的文本框变量和数据库之间。它不喜欢什么东西。我还创建了一个名为“HardwareTree”的新硬件表,只是为了测试,所以在这里重命名了它

import pyodbc

azureServer = "pythonserver5874.database.windows.net"
azureDB = "inventoryDatabase"
userName = "na"
password = "na"
driver = "{ODBC Driver 17 for SQL Server}"
connectionString = f"DRIVER={driver};SERVER={azureServer};PORT=1433;DATABASE= 
{azureDB};UID={userName};PWD={password}"

conn = pyodbc.connect(connectionString)
cursor = conn.cursor()

sql_statement = '''INSERT INTO inventoryDatabase.dbo.HardwareThree (serialNumber, 
modelName, userName, machineActive)
                VALUES (?, ?, ?, ?)'''
data = ('Test', 'Test', 'Test', 'Test')

cursor.execute(sql_statement, data)
conn.commit()
cursor.commit()
conn.close()

enter image description here

您的代码非常适合我,我创建了一个名为“inventoryDatabase”的新数据库,其中包含一个名为“Hardware”的表,如下所示:

enter image description here

我尝试了你的代码,但一切都非常适合我: enter image description here

根据您提供的所有信息,一切正常,因此请检查您的表设计,是否已将本地公共IP添加到Azure SQL防火墙规则中(see here以将本地公共规则添加到Azure SQL防火墙)

有时,Azure Portal检测到的公共IP地址不太准确,您可以仔细检查您的IPhere

如果这两点不能解决您的问题,请向我提供一些详细的异常信息

相关问题 更多 >