SHARE
TWEET

Untitled

a guest Oct 3rd, 2017 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import psycopg2
  3. import sys
  4. import pprint
  5.  
  6.  
  7. class ConnectionTest():
  8.     def __init__(self):
  9.         #start of script
  10.         #Define our connection string
  11.         databaseName = 'kexp'
  12.         databaseUser = 'kexp_user'
  13.         databasePswd = 'kexp_pass'
  14.         databaseHost = 'localhost'
  15.         conn_string = "dbname='%s' user='%s' password='%s' host='%s'" % (databaseName, databaseUser, databasePswd, databaseHost)
  16.        
  17.         # print the connection string we will use to connect
  18.         print "Connecting to database\n ->%s" % (conn_string)
  19.         try:
  20.             # get a connection, if a connect cannot be made an exception will be raised here
  21.             self.conn = psycopg2.connect(conn_string)
  22.            
  23.             # conn.cursor will return a cursor object, you can use this cursor to perform queries
  24.             print "Connected!\n"
  25.         except:
  26.             # Get the most recent exception
  27.             exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
  28.             # Exit the script and print an error telling what happened.
  29.             sys.exit("Database connection failed!\n ->%s" % (exceptionValue))
  30.    
  31.     def runCommand(self):
  32.         try:
  33.             print "fuck"
  34.             cursor = self.conn.cursor()
  35.             cursor.execute(
  36.                 """INSERT INTO kexp_test (artist, songtitle)
  37.                    VALUES (%s, %s);""",
  38.                 ('beyonce', 'fuck you'))
  39.             records = self.cursor.fetchall()
  40.             print "results:"
  41.             for row in records:
  42.                 print "   ", row[0]
  43.         except:
  44.             # Get the most recent exception
  45.             exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
  46.             # Exit the script and print an error telling what happened.
  47.             sys.exit("Database connection failed!\n ->%s" % (exceptionValue))
  48.        
  49.  
  50.     if __name__ == "__init__":
  51.         sys.exit(__init__)
  52.        
  53. x = ConnectionTest()
  54.  
  55. #while 3 > 1:
  56. #    command = raw_input('\nPlease enter a query:')
  57. #    command = "\"" + command + "\""
  58. x.runCommand() #"INSERT INTO kexp_test ( artist, songtitle ) VALUES (  'beyonce', 'crazy in love' );"
  59.    
  60.     #SELECT * from kexp_test
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top