mysql python插入用户给定的数据

问题描述 投票:0回答:1

我一直在使用这段代码使用python将用户给定的数据插入到mysql表中,但是在我运行代码后,用户给定的数据没有显示在MYSQL中:

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

python mysql mysql-python
1个回答
0
投票

执行查询更改数据库后,您必须使用

commit()
方法。

cursor.execute(sql_insert_Query,param)
cursor.commit()
© www.soinside.com 2019 - 2024. All rights reserved.