UCONN

UCONN
UCONN

Wednesday, October 6, 2021

custadd, 10/6/2021, 8:30 PM, English, 10/6/2021, 8:30 PM

Your assigned language is: English
Classroom blog: googleclouduconn.blogspot.com

10/6/2021, 8:30 PM

# This file contains all the code used in the codelab.
import sqlalchemy

# Depending on which database you are using, you'll set some variables
differently.
# In this code we are inserting only one field with one value.
# Feel free to change the insert statement as needed for your own
table's requirements.

# Uncomment and set the following variables depending on your specific
instance and database:
connection_name = 'uconn-engr:us-central1:customer'
table_name = 'customer'
db_name = 'customer'
db_user = 'root'
db_password = 'uconnstamford'

# If your database is MySQL, uncomment the following two lines:
driver_name = 'mysql+pymysql'
query_string = dict({"unix_socket": "/cloudsql/{}".format(connection_name)})

# If your database is PostgreSQL, uncomment the following two lines:
#driver_name = 'postgres+pg8000'
#query_string = dict({"unix_sock":
"/cloudsql/{}/.s.PGSQL.5432".format(connection_name)})

# If the type of your table_field value is a string, surround it with
double quotes.

def custadd(request):
request_json = request.get_json()
request_data = request.values
email = request_data['myemail']
lname = request_data['mylastname']
fname = request_data['myfirstname']
addr = request_data['myaddress']
city = request_data['mycity']
state = request_data['mystcode']
zipcode = request_data['myzipcode']
stmt = sqlalchemy.text('insert into customer (email, LastName,
FirstName, Address, City, StateCode, ZipCode) values
("'+email+'","'+lname+'","'+fname+'","'+addr+'","'+city+'","'+state+'","'+zipcode+'")')





db = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL(
drivername=driver_name,
username=db_user,
password=db_password,
database=db_name,
query=query_string,
),
pool_size=5,
max_overflow=2,
pool_timeout=30,
pool_recycle=1800
)
try:
with db.connect() as conn:
conn.execute(stmt)
except Exception as e:
return 'Error: {}'.format(str(e))
return '<!DOCTYPE html><html><h1>You Entered</h1><br><p>Customer
Successfully Added</p></html>'

No comments:

Post a Comment

Disable Billing

Search for Billing Manage billing accounts Go to MYPROJECTS CLICK ON THE 3 BUTTON Actions Then hit disable