skip to Main Content
from tkinter import *

from tkinter import messagebox

import mysql.connector

from tkinter import Button


def ok():

    name = e1.get()

    salary = e2.get()

    dob = e3.get()

    mysqldb = mysql.connector.connect(host="localhost", user="root", password="", database="sahil")

    mycursor = mysqldb.cursor()

    try:
        sql = "INSERT INTO clients(ID,NAME,SALARY,DOB) VALUES(%s, %s, %s, %s)"
        val = (name, salary, dob)
        mycursor.execute(sql, val)
        mysqldb.commit()
        messagebox.showinfo("information", "record inserted succesfully..")
    except Exception as e:
        print(e)
        mysqldb.rollback()
        mysqldb.close()


root = Tk()

root.title("client data")

root.geometry("400x400")

global e1

global e2

global e3

Label(root, text="NAME").place(x=10, y=10)

Label(root, text="SALARY").place(x=10, y=40)

Label(root, text="DOB").place(x=10, y=80)

e1 = Entry(root)

e1.place(x=140, y=10)

e2 = Entry(root)

e2.place(x=140, y=40)

e3 = Entry(root)

e3.place(x=140, y=80)

Button(root, text="ADD", command="ok").place(x=10, y=120)

root.mainloop()

2

Answers


  1. Chosen as BEST ANSWER

    it should be command=ok ...rather then command="ok"


  2. you dont show parts of your code, they would maybe interesting. but if you want to know how to use "Insert" with pythond and mysql here is one suggestion (this isn’t on your code because you give less informations, but you can try this)

                cur_personen.execute("INSERT INTO person "                                      # INSERT INTO PERSON TABLE /
                                 "(id, BLA, bLUB, Whatever, Nothing_fk)"              #
                                 "VALUES (%s, %s, %s, %s, %s) ",                                #
                                 (                                                              #
                                     self.parent.optionbar.person[0].get(),                     #
                                     self.parent.optionbar.person[1].get(),                     #
                                     self.parent.optionbar.person[2].get(),                     #
                                     self.parent.optionbar.person[3].get(),                     #
                                     self.parent.optionbar.person[4].get(),                     #
                                 ))    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search