无法通过python和tkinter发送mysql查询

无法通过python和tkinter发送mysql查询,python,mysql,tkinter,Python,Mysql,Tkinter,我想制作一个程序,使用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=&quo

我想制作一个程序,使用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语句以检查查询是否获得了所有正确的信息。输入如下:

打印(todo)输出如下所示:

Mysql表,查询将在其中插入值:

问题就在这里。todo查询非常完美。但它并没有将行添加到表中。如您所见,名为“Future Generali”的股票已经存在。我们的股票是“HDFC银行”,它显然没有增加


有人能帮我吗。我的代码出错了,或者游标没有执行涉及在表中插入值的查询。如果是,解决办法是什么。请帮助我。

你必须提交你的更改,试着说
cursor.execute('commit')
在说
cursor.execute(todo)
todo=“插入所有股票的价值('%s'、%s、%s、%s、'%s')”)”%(股票名称、股票单价、购买的股票、总投资、日期)
。您的
股票\u单价
和购买的
股票
属于数据类型
double
。请不要将其作为
%s%s
插入。请检查您的
数据库列
数据类型,并将其与您的数据类型匹配inserting@Karthik那么如何将数据类型从double改为column?@CoolCloud我会试试,谢谢你的回答@G Anand Iyengar请检查您在数据库中为
stock\u unit\u price和stock\u bunded
创建的列的类型。主要检查列的数据类型
date
。我觉得它可能是数据类型
datetime
,但这里是一个字符串