0

I am trying to take data from entry fields and save it in the database. I have mentioned 6 columns in my SQL query but I have one more column in database which is ID and it is auto incremented. When I run this script I have no error but also no data gets inserted into the db rather than ID which is auto incremented. Here is my code:

window=tkinter.Tk()
# Set the window title
window.title("Princedeep Singh")
window.geometry("500x400")
current_row = 0

# Set the current row to zero

refdate_label = Label(window, text="Enter value for date: ")
refdate_label.grid(row=current_row, column=0)
refdate_text = StringVar()
refdate_entry = Entry(window, textvariable=refdate_text)
refdate_entry.grid(row=current_row, column=1)

current_row += 1

# geogeo field
geo_t_label = Label(window ,text="Enter value for geo:")
geo_t_label.grid(row=current_row, column=0)
geo_t_text = StringVar()
geo_t_entry = Entry(window, textvariable=geo_t_text)
geo_t_entry.grid(row=current_row, column=1)
current_row += 1


# commod field
commod_label = Label(window, text="Enter value for commod:")
commod_label.grid(row=current_row, column=0)
commod_text = StringVar()
commod_entry = Entry(window, textvariable=commod_text)
commod_entry.grid(row=current_row, column=1)
current_row += 1


# vector
vector_label = Label(window, text="Enter value for vector:")
vector_label.grid(row=current_row, column=0)
vector_text = StringVar() 
vector_entry = Entry(window, textvariable=vector_text)
vector_entry.grid(row=current_row, column=1)
current_row += 1



# commod field
coordinate_label = Label(window, text="Enter value for coordinate:")
coordinate_label.grid(row=current_row, column=0)
coordinate_text = StringVar()
coordinate_entry = Entry(window, textvariable=coordinate_text)
print(coordinate_text)
coordinate_entry.grid(row=current_row, column=1)
current_row += 1


value_label = Label(window, text="Enter value :")
value_label.grid(row=current_row, column=0)
value_text = StringVar()
value_entry = Entry(window, textvariable=value_text)
value_entry.grid(row=current_row, column=1)
current_row += 1



conn = mysql.connector.connect(host='localhost', db='postgres', user='root', 
       password='root')
cur = conn.cursor()

submit = Button(window, text='submit', command=cur.execute('INSERT INTO 
         record(refdate,geo,commod,vector,coordinate,value) VALUES (%s, %s, 
         %s, %s, %s,%s)',
         (refdate_text.get(),geo_t_text.get(),commod_text.get()
         ,vector_text.get(),str(coordinate_text.get()),value_text.get() )))  
submit.grid(row=current_row+1, column=4)
conn.commit()
window.mainloop()
1

1 Answer 1

1

The problem is here:

submit = Button(window, text='submit', command=cur.execute('INSERT INTO 
         record(refdate,geo,commod,vector,coordinate,value) VALUES (%s, %s, 
         %s, %s, %s,%s)',
         (refdate_text.get(),geo_t_text.get(),commod_text.get()
         ,vector_text.get(),str(coordinate_text.get()),value_text.get() )))  
submit.grid(row=current_row+1, column=4)

You're calling cur.execute(…) at creation time, and passing the result as the command for the Button.

Of course at creation time, the entries are all empty, so you insert an empty row.

And then, the result of execute is not a function, so what you store as the command is not something that can be called, so clicking the button later does nothing except write a warning to stderr that you aren't looking at.

You need to create a function (of no arguments), that you can pass to command—a function that, when called, will do this insert for you. For example:

def on_submit():
    cur.execute('INSERT INTO record blah blah blah' …, value_text.get()))

submit = Button(window, text='submit', command=on_submit)

If you'd prefer to do it all in one giant expression, as in your old code, you can use lambda instead of def, or use functools.partial(execute, …), but it's probably clearer this way.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks abarnert for your solution. I followed your answer but i have same issue. I made two seperate functions under class one function has all code listed above other method has db connection and query. I made variables global but don't know how to use variables value from one function to another. Please give me some suggestions.
@ZimidarBoy That explanation of what you did isn't nearly enough for me to debug it. If you're actually having the same problem after a small change to your code, edit the question. If you're having a similar problem after rewriting your code, post a new question. (You can link to this one to explain why you've done what you did.) Either way, that gives you room to include a complete example that people can debug, and it means your example will be seen by the whole SO Python community instead of just whoever's reading comments under an old answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.