所以我創建了一個選單選項,用戶在其中輸入一些資料并使用一個函式將所有這些資料添加到 MySQL 表中,但是資料表沒有正確更新我檢查過創建的表沒有任何問題,所以我認為問題在于某處的代碼
import mysql.connector
def add_det(x1, x2, x3, x4):
global icode, iname, price, quantity
try:
conn=mysql.connector.connect(host='localhost', database='practicals', user='root', password='root1')
cursor=conn.cursor()
print("cursor objected created")
sql = """#INSERT INTO inventory(item_code, item_name, price, quantity) VALUES (%s,%s,%s,%s)"""
t=(x1, x2, x3, x4)
print(t)
cursor.execute(sql)
conn.commit()
print(cursor.rowcount, "Records inserted")
except mysql.connector.Error as e:
print("Failed to get record from Mysql table: {}", format(error))
finally:
if conn.is_connected():
print("closing...")
cursor.close()
conn.close()
print("Mysql connection is closed")
add_det(icode, iname, price, quantity)
任何人都可以幫我找到錯誤嗎?
uj5u.com熱心網友回復:
洗掉#SQL 查詢前面的會發生什么?
或者嘗試使用以下方法更改您的代碼:
sql = """#INSERT INTO inventory(item_code, item_name, price, quantity)
VALUES ({}, {}, {}, {})""".format(
x1, x2, x3, x4
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/329532.html
