简体   繁体   English

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

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

I'm working on a simple inventory app that will manage hardware and software inventory.我正在开发一个简单的库存应用程序,它将管理硬件和软件库存。 Right now I'm just trying to simply enter in the data a user inputs into the text boxes into my database.现在我只是想简单地将用户输入的数据输入到我的数据库中的文本框中。 The program runs but when I enter text and click the button to enter the data the cursor spins for second and the app closes out.程序运行,但是当我输入文本并单击按钮输入数据时,cursor 旋转第二次,应用程序关闭。 Any ideas?有任何想法吗? I have tried multiple formats for the sql statement.我已经为 sql 语句尝试了多种格式。 I did at one point get it where it was entering blank/null rows.我曾在某一时刻得到它输入空白/空行的位置。 The print functions are there just to make sure I was retrieving the data from the text boxes.打印功能只是为了确保我从文本框中检索数据。

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

Your code works perfectly for me, I created a new DB named "inventoryDatabase" with a table named "Hardware" just as below:您的代码非常适合我,我创建了一个名为“inventoryDatabase”的新数据库,其中包含一个名为“Hardware”的表,如下所示:

在此处输入图像描述

I tried your code on my side but everything works for me perfectly:我在我这边尝试了你的代码,但一切对我来说都很完美: 在此处输入图像描述

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

Sometimes, your public IP address detected by Azure Portal is not so accurate, you can double-check your IP here .有时,Azure Portal 检测到的您的公共 IP 地址不是那么准确,您可以在此处仔细检查您的 IP。

If these 2 points can't solve your issue, please provide me with some detailed exception information.如果这两点都不能解决你的问题,请给我一些详细的异常信息。

My IP has been added and my firewall is setup correctly.我的 IP 已添加,我的防火墙设置正确。 Reason I know this is because I took the enterNewHardware function code and put it in it's own py file, removed the textbox variables out of the data variable and just added literal strings.我知道这是因为我将 enterNewHardware function 代码放入它自己的 py 文件中,从数据变量中删除了文本框变量,并添加了文字字符串。 This works and inputs the data.这工作并输入数据。 Something between my textbox variables and the database.我的文本框变量和数据库之间的东西。 It's not liking something.它不喜欢什么。 Also I created a new Hardware table called "HardwareThree" just for testing so that why it's renamed here.此外,我创建了一个名为“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