Python Forum
Migrating to Mysql from SQlite
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Migrating to Mysql from SQlite
#11
First, I would like to thank Buran for his support.

Secondly, attached is the working code for anyone whom is interested. I have removed "create user" option for simplicity.
#imports
#don't forget to dos>pip install mysql-connector-python
from tkinter import *
from tkinter import messagebox as ms
import mysql.connector as mysql
 
mydb = mysql.connect(
    host="localhost",
    user="goktan",
    passwd="nopassword64",
    database="testdb"
)
#main Class
class main:
    def __init__(self,master):
        # Window 
        self.master = master
        # Some Usefull variables
        self.username = StringVar()
        self.password = StringVar()
        self.n_username = StringVar()
        self.n_password = StringVar()
        #Create Widgets
        self.widgets()
 
    #Login Function
    def login(self):
        #Establish Connection
        cursor = mydb.cursor()
        #Find user If there is any take proper action
        find_user = ("SELECT * FROM user WHERE username = %s and password = %s")
        cursor.execute(find_user, [(self.username.get()), (self.password.get())])
        result = cursor.fetchall()
        if result:
            self.logf.pack_forget()
            self.head['text'] = self.username.get() + '\n Loged In'
            self.head['pady'] = 150
        else:
            ms.showerror('Oops!','Username Not Found.')
                
        #Frame Packing Methords
    def log(self):
        self.username.set('')
        self.password.set('')
        self.crf.pack_forget()
        self.head['text'] = 'LOGIN'
        self.logf.pack()
         
    #Draw Widgets
    def widgets(self):
        self.head = Label(self.master,text = 'LOGIN',font = ('',35),pady = 10)
        self.head.pack()
        self.logf = Frame(self.master,padx =10,pady = 10)
        Label(self.logf,text = 'Username: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
        Entry(self.logf,textvariable = self.username,bd = 5,font = ('',15)).grid(row=0,column=1)
        Label(self.logf,text = 'Password: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
        Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1)
        Button(self.logf,text = ' Login ',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.login).grid()
        self.logf.pack()
  
#create window and application object
root = Tk()
root.title("Login Form")
main(root)
root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad Migrating of python2 script to python3 zuri 7 992 Oct-05-2023, 02:40 PM
Last Post: snippsat
  Mysql and mysql.connector error lostintime 2 692 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Migrating data from oracle into postgres python_student 1 2,454 Feb-10-2022, 09:16 PM
Last Post: buran
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,138 Feb-09-2022, 09:55 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020