-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsql.py
37 lines (27 loc) · 1.14 KB
/
csql.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
"""
This python script can be used to get data from google cloud sql database
by querying with python package sqlalchemy.
"""
import sqlalchemy
import pandas as pd
# ----------------------------------------------------------------------------------------------------------------------
login = 'USERNAME' # Replace USERNAME with actual username
passwd = 'PASSWORD' # Replace PASSWORD with actual password
server = 'SEVER_IP:3306' # Replace SEVER_IP with actual server IP
db = 'Your database name' # Write your database name
# ----------------------------------------------------------------------------------------------------------------------
# Cloud SQL Query
engine_str = 'mysql+pymysql://{}:{}@{}/{}'.format(login, passwd, server, db)
engine = sqlalchemy.create_engine(engine_str)
query = """
SELECT
FROM
WHERE
"""
# ----------------------------------------------------------------------------------------------------------------------
# Get Query results as pandas DataFrame
result = engine.execute(query)
df = pd.DataFrame(result, columns=['col1', 'col2', ..., 'coln'])
print(df.head())
print(df.shape)
df.to_csv('csql_data.csv', index=False)