-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_connection.py
164 lines (121 loc) · 4.63 KB
/
my_connection.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# [3:59 AM, 2/18/2024] Swapnil Mane CS Bing: integral-vim-230317:us-central1:sem-hack-bu
# [3:59 AM, 2/18/2024] Swapnil Mane CS Bing: 34.123.254.183
# [3:59 AM, 2/18/2024] Swapnil Mane CS Bing: SQL Instance
# user id: sem-hack-bu
# SemHack
import mysql.connector
# GCP MySQL instance details
gcp_mysql_config = {
'user': 'root',
'password': 'SemHack',
'host': '34.123.254.183',
'database': 'myexpense',
'raise_on_warnings': True,
}
import os
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service_account.json"
from google.cloud.sql.connector import Connector, IPTypes
import pymysql
import sqlalchemy
def connect_with_connector() -> sqlalchemy.engine.base.Engine:
"""
Initializes a connection pool for a Cloud SQL instance of MySQL.
Uses the Cloud SQL Python Connector package.
"""
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
# keep secrets safe.
instance_connection_name = "integral-vim-230317:us-central1:sem-hack-bu" # e.g. 'project:region:instance'
db_user = "root" # e.g. 'my-db-user'
db_pass = 'SemHack' # e.g. 'my-db-password'
db_name = 'myexpense' # e.g. 'my-database'
ip_type = IPTypes.PRIVATE if os.environ.get("PRIVATE_IP") else IPTypes.PUBLIC
connector = Connector(ip_type)
# def getconn() -> pymysql.connections.Connection:
conn: pymysql.connections.Connection = connector.connect(
instance_connection_name,
"pymysql",
user=db_user,
password=db_pass,
db=db_name,
)
return conn
# pool = sqlalchemy.create_engine(
# "mysql+pymysql://",
# creator=getconn,
# # ...
# )
# return pool
def insert_into_customer(data):
try:
connection = connect_with_connector()
# Create a cursor object
cursor = connection.cursor()
# SQL query for inserting a new record
insert_query = "INSERT INTO Customer (Full_Name, email_address, password, Income_amount, Saving_Percentage) VALUES (%s, %s, %s, %s, %s)"
# Data to be inserted
# data = ('Jane Doe', '[email protected]', 'newpassword', 60000.00, 8.75)
# Execute the query
cursor.execute(insert_query, data)
# Commit the changes
connection.commit()
finally:
# Close the cursor and connection
cursor.close()
connection.close()
# data = ('Anagha', '[email protected]', 'newpassword', 6000.00, 10.75)
# insert_into_customer(connection, data)
def insert_into_transactions(data):
try:
connection = connect_with_connector()
# Create a cursor object
cursor = connection.cursor()
# SQL query for inserting a new record
insert_query = "INSERT INTO Transactions (Date, Description, Category, Cost, Currency, customer_id) VALUES (%s, %s, %s, %s, %s, %s)"
# Data to be inserted
# data = ('Jane Doe', '[email protected]', 'newpassword', 60000.00, 8.75)
# Execute the query
cursor.execute(insert_query, data)
# Commit the changes
connection.commit()
finally:
# Close the cursor and connection
cursor.close()
connection.close()
def select_all(query):
connection = connect_with_connector()
try:
# Create a cursor object
cursor = connection.cursor()
# SQL query for selecting data
# select_query = "SELECT * FROM " + table_name
# Execute the query
cursor.execute(query)
# Fetch all rows
rows = cursor.fetchall()
return rows
finally:
# Close the cursor and connection
cursor.close()
connection.close()
# print(select_all(connection, "Customer"))
def update(table_name, columns, data, condition):
connection = connect_with_connector()
try:
# Create a cursor object
cursor = connection.cursor()
# SQL query for updating data
update_query = "UPDATE " + table_name + " SET " + columns + " = %s WHERE "+ condition
# New data and condition
new_value = 'new_value'
condition_value = 'condition_value'
# Execute the query
cursor.execute(update_query, (new_value, condition_value))
# Commit the changes
connection.commit()
finally:
# Close the cursor and connection
cursor.close()
connection.close()