-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_filler.py
51 lines (42 loc) · 1.5 KB
/
database_filler.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
import sqlite3
import pyodbc
import pandas as pd
# SQLite veritabanına bağlan
sqlite_conn = sqlite3.connect('./Source/Formula1.sqlite')
# SQLite veritabanındaki tüm tablo isimlerini al
cursor = sqlite_conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
tables = [table[0] for table in tables]
mssql_conn = pyodbc.connect(
'DRIVER={ODBC Driver 17 for SQL Server};'
'SERVER=IAMEHMETOZDEMIR\\SQLEXPRESS;'
'DATABASE=Formula;'
'UID=sa;'
'PWD=iamehmetozdemir'
)
for table in tables:
if not table == 'results':
continue
print(f"Reading {table} table from SQLite...")
sqlite_cursor = sqlite_conn.cursor()
sqlite_cursor.execute(f"SELECT * FROM {table};")
data = sqlite_cursor.fetchall()
columns = [desc[0] for desc in sqlite_cursor.description]
print(f"Writing {table} table to MSSQL Server...")
mssql_cursor = mssql_conn.cursor()
for row in data:
query = f"INSERT INTO {table} ({', '.join(columns)}) VALUES ("
for i, value in enumerate(row):
if type(value) == str:
query += f"'{value}'"
else:
query += str(value)
if i != len(row) - 1:
query += ", "
query += ");"
print(query)
with open("errors.txt", "a") as f:
query = query.encode("ascii", "ignore").decode("ascii")
f.write(query + "\n")
print("All tables are written to MSSQL Server.")