Get Data From Mysql Database Into Entry - The Advantages of Learning Python Programming for Beginners: A Comprehensive Guide

Latest

Dive into the world of Python programming with our comprehensive resources

Music

logo

Friday, February 16, 2024

Get Data From Mysql Database Into Entry

 

In this article, we will include the data in the MySql database into Entries. By clicking the Next button, we will show the next data in the Entries.

 


 









Figure 1

 



 








Figure 2

 

import mysql.connector

import tkinter  as tk

from tkinter import *

from tkinter import ttk

 

root = tk.Tk()

root.title("Next record..bs")

root.geometry("350x250")

 

con1 = mysql.connector.connect(

  host="localhost",

  user="root",

  passwd="2344",

  database="dbemployee"

)

 

cur1 = con1.cursor()

 

#we will use query with LIMIT to collect 1 records from the person table

 

cur1.execute("Select * From person LIMIT 1 OFFSET 0")

 

rows = cur1.fetchall()   

 

for row in rows:

 

 #print(row)

 

 lblId = Label(root, text = "Id : ", width=10,height=2,font=('Arial',12,'bold')).grid(column = 1, row = 0) 

 

 lblfName = Label(root, text = "First Name : ", width=10,height=2,font=('Arial',12,'bold')).grid(column = 1, row = 1) 

 

 lbllName = Label(root, text = "Last Name : ", width=10,height=2,font=('Arial',12,'bold')).grid(column = 1, row = 2) 

 

 lblMail = Label(root, text = "Mail : ", width=10,height=2,font=('Arial',12,'bold')).grid(column = 1, row = 3) 

 

 txtId = Entry(root, width=25,font=('Arial',12,'normal'), fg='blue')

 txtId.grid(row=0, column=10)

 txtId.insert(END, row[0])

 

 txtFirstName = Entry(root, width=25,font=('Arial',12,'normal'), fg='blue')

 txtFirstName.grid(row=1, column=10)

 txtFirstName.insert(END, row[1])

 

 txtLastName = Entry(root, width=25,font=('Arial',12,'normal'), fg='blue')

 txtLastName.grid(row=2, column=10)

 txtLastName.insert(END, row[2])

 

 txtMail = Entry(root, width=25,font=('Arial',12,'normal'), fg='blue')

 txtMail.grid(row=3, column=10)

 txtMail.insert(END, row[3])

 

 

def next():

 

  deg = 0

 

  global x

 

  x += 1

 

  cur1 = con1.cursor()

 

 

  cur1.execute("Select * From person ")

 

  rows = cur1.fetchall()

 

  #we use rowcount in mysql.

 

  rc = cur1.rowcount

 

  cur1.execute("Select * From person LIMIT 1 OFFSET "+ str(x))

 

  rows = cur1.fetchall()  

 

  for row in rows:

 

   print(row)

 

   if x == rc-1:

        x=-1

 

  txtId = Entry(root, width=25,font=('Arial',12,'normal'), fg='blue')

  txtId.grid(row=0, column=10)

  txtId.insert(END, row[deg])

 

  txtFirstName = Entry(root, width=25,font=('Arial',12,'normal'), fg='blue')

  txtFirstName.grid(row=1, column=10)

  txtFirstName.insert(END, row[deg+1])

 

  txtLastName = Entry(root, width=25,font=('Arial',12,'normal'), fg='blue')

  txtLastName.grid(row=2, column=10)

  txtLastName.insert(END, row[deg+2])

 

  txtMail = Entry(root, width=25,font=('Arial',12,'normal'), fg='blue')

  txtMail.grid(row=3, column=10)

  txtMail.insert(END, row[deg+3])

 

x = -1

 

next()

 

btn=tk.Button(root, text = "Next Record" , width=23,height=2, command=next,font=('Arial',12,'normal')).grid(column = 10, row = 5) 

 

root.mainloop() 

 

Hope to see you in the next article. Happy coding.

No comments:

Post a Comment

Featured post

Python Introduction

What is Python? Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built ...