upGradGPT/GPT_Interview_beta
1
1import pymysql2#We will use connect() to connect to RDS Instance3#host is the endpoint of your RDS instance4#user is the username you have given while creating the RDS instance5#Password is Master pass word you have given 6db = pymysql.connect(host="database-gpt.cjdcshirzwmk.us-east-1.rds.amazonaws.com", user = "admin123", password="admin123", port=3306)7# you have cursor instance here8cursor = db.cursor()9cursor.execute("select version()")10#now you will get the version of MYSQL you have selected on instance11data = cursor.fetchone()12#Lets's create a DB13# sql = '''create database kTestDb'''14# cursor.execute(sql)15# cursor.connection.commit()16db.select_db("kTestDb")17#Create a table 18sql = '''19create table person ( id int not null auto_increment,fname text, lname text, primary key (id) )'''20cursor.execute(sql)21#Check if our table is created or not 22sql = '''show tables'''23cursor.execute(sql)24cursor.fetchall()25#Output of above will be (('person',),)26#Insert some records in the table 27sql = ''' insert into person(fname, lname) values('%s', '%s')''' % ('XXX', 'YYY')28cursor.execute(sql)29db.commit()30#Lets select the data from above added table31sql = '''select * from person'''32cursor.execute(sql)33cursor.fetchall()34#Output of above will be ((1, 'XXX', 'YYY'),)