Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. #!/usr/bin/python
  2. import psycopg2
  3. #note that we have to import the pyscopg2 extras library!
  4. import psycopg2.extras
  5. import sys
  6.  
  7. def main():
  8.     #start the script
  9.  
  10.     conn_string = "host='localhost' dbname='my_database' user='postgres' password='secret'"
  11.     # print the connection string we will use to connect
  12.     print "Connecting to database\n ->%s" % (conn_string)
  13.     try:
  14.         # get a connection, if a connect cannot be made an exception will be raised here
  15.         conn = psycopg2.connect(conn_string)
  16.         # conn.cursor will return a cursor oject, you can use this query to perform queries
  17.         # note that in this example we pass a cursor_factory argument that will
  18.         # dictionary cursor so COLUMNS will be returned as a dictionary so we
  19.         # can access columns by their name instead of index.
  20.         cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  21.  
  22.         # tell postgres to use more work memory
  23.         work_mem = 2048
  24.  
  25.         # by passing a list as the 2nd argument to the execution function our
  26.         # %s string variable will get replaced with the order of variables in
  27.         # the list. In this case there is only 1 variable.
  28.         # Note that in python you specify a list with one item in it by placing
  29.         # a comma after the first variable and surrounding it in partenthesis.
  30.         cursor.execute('SET work_mem TO %s', (work_mem,))
  31.  
  32.         # Then we get the work memory we just set -> we know we only want the
  33.         # first ROW so we call fetchone.
  34.         # then we use bracket access to get the FIRST value.
  35.         # Note that even though we've returned the columns by name we can still
  36.         # access columns by numeric index as well - which is really nice.
  37.         cursor.execute('SHOW work_mem')
  38.  
  39.         # Call fetchone - which will fetch the first row returned from the
  40.         # database.
  41.         memory = cursor.fetchone()
  42.         # access the column by numeric index:
  43.         # even though we enabled columns by name I'm showning you this to
  44.         # show that you can still access columns by index and iterate over them.
  45.         print "Value: ", memory[0]
  46.         # print the entire row
  47.         print "Row: ", memory
  48.     except:
  49.         # Get the most recent exception
  50.         exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
  51.         # Exit the script and print an error telling what happened.
  52.         sys.exit("Database connection failed!\n ->%s" % (exceptionValue))
  53.  
  54.  
  55. if __name__ == "__main__":
  56.     sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement