-
Notifications
You must be signed in to change notification settings - Fork 0
/
excelToSqlOnlyforLearn.py
73 lines (53 loc) · 1.79 KB
/
excelToSqlOnlyforLearn.py
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
import pandas as pd
import pyodbc
import tkinter as tk
from tkinter import filedialog
svr_name = 'localhost, 1433'
u_name = 'as'
u_password = '123456a@'
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
print(file_path)
data_frame = pd.read_excel(file_path)
df = pd.DataFrame(data_frame)
try:
connectSever = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server}' +
';SERVER=' + 'localhost, 1433' +
# ';Database= QLSV' +
';UID=' + 'sa' +
';PWD=' + '123456a@;' +
'Authentication type = SQL Login;')
except NameError:
print(NameError)
print('accept connect')
cursor = connectSever.cursor()
cursor.execute('USE QLSV')
cursor.execute('SELECT * FROM SinhVien')
list1 = ['MaSV', 'HotenSV', 'GioiTinh', 'NgaySinh', 'QueQuan', 'Email']
for row in df.itertuples():
sql = '''INSERT INTO SINHVIEN ({}) VALUES ({})'''.format(
','.join(list1), ','.join(['?']*len(df.columns)))
cursor.execute(sql, tuple(row[1:]))
# for row in df.itertuples():
# cursor.execute('''
# INSERT INTO SINHVIEN (MaSV, HotenSV ,GioiTinh , NgaySinh, QueQuan, Email)
# VALUES (?,?,?,?,?,?)
# ''',
# row.MaSV,
# row.HoTenSV,
# row.gioitinh,
# row.ngaysinh,
# row.QueQuan,
# row.email
# )
# for row in df.itertuples():
# cursor.execute('''
# INSERT INTO MONHOC (MaMH, tenMH ,DVHT)
# VALUES (?,?,?)
# ''',
# row.MaMH,
# row.TenMH,
# row.DVHT
# )
cursor.commit()