I am creating a GUI for all database queries such as to create database, create table and insert record into table. But I am stuck in creating database as I want that all values like : host name, username, password and database name should be enter on gui window.
I tried this code
import numpy as np
from tkinter import *
from tkinter.filedialog import askopenfile
import importlib
import os
import tkinter
import mysql.connector as msql
from mysql.connector import Error
import pandas as pd
root = Tk()
root.geometry("500x500")
root.config(bg="light gray")
# declaring string variable
host_var = StringVar()
user_var = StringVar()
pass_var = StringVar()
db_name = StringVar()
def Submit_db():
host = host_var.get()
user = user_var.get()
pas = pass_var.get()
db = db_name.get()
print("Host name is"+host)
print("user name is"+user)
print("pass name is"+pas)
print("db name is"+db)
try:
conn = msql.connect(host=host, user=user, password=pas)
if conn.is_connected():
cursor = conn.cursor()
cursor.execute("CREATE DATABASE "+ db)
print(db+"database is created")
except Error as e:
print("Error while connecting to MySQL", e)
def create_db():
root_db=Tk()
root_db.geometry("350x350")
Label(root_db,text="Host").grid(row=0,column=1)
entry1= Entry(root_db,textvariable = host_var).grid(row=0,column=2)
Label(root_db,text="User").grid(row=1,column=1)
entry2 = Entry(root_db, textvariable = user_var).grid(row=1,column=2)
Label(root_db,text="Password").grid(row=2,column=1)
entry3 = Entry(root_db,textvariable= pass_var, show="*").grid(row=2,column=2)
Label(root_db, text = "Database Name").grid(row=3,column=1)
entry4 = Entry(root_db, textvariable= db_name).grid(row=3,column=2)
Button(root_db, text= "Submit",command=Submit_db).grid(row=4,column=1)
root_db.mainloop()
label1 = Label(root, text="Select your choice", font="bold")
label1.grid(row=0,column=1)
db_button = Button(root,text="Create database ",command=create_db)
db_button.grid(row=2,column=1)
table_botton = Button(root,text="Create table",font="bold").grid(row=2,column=2)
record_table=Button(root,text="Insert record",font="bold").grid(row=2,column=3)
root.mainloop()
but it gives the error:
Host name is
user name is
pass name is
db name is
Error while connecting to MySQL 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)
It looks like it does not took the values from the entry window as print function returns nothing. I may be make mistake in submit button. But i have no idea how to connect them with each other to get it worked.
Tk()
. UseToplevel()
insidecreate_db()
. – acw1668 Jan 23 at 9:27