python - 无法通过 python 和 tkinter 发送 mysql 查询
问题描述
我想制作一个程序,它接收股票名称、股票单价、使用 tkinter 接口购买的总股票并用数据更新 mysql 数据库:
import mysql.connector as mc
from tkinter import*
import time
from datetime import date
mycon=mc.connect(host="localhost",user="root",passwd="1234",database="stock_manager")
cursor=mycon.cursor()
global date
date=date.today()
global stock_name
global stock_unit_price
global stocks_bought
def add():
global stock_unit_price_entry
global stocks_bought_entry
global stock_name_entry
global stock_name
global stock_unit_price
global stocks_bought
stock_name_label=Label(root,text="Enter Stock Name")
stock_name_entry=Entry(root,width=50)
stock_unit_price_label=Label(root,text="Enter unit price of stock")
stock_unit_price_entry=Entry(root,width=50)
stock_unit_price_entry.insert(0,"0.00")
stocks_bought_label=Label(root,text="Enter number of stocks bought: ")
stocks_bought_entry=Entry(root,width=50)
stocks_bought_entry.insert(0,"0")
stock_name_label.grid(row=2,column=0)
stock_name_entry.grid(row=3,column=0)
stock_unit_price_label.grid(row=4,column=0)
stock_unit_price_entry.grid(row=5,column=0)
stocks_bought_label.grid(row=6,column=0)
stocks_bought_entry.grid(row=7,column=0)
submit_stock_button=Button(root,text="Submit",command=submit)
submit_stock_button.grid(row=8,column=1)
def submit():
global stock_name_entry
global stock_unit_price_entry
global stocks_bought_entry
global stock_name
global date
global stock_unit_price
global stocks_bought
stock_unit_price=float(stock_unit_price_entry.get())
stocks_bought=int(stocks_bought_entry.get())
stock_name=stock_name_entry.get()
total_investment=(stock_unit_price)*(stocks_bought)
todo="insert into all_stocks values('%s',%s,%s,%s,'%s')"%(stock_name,stock_unit_price,stocks_bought,total_investment,date)
print(todo)
cursor.execute(todo)
submitted_label=Label(root,text="Submitted!")
submitted_label.grid(row=9,column=1)
root=Tk()
title_label=Label(root,text="All Investments")
add_stock_button=Button(root,text="Add Stock",command=add)
title_label.grid(row=0,column=1)
add_stock_button.grid(row=1,column=0)
root.mainloop()
在上面的代码中,“todo”是要被传递的查询。我已经打印了 todo 语句来检查查询是否得到所有信息正确。输入如下: 输入画面
print(todo) 输出如下: todo 输出
查询应该插入值的 Mysql 表: mysql 表
这就是问题所在。待办事项查询是完美的。但它不会将该行添加到表中。如您所见,名为“Future Generali”的股票已经存在。我们的股票是“HDFC Bank”,显然没有添加。
有人可以帮我弄这个吗。我的代码是否出错或游标没有执行涉及在表中插入值的查询。如果是这样,解决方案是什么。请帮助我。
解决方案
推荐阅读
- python - 使用 Networkx 计算最短路径时出错
- android - java.lang.IllegalArgumentException:值“i”不是参考值 [proguard.evaluation.value.UnknownIntegerValue]
- javascript - 我想给每个渲染的 todo 元素一个 id
- javascript - 甚至在某些数组函数完成之前渲染的对象数组
- powershell - 在 PowerShell 中连接到 Exchange Online 时停止输出
- twilio - 获取使用通知 api 发送的短信
- linux - gcc:错误:无法识别的命令行选项
- .htaccess - apache2 config 将文件夹路径包含在 URL 中
- hibernate - 在 Wildfly 中使用 infinispan 作为 2LC 时缺少 MBean 类型:javax.cache:type=CacheStatistics
- c# - WCF 服务增加最大消息大小配额和配置