Aşağıdaki yazılı olan python kodun c# olanını forumda bir arkadaş istemişti, bende testi için python diline çevirdim. işine yarayan arkadaşlar için paylaşıyorum.
Python Mysql Tablosuna Veri Girişi Sorgulama Ve Gridde Gösterme
Bu Python kodu, MySQL veritabanı ve tablo oluşturup örnek veriler ekleyerek basit bir GUI uygulaması oluşturur.
Python'da gerekli kütüphaneler: mysql-connector-python ve tkinter. Kütüphaneleri yüklemek için aşağıdaki komutları kullanabilirsiniz:
pip install mysql-connector-python
Bu Python kodu, veritabanını ve tabloları oluşturur, örnek veriler ekler ve bir GUI oluşturur. Kullanıcı, öğrenci numarasını girip "Ara" butonuna bastığında, ilgili kitap ve ders bilgileri görüntülenir.
import mysql.connector
from mysql.connector import Error
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
def create_database_and_tables():
try:
con = mysql.connector.connect(host='localhost', user='root', password='')
cursor = con.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS ogrenci")
cursor.execute("USE ogrenci")
cursor.execute("""
CREATE TABLE IF NOT EXISTS kitap (
kitapno INT AUTO_INCREMENT PRIMARY KEY,
kitapadi VARCHAR(255),
tarih DATE,
ogrencino INT
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS ders (
dersno INT AUTO_INCREMENT PRIMARY KEY,
dersadi VARCHAR(255),
tarih DATE,
ogrencino INT
)
""")
cursor.execute("SELECT COUNT(*) FROM kitap")
count_books = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM ders")
count_courses = cursor.fetchone()[0]
if count_books == 0:
cursor.execute("INSERT INTO kitap (kitapadi, tarih, ogrencino) VALUES ('Kitap1', '2023-01-01', 1)")
cursor.execute("INSERT INTO kitap (kitapadi, tarih, ogrencino) VALUES ('Kitap2', '2023-01-02', 2)")
cursor.execute("INSERT INTO kitap (kitapadi, tarih, ogrencino) VALUES ('Kitap3', '2023-01-03', 3)")
cursor.execute("INSERT INTO kitap (kitapadi, tarih, ogrencino) VALUES ('Kitap4', '2023-01-04', 4)")
cursor.execute("INSERT INTO kitap (kitapadi, tarih, ogrencino) VALUES ('Kitap5', '2023-01-05', 5)")
if count_courses == 0:
cursor.execute("INSERT INTO ders (dersadi, tarih, ogrencino) VALUES ('Ders1', '2023-01-01', 1)")
cursor.execute("INSERT INTO ders (dersadi, tarih, ogrencino) VALUES ('Ders2', '2023-01-02', 2)")
cursor.execute("INSERT INTO ders (dersadi, tarih, ogrencino) VALUES ('Ders3', '2023-01-03', 3)")
cursor.execute("INSERT INTO ders (dersadi, tarih, ogrencino) VALUES ('Ders4', '2023-01-04', 4)")
cursor.execute("INSERT INTO ders (dersadi, tarih, ogrencino) VALUES ('Ders5', '2023-01-05', 5)")
con.commit()
cursor.close()
con.close()
except Error as e:
print(f"Error: {e}")
def display_books(student_id):
try:
con = mysql.connector.connect(host='localhost', database='ogrenci', user='root', password='')
cursor = con.cursor()
cursor.execute("SELECT kitapno, kitapadi, tarih FROM kitap WHERE ogrencino = %s", (student_id,))
rows = cursor.fetchall()
con.close()
return rows
except Error as e:
print(f"Error: {e}")
return []
def display_courses(student_id):
try:
con = mysql.connector.connect(host='localhost', database='ogrenci', user='root', password='')
cursor = con.cursor()
cursor.execute("SELECT dersno, dersadi, tarih FROM ders WHERE ogrencino = %s", (student_id,))
rows = cursor.fetchall()
con.close()
return rows
except Error as e:
print(f"Error: {e}")
return []
def search():
student_id = entry_student_id.get()
if not student_id:
messagebox.showwarning("Uyarı", "Lütfen bir öğrenci numarası girin.")
return
books = display_books(student_id)
for row in tree_books.get_children():
tree_books.delete(row)
for book in books:
tree_books.insert('', 'end', values=book)
courses = display_courses(student_id)
for row in tree_courses.get_children():
tree_courses.delete(row)
for course in courses:
tree_courses.insert('', 'end', values=course)
create_database_and_tables()
app = tk.Tk()
app.title("Öğrenci Bilgileri")
frame = tk.Frame(app)
frame.pack(pady=10)
lbl_student_id = tk.Label(frame, text="Öğrenci Numarası:")
lbl_student_id.grid(row=0, column=0, padx=5)
entry_student_id = tk.Entry(frame)
entry_student_id.grid(row=0, column=1, padx=5)
btn_search = tk.Button(frame, text="Ara", command=search)
btn_search.grid(row=0, column=2, padx=5)
tree_books = ttk.Treeview(app, columns=("kitapno", "kitapadi", "tarih"), show='headings')
tree_books.heading("kitapno", text="Kitap No")
tree_books.heading("kitapadi", text="Kitap Adı")
tree_books.heading("tarih", text="Tarih")
tree_books.pack(pady=10)
tree_courses = ttk.Treeview(app, columns=("dersno", "dersadi", "tarih"), show='headings')
tree_courses.heading("dersno", text="Ders No")
tree_courses.heading("dersadi", text="Ders Adı")
tree_courses.heading("tarih", text="Tarih")
tree_courses.pack(pady=10)
app.mainloop()