Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. import psycopg2
  2.  
  3.  
  4. class MyDatabase(object):
  5.  
  6.   _INSERT_QUERY_TEMPLATE = "INSERT INTO mytable (name, surname) VALUES (%s, %s)"
  7.   _SELECT_QUERY = "SELECT id, name, surname FROM mytable"
  8.   _UPDATE_QUERY_TEMPLATE = "UPDATE mytable SET surname = %s WHERE name = %s)"
  9.   _DELETE_QUERY_TEMPLATE = "DELETE FROM mytable WHERE name = %s"
  10.  
  11.  
  12.   def __init__(self, user, password, dbname="postgres", host="127.0.0.1",
  13.       port=5432):
  14.     self._user = user
  15.     self._password = password
  16.     self._host = host
  17.     self._port = port
  18.     self._dbname = dbname
  19.     self._connection = None
  20.  
  21.  
  22.   def _GetConnection(self):
  23.     return psycopg2.connect(dbname=self._dbname, user=self._user,
  24.         password=self._password, host=self._host, port=self._port)
  25.  
  26.  
  27.   # Better to be more specific, like: InsertUser, or InsertPerson. Depends on
  28.   # the table you insert into.
  29.   def InsertValues(name, surname):
  30.     try:
  31.       with self._GetConnection() as conn:
  32.         cur = conn.cursor()
  33.         query = cur.mogrify(_INSERT_TEMPLATE, (name, surname))
  34.         cur.execute(query)
  35.     except Exception, e:
  36.       print "Failed to insert values:", name, surname
  37.       print e
  38.  
  39.  
  40.   def GetAllValues(self):
  41.     try:
  42.       with self._GetConnection() as conn:
  43.         cur = conn.cursor()
  44.         cur.execut(_SELECT_QUERY)
  45.         return cur.fetchall()
  46.     except Exception, e:
  47.       print "Failed to get values.", e
  48.  
  49.  
  50.   def UpdateSurnameByName(self, name, surname):
  51.     try:
  52.       with self._GetConnection() as conn:
  53.         cur = conn.cursor()
  54.         query = cur.mogrify(_UPDATE_QUERY_TEMPLATE, surname, name)
  55.         cur.execute(query)
  56.     except Exception, e:
  57.       print "Failed to update values:", name, surname
  58.       print e
  59.  
  60.  
  61.   def DeleteValuesByName(self, name):
  62.     try:
  63.       with self._GetConnection() as conn:
  64.         cur = conn.cursor()
  65.         query = cur.mogrify(_DELETE_QUERY_TEMPLATE, name)
  66.         cur.execute(query)
  67.     except Exception, e:
  68.       print "Failed to delete values with name =", name
  69.       print e
  70.  
  71.  
  72. def printValues(values):
  73.   for row in rows:
  74.     print "id = ", values[0]
  75.     print "name = ", values[1]
  76.     print "surname = ", values[2]
  77.     print "----------\n"
  78.  
  79.  
  80. def main():
  81.   # TODO: read configuration with ini parser.
  82.   user = "postgres"
  83.   passwd = "success"
  84.   db = MyDatabase(user=user, password=passwd)
  85.   db.InsertValues(name='John', surname='McRead')
  86.   printValues(fordb.GetAllValues())
  87.  
  88.   db.UpdateSurnameByName(name='John', surname='McDonald')
  89.   printValues(fordb.GetAllValues())
  90.  
  91.   db.DeleteValuesByName('John')
  92.   printValues(fordb.GetAllValues())
  93.  
  94.  
  95. if __name__ == "__main__":
  96.   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement