-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
152 lines (131 loc) · 3.99 KB
/
Copy pathutils.py
File metadata and controls
152 lines (131 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# TODO İki tane olan get_data kodu teke düsürülebilir
import streamlit as st
import mysql.connector
import pandas as pd
import re
import config as c
from mysql.connector import Error
#Giriş bilgileri
'''db_config = {
'user': 'root',
'password': 'umut',
'host': '127.0.0.1',
'database': 'bil372_project',
}'''
def format_time(time_str):
"""Saat formatını düzenler (09.00 -> 9, 17.00 -> 17)."""
try:
return int(time_str.split('.')[0]) # Saat kısmını integer'a çevir
except ValueError:
return time_str # Eğer format doğru değilse orijinal değeri döndür
# MySQL bağlantısı oluştur
def create_connection():
return mysql.connector.connect(**c.Config.db_config)
# MySQL veritabanından veri almak için fonksiyon
# TODO Bu get_data versiyonu silinecek
def get_data(query):
conn = create_connection()
df = pd.read_sql(query, conn)
conn.close()
return df
def update_data(query, params):
"""
Veri tabanında güncelleme yapar.
Args:
query (str): Güncelleme sorgusu. Sorguda %s yer tutucuları kullanılabilir.
params (tuple): Sorguda yer tutucular için değerler.
"""
conn = create_connection()
if conn is None:
return
try:
cursor = conn.cursor()
cursor.execute(query, params)
conn.commit() # Değişiklikleri kaydeder
print(f"{cursor.rowcount} kayıt güncellendi.")
except Error as e:
print(f"Bir hata oluştu: {e}")
conn.rollback() # Hata durumunda geri alır
finally:
cursor.close()
conn.close()
def insert_data(query, params):
"""
Veri tabanına yeni bir satır ekler.
Args:
query (str): Ekleme sorgusu. Sorguda %s yer tutucuları kullanılabilir.
params (tuple): Sorguda yer tutucular için değerler.
"""
conn = create_connection()
if conn is None:
return
try:
cursor = conn.cursor()
cursor.execute(query, params)
conn.commit() # Değişiklikleri kaydeder
print(f"Yeni satır eklendi, ID: {cursor.lastrowid}")
except Error as e:
print(f"Bir hata oluştu: {e}")
conn.rollback() # Hata durumunda geri alır
finally:
cursor.close()
conn.close()
def get_highest_id(table_name, id_ismi):
"""
Belirtilen tabloda en yüksek ID değerini getirir.
Args:
table_name (str): Üzerinde sorgulama yapılacak tablo adı.
Returns:
int: En yüksek ID değeri. Eğer hata oluşursa veya tablo boşsa None döner.
"""
conn = create_connection()
if conn is None:
return None
try:
cursor = conn.cursor()
query = f"SELECT MAX({id_ismi}) FROM {table_name}"
cursor.execute(query)
result = cursor.fetchone()
highest_id = result[0] if result[0] is not None else None
return highest_id
except Error as e:
print(f"Bir hata oluştu: {e}")
return None
finally:
cursor.close()
conn.close()
def get_data(query, params=None):
conn = create_connection()
if conn is None:
return None
try:
cursor = conn.cursor(dictionary=True)
if params:
cursor.execute(query, params)
else:
cursor.execute(query)
result = cursor.fetchall()
df = pd.DataFrame(result)
return df
except mysql.connector.Error as e:
st.error(f"Bir hata oluştu: {e}")
return None
finally:
cursor.close()
conn.close()
# Veritabanından veri silme fonksiyonu
def delete_data(query, params):
conn = create_connection()
if conn is None:
return
try:
cursor = conn.cursor()
cursor.execute(query, params)
conn.commit() # Değişiklikleri kaydeder
print(f"{cursor.rowcount} kayıt silindi.")
except Error as e:
print(f"Bir hata oluştu: {e}")
conn.rollback() # Hata durumunda geri alır
finally:
cursor.close()
conn.close()