Я устанавливаю интерфейс в Python 3.7.2 для подключения и взаимодействия с базой данных в MYSQL. У меня проблемы с созданием записи с использованием входов tkinter.
import mysql.connector
import tkinter
def insertrecord1(menu):
menu.destroy()
insert=tkinter.Tk()
insert.title=("Insert a record")
pnamelbl=tkinter.Label(insert, text="Product name:")
pnamelbl.pack()
pnameenter=tkinter.Entry(insert)
pnameenter.pack()
costlbl=tkinter.Label(insert, text="Product buying cost (xx.xx):")
costlbl.pack()
costenter=tkinter.Entry(insert)
costenter.pack()
pricelbl=tkinter.Label(insert, text="Product selling price (xx.xx):")
pricelbl.pack()
priceenter=tkinter.Entry(insert)
priceenter.pack()
stocklbl=tkinter.Label(insert, text="Current product stock")
stocklbl.pack()
stockenter=tkinter.Entry(insert)
stockenter.pack()
toorderlbl=tkinter.Label(insert, text="Stock of product to be ordered")
toorderlbl.pack()
toorderenter=tkinter.Entry(insert)
toorderenter.pack()
recordconfirm=tkinter.Button(insert, text="Confirm", command=lambda:
insertrecord2(pnameenter, costenter, priceenter, stockenter,
toorderenter))
recordconfirm.pack()
def insertrecord2(pnameenter, costenter, priceenter, stockenter,
toorderenter):
product_name=pnameenter.get()
cost=costenter.get()
price=priceenter.get()
stock=stockenter.get()
to_order=toorderenter.get()
stock=mysql.connector.connect(user="root",password="hm2002",
host="127.0.0.1",
database="getconnected")
cursor=stock.cursor()
add_pstock=("INSERT INTO stock"
"(product_id, product_name, cost, price, stock, to_order)"
"VALUES (%s, %s, %s, %s, %s, %s)")
#defines auto increment value for easy primary key
product_id=cursor.lastrowid
product_data=(product_id,product_name, cost, price, stock, to_order)
#executes insert
cursor.execute(add_pstock, product_data)
#commits data to database
stock.commit()
cursor.close()
stock.close()