Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. from texttable import Texttable
  2. import MySQLdb
  3.  
  4. USERNAME = 'root'
  5. PASSWORD = 'password'
  6. DB_NAME = 'DBECE4813'
  7.  
  8. print "Connecting to RDS instance"
  9.  
  10. #Insert host url from RDS
  11. conn = MySQLdb.connect ( host = "<RDS-INSTANCE-HOST>",
  12. user = USERNAME,
  13. passwd = PASSWORD,
  14. db = DB_NAME,
  15. port = 3306
  16. )
  17.  
  18. print "Connected to RDS instance"
  19.  
  20. cursor = conn.cursor ()
  21. cursor.execute ("SELECT VERSION()")
  22. row = cursor.fetchone ()
  23. print "server version:", row[0]
  24.  
  25.  
  26. print "\nCreating Table Student. Adding 5 Students to the table"
  27. cursor.execute ("CREATE TABLE Student(sId INT NOT NULL, Name TEXT NOT NULL, LastName TEXT NOT NULL, Major TEXT NOT NULL, GPA FLOAT, PRIMARY KEY (sId)) ")
  28.  
  29. cursor.execute ("INSERT INTO Student VALUES(100, 'John', 'Martin', 'CS', 3)")
  30. cursor.execute ("INSERT INTO Student VALUES(101, 'David', 'Kennerly', 'ECE', 3.5)")
  31. cursor.execute ("INSERT INTO Student VALUES(102, 'Bob', 'Reeves', 'CS', 3.9)")
  32. cursor.execute ("INSERT INTO Student VALUES(103, 'Alex', 'Han', 'CS', 3.6)")
  33. cursor.execute ("INSERT INTO Student VALUES(104, 'Martin', 'Lorenz', 'ECE', 3.1)")
  34.  
  35. cursor.execute("SELECT * FROM Student")
  36. rows = cursor.fetchall()
  37.  
  38. print "\nPrinting Students:"
  39.  
  40. table = Texttable()
  41. content_table = [['Student ID', 'Name', 'Last Name', 'Major', 'GPA']]
  42.  
  43. for row in rows:
  44. content_table.append([int(row[0]), row[1], row[2], row[3], row[4]])
  45.  
  46. table.add_rows(content_table)
  47. print table.draw()
  48.  
  49. print "\nDropping Table..."
  50. cursor.execute ("DROP TABLE Student")
  51. print "\nTable Dropped\n"
  52.  
  53. cursor.close()
  54. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement