Created
December 10, 2013 07:17
-
-
Save CaptSolo/7886852 to your computer and use it in GitHub Desktop.
Code snippet for a discussion
This file contains hidden or 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
# -*- coding: utf8 -*- | |
import psycopg2, psycopg2.extensions | |
import atexit | |
# no FAQ - lai simbolus atgriež kā python unicode tipu | |
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) | |
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY) | |
class MyApi(object): | |
def __init__(self, conn_info): | |
self.conn = psycopg2.connect( | |
host = conn_info["host"], | |
port = conn_info["port"], | |
dbname = conn_info["dbname"], | |
user = conn_info["user"], | |
password = conn_info["password"], | |
) | |
atexit.register(self.finalize) | |
def insertSomething(self, name): | |
cursor = self.conn.cursor() | |
cursor.execute("INSERT INTO Entities(Name) VALUES (%s) RETURNING EntityID;", (name,)) | |
entityid = cursor.fetchone()[0] | |
self.conn.commit() # ņem commit te ārā lai palielinātu ātrdarbību? | |
cursor.close() | |
return entityid | |
def finalize(self): | |
self.conn.commit() | |
self.conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Konekcija tiek padota
MyApi
kā parametrs. Viņas mūža ilgums tiek kontrolēts ārpusMyApi
, iespējams izmantojot atexit.Jādodās iesmelt iedvesma no kāda projekta! :D