mysql python 插入用户提供的数据

huangapple go评论69阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月6日 22:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定