Skip to content

Instantly share code, notes, and snippets.

@yukpiz
Created May 8, 2015 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukpiz/efa73fedc7f1bebe0ca2 to your computer and use it in GitHub Desktop.
Save yukpiz/efa73fedc7f1bebe0ca2 to your computer and use it in GitHub Desktop.
Connection the PostgreSQL for Python.
#-*- encoding: utf-8 -*-
import psycopg2
class PostgreSQL:
connects = {
"host": "host"
"port": "5432",
"dbname": "dbname",
"user": "postgres",
"password": "postgres",
}
def __init__(self, connects=None):
print "\n<------ Initialize PostgreSQL Class ------>"
self.connects = self.connects if connects == None else connects
print "Connection parameters: %s" % self.connects
print "<End Initialize>\n"
def constring(self):
strings = []
for k in self.connects:
strings.append("%s=%s" % (k, self.connects[k]))
return " ".join(strings)
def connect(self):
print "<------ Connection PostgreSQL Database ------>"
self.con = psycopg2.connect(self.constring())
print "Connection string: %s" % self.constring()
print "<End Connection>\n"
def select(self, sql):
self.con.execute(sql)
def execute(self, sql):
self.con.execute(sql)
pg = PostgreSQL()
pg.connect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment