upGradGPT/GPT_Interview_beta
1
1import pymysql2db = pymysql.connect(host="database-gpt.cjdcshirzwmk.us-east-1.rds.amazonaws.com", user="admin123", password="admin123", port=3306)3cursor = db.cursor()4 5# Use the correct database6 7try:8 db.select_db('gpt_interview_data')9 print("Connection successful")10except Exception as e:11 print(e)12 print("DB connection error")13 exit(1)14 15# Insert a new row into table1 and return the session ID16sql_insert = '''17START TRANSACTION;18INSERT INTO table1 (session_id, rating)19SELECT t.count + 1, 520FROM (SELECT COUNT(*) AS count FROM table1) AS t;21 22SELECT COUNT(*) FROM table1;23COMMIT;24'''25 26try:27 cursor.execute(sql_insert)28 session_id = cursor.fetchone()[0]29 print("New session ID:", session_id)30except Exception as e:31 print(e)32 print("Error inserting row")33 exit(1)34 35# Get the total number of rows in table136sql_count = "SELECT COUNT(*) FROM table1"37try:38 cursor.execute(sql_count)39 count = cursor.fetchone()[0]40 print("Total rows in table1:", count)41except Exception as e:42 print(e)43 print("Error getting row count")44 exit(1)45 46db.close()47 