Created
May 8, 2015 08:47
-
-
Save yukpiz/efa73fedc7f1bebe0ca2 to your computer and use it in GitHub Desktop.
Connection the PostgreSQL for Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- 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