英文:
mysql python insert data given by user
问题
I've been using this code to insert data given by the user into a MySQL table using Python, but the user-given data doesn't show in MYSQL after I run the code:
import mysql.connector
connection = mysql.connector.connect(host='localhost',
database='CLOTHdb',
user='root',
password='')
sql_insert_Query = "INSERT INTO PRODUCT_MASTER values (%s,%s,%s,%s,%s)"
prod_code = int(input("Enter Product Code: "))
pname = input("Enter Product Name: ")
price = float(input("Enter Product Price: "))
pcolor = input("Enter Product Color: ")
pdesc = input("Enter Product Description: ")
param = (prod_code, pname, price, pcolor, pdesc)
cursor = connection.cursor()
cursor.execute(sql_insert_Query, param)
records = cursor.fetchall()
print("Total number of rows in the table is: ", cursor.rowcount)
print("\nPrinting product info")
for row in records:
print("prod_code =", row[0])
print("pname =", row[1])
print("price =", row[2])
print("pcolor =", row[3])
print("pdesc =", row[4], "\n")
英文:
I've been using this code to insert data given by user into mysql table using python, but the user given data doesn't show in MYSQL after I run the code:
import mysql.connector
connection = mysql.connector.connect(host='localhost',
database='CLOTHdb',
user='root',
password='')
sql_insert_Query = "INSERT INTO PRODUCT_MASTER values (%s,%s,%s,%s,%s)"
prod_code = int(input("Enter Product Code: "))
pname = input("Enter Product Name: ")
price = float(input("Enter Product Price: "))
pcolor = input("Enter Product Color: ")
pdesc = input("Enter Product Description: ")
param = (prod_code, pname, price, pcolor, pdesc)
cursor = connection.cursor()
cursor.execute(sql_insert_Query,param)
records = cursor.fetchall()
print("Total number of rows in table is: ", cursor.rowcount)
print("\nPrinting product info")
for row in records:
print("prod_code = ", row[0], )
print("pname = ", row[1])
print("price = ", row[2])
print("pcolor = ", row[3])
print("pdesc = ", row[4], "\n")
答案1
得分: 1
You have to use commit()
method after executing the query for changes to the database.
cursor.execute(sql_insert_Query,param)
cursor.commit()
英文:
You have to use commit()
method after executing the query for changes to the database.
cursor.execute(sql_insert_Query,param)
cursor.commit()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论