繁体   English   中英

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

[英]What is wrong with this SQL statement in my Python code?

我正在开发一个简单的库存应用程序,它将管理硬件和软件库存。 现在我只是想简单地将用户输入的数据输入到我的数据库中的文本框中。 程序运行,但是当我输入文本并单击按钮输入数据时,cursor 旋转第二次,应用程序关闭。 有任何想法吗? 我已经为 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_())

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

在此处输入图像描述

我在我这边尝试了你的代码,但一切对我来说都很完美: 在此处输入图像描述

Based on all the info you provided, it all works, so could you pls check your table design and have you added your local public IP to your Azure SQL firewall rules( see here to add your local public rules to your Azure SQL firewall)?

有时,Azure Portal 检测到的您的公共 IP 地址不是那么准确,您可以在此处仔细检查您的 IP。

如果这两点都不能解决你的问题,请给我一些详细的异常信息。

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

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()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM