2016-04-26 4 views
0

Ich habe diese Mauer, ich versuche zu überwinden. Allerdings nehme ich diese Route (Single Connection Sharing), weil der vorherige Ansatz (Mehrfachverbindung) fehlgeschlagen ist, als ich auf eine bestimmte Tabelle mit anderen Verbindungsobjekt von einer anderen Methode zugreifen musste. Also, im Prinzip, mit Singleton Verbindung Ansatz, siehe Beispielcode unten;Sharing single POSTGRESQL Verbindung zwischen mehreren Methoden der gleichen Klasse

import sys, psycopg2 


class myclass(): 
    def __inti__(self): 
     pass 

# Declaring DB connection 

def dbconn(self): 
    try: 
     connect_string = psycopg2.connect(host='localhost', database='', user='',password='') 
     self.con = connect_string.cursor() 
    except: 
     print('Connection Error') 

    # return dbconn 
    print(self.con, 'this is at dbconn method level') 
    return self.con 


def registration(self): 
    """ Detail of our Contacts is being collected""" 

    print('Enter 1 or 2 to update Network Provider) 
    update_data = int(input('Enter your 1 or 2 :')) 
    if update_data == 1: 
     network_name = input('Enter name of the Network Provider : ') 
     # Calling db_conn method in order to established connection to DB SERVER 
     try: 
      local_con = myclass().dbconn() 
      print(local_con, 'this is at registration method level') 

     except: 
      if local_con: 
       local_con.rollback() 

      print('Error Connection') 

     # Sending the data to the database for permanent storage 

     local_con.execute(
      "INSERT INTO network(network_name)VALUES(%s)", (network_name,)) 
     local_con.commit() 
     print('Data Save properly') 

Der obige Code in diese Ausgabe von meiner Konsole geführt,

You can update Network Provider, API Setting and Price here 
Enter 1 or 2 to update Network Provider 
Enter your 1 or 2 :1 
Enter name of the Network Provider : starcom Nigeria 
<cursor object at 0x0000000002FD7EA0; closed: 0> this is at dbconn method level 
<cursor object at 0x0000000002FD7EA0; closed: 0> this is at registration method level 
Traceback (most recent call last): 
File "/myswitch.py", line 258, in <module> 
new_provider.registration() 
File "/myswitch.py", line 53, in registration 
local_con.commit() 
AttributeError: 'psycopg2.extensions.cursor' object has no attribute 'commit' 

aus der obigen Ausgabe, ich verstehe, dass ich (self.con) zugreifen kann von dbconn() -Methode in einem anderen method registration() der gleichen Klasse, jedoch das commit() - Attribut, das ich auf der lokalen Variablen unter registration() erwartet mit folgendem Code fehlgeschlagen ist: AttributError: 'psycopg2.extensions.cursor' Objekt hat kein Attribut 'commit'

Daher konnte die Transaktion nicht in der Datenbank gespeichert werden. Ich hoffe, ich kann auf die Lösung dieses Problems führen. Vielen Dank im Voraus

+0

** def __inti __ **? –

+0

Bitte lesen Sie über Klassen und OOP. –

Antwort

0

Sie haben Ihre "self.con = connect_string.cursor()" und daher local_con einem Cursor zugewiesen? Das Commit muss von der Klasse psycopg2.connect stammen.

So muss es connect_string.commit() in Ihrem Fall sein.

+1

Danke, es funktioniert – Bluelily

Verwandte Themen