-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickDb.py
36 lines (28 loc) · 1017 Bytes
/
QuickDb.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
import sqlite3
import pandas as pd
# establish a connection to the SQLite database
conn = sqlite3.connect('mydatabase.db')
# create a pandas DataFrame
df = pd.DataFrame()
sheet_id = '1bNuRvFZHQGvprpVO1Vag0LgJRkSzjkiZV4XSrvzcVLQ'
sheet_name = 'Sheet1'
url = f'https://docs.google.com/spreadsheets/d/{sheet_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}'
df = pd.read_csv(url)
df.columns = map(str.title, df.columns)
df.columns = df.columns.str.replace(' ','_')
df.columns = df.columns.str.replace('[^a-zA-Z_]', '', regex=True)
df = df.replace('[^0-9a-zA-Z,_-]+', ' ', regex=True)
display(df)
# write the DataFrame to the database table
df.to_sql('us_ships', conn, if_exists='replace', index=False)
# close the connection
conn.close()
# establish a connection to the SQLite database
conn = sqlite3.connect('mydatabase.db')
# execute a SELECT query
query = "SELECT * FROM us_ships"
results = pd.read_sql_query(query, conn)
# close the connection
conn.close()
# print the results as a pandas DataFrame
print(results)