seriouspark commited on
Commit
7b1cbee
β€’
1 Parent(s): 0aa934a

sql_uploader and practier

Browse files
Files changed (4) hide show
  1. app.py +21 -0
  2. make_db.py +81 -0
  3. requirements.txt +5 -0
  4. sql_training.py +30 -0
app.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit import config
3
+ import make_db
4
+ #import text2sql
5
+ import sql_training
6
+
7
+
8
+ # νŽ˜μ΄μ§€ μ„€μ •
9
+ #st.set_page_config(page_title='text2sql', layout = 'wide')
10
+
11
+ PAGES = {
12
+ 'Excel to DataBase': make_db,
13
+ 'SQL Training' : sql_training,
14
+ # 'Text2SQL' : text2sql
15
+ }
16
+
17
+ st.sidebar.title('메뉴')
18
+ selection = st.sidebar.radio('Go to', list(PAGES.keys()))
19
+
20
+ page = PAGES[selection]
21
+ page.app()
make_db.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import sqlite3
4
+ import os
5
+ from datetime import datetime
6
+
7
+
8
+ def app():
9
+ st.title('Excel to DataBase')
10
+ st.write('엑셀을 λ„£μ–΄ λ°μ΄ν„°λ² μ΄μŠ€λ₯Ό λ§Œλ“€μ–΄λ΄…μ‹œλ‹€.')
11
+ file_name = st.text_input('파일λͺ… μ§€μ •ν•˜κΈ°')
12
+ # μ—‘μ…€ 파일 μ—…λ‘œλ“œ
13
+ uploaded_file = st.file_uploader('Choose an Excel file', type = ['xlsx','xls','csv'])
14
+
15
+ if uploaded_file is not None:
16
+ # μ—‘μ…€ νŒŒμΌμ„ λ°μ΄ν„°ν”„λ ˆμž„μœΌλ‘œ λ³€ν™˜
17
+ try:
18
+ df = pd.read_csv(uploaded_file)
19
+
20
+ except:
21
+ df = pd.read_excel(uploaded_file)
22
+
23
+
24
+ # 각 열에 λŒ€ν•œ 데이터 νƒ€μž… 선택 μ˜΅μ…˜ 제곡
25
+ data_types = {'object': 'String (TEXT)',
26
+ 'float' : 'Float (REAL)',
27
+ 'int' : 'Integer (INT)',
28
+ 'datetime': 'Datetime (TEXT)',
29
+ 'bool' : 'Bool',
30
+ }
31
+ selected_data_types = {}
32
+ for column in df.columns:
33
+ data_type = st.selectbox(f"SELECT data type for column '{column}'",
34
+ options = list(data_types.keys()),
35
+ format_func = lambda x : data_types[x],
36
+ key = column)
37
+ selected_data_types[column] = data_type
38
+ # μ›λž˜ int / float 것듀 μ€‘μ—μ„œ object 둜 λ³€ν™˜ν•΄μ•Ό ν•  것듀은 object 둜 λ°”κΎΈμ–΄μ£ΌκΈ°
39
+ if st.button('데이터 λ³€ν™˜ν•˜κ³  μ €μž₯ν•˜κΈ°'):
40
+ for column, data_type in selected_data_types.items():
41
+ if data_type == 'float':
42
+ try:
43
+ df[column] = df[column].str.replace(',','')
44
+ except:
45
+ continue
46
+ df[column] = pd.to_numeric(df[column], errors = 'coerce')
47
+ elif data_type == 'int':
48
+ try:
49
+ df[column] = df[column].str.replace(',','')
50
+ except:
51
+ continue
52
+ df[column] = pd.to_numeric(df[column].str.replace(',',''), errors = 'coerce').fillna(0).astype(int)
53
+ elif data_type == 'datetime':
54
+ df[column] = pd.to_datetime(df[column], errors = 'coerce')
55
+ elif data_type == 'bool':
56
+ df[column] = df[column].astype(bool)
57
+ elif data_type == 'object':
58
+ if df[column].dtypes == float or df[column].dtypes == int:
59
+ df[column] = df[column].astype(str).str.replace('.0','')
60
+ else:
61
+ df[column] = df[column]
62
+
63
+ next = True
64
+
65
+
66
+ if next:
67
+ # sql lite λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²° 및 생성
68
+ conn = sqlite3.connect(file_name)
69
+ c = conn.cursor()
70
+
71
+ # λ°μ΄ν„°ν”„λ ˆμž„μ„ SQLν…Œμ΄λΈ”λ‘œ λ³€ν™˜
72
+
73
+ df.to_sql(f'{file_name}', conn, dtype = selected_data_types, if_exists = 'replace', index = False)
74
+
75
+ st.success(f'νŒŒμΌμ€ μ„±κ³΅μ μœΌλ‘œ λ°μ΄ν„°λ² μ΄μŠ€λ‘œ μ €μž₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€. λ°μ΄ν„°λ² μ΄μŠ€λͺ… [{file_name}]')
76
+
77
+ # μ—°κ²° μ’…λ£Œ
78
+ conn.close()
79
+
80
+
81
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ openpyxl
5
+ pandas
sql_training.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import sqlite3
4
+ from datetime import datetime
5
+
6
+ def app():
7
+ st.title('SQL Training')
8
+ st.write('SQL μ—°μŠ΅μ„ ν•΄λ΄…μ‹œλ‹€.')
9
+ file_name = st.text_input('file name:', )
10
+
11
+ # μ—‘μ…€ 파일 μ—…λ‘œλ“œ
12
+
13
+
14
+ user_query = st.text_area('Enter your SQL query:', height = 100)
15
+ if st.button('쿼리 μ‹€ν–‰'):
16
+ try:
17
+ # 쿼리 μ‹€ν–‰ 및 κ²°κ³Ό 좜λ ₯
18
+ conn = sqlite3.connect(file_name)
19
+ c = conn.cursor()
20
+ query_results = pd.read_sql_query(user_query, conn)
21
+ if not query_results.empty:
22
+ st.dataframe(query_results)
23
+ else:
24
+ st.write('μΏΌλ¦¬λŠ” μ„±κ³΅μ μœΌλ‘œ μ‹€ν–‰λ˜μ—ˆμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ κ²°κ³Όκ°€ μ—†λ„€μš”.')
25
+ except Exception as e:
26
+ st.error(f'μ—λŸ¬κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}')
27
+ finally:
28
+ conn.close()
29
+
30
+