This is my ConnectionFactory class used by DAO
import MySQLdb
import MySQLdb.cursors
import pymysql
class ConnectionFactory:
host='127.0.0.1'
user='nicolacivile'
password='12345'
db='ildb'
connectionFactory = None
def __new__(cls, *args, **kwargs):
if cls.connectionFactory is None:
cls.connectionFactory = super().__new__(ConnectionFactory)
return cls.connectionFactory
return cls.connectionFactory
def __init__(self):
self.conn = self.connect()
def connect(self):
try:
return pymysql.connect(host=self.host,
user=self.user,
password=self.password,
db=self.db,
cursorclass=MySQLdb.cursors.DictCursor)
except pymysql.Error as e:
pass
def getConn(self):
return self.conn
This is class Dao that execute query for return one annotation passing idAnnotationWebServer
my problem is that cursor.execute(self.SELECT_ANNWEBSERVER_PK_QUERY, (idAnnotationWebServer) ) isn't execute. Program it stop before.
from etl_model_connectionfactory import ConnectionFactory
import MySQLdb
class AnnotationWebServerDAO:
conn = None
errorMessage = ""
SELECT_ANNWEBSERVER_PK_QUERY = '''
SELECT
annotation_web_server_id,
web_server_id,
annotation_code,
annotation_name,
date_created,
date_modified,
date_deleted,
deleted
FROM annotation_web_server
WHERE annotation_web_server_id = %s
AND (deleted = 0 OR deleted IS NULL) '''
def clearErrorMessage(self):
self.errorMessage = ""
def setErrorMessage(self, msg):
self.errorMessage = msg
def getErrorMessage(self):
return self.errorMessage
def getConnection(self):
connes=None
connes=ConnectionFactory()
return connes.getConn()
def setConnection(self,connes):
self.conn = connes
def getAnnWebServer(self,idAnnotationWebServer):
print('in getAnnWebServer')
if self.conn is None:
print("is none self.conn")
self.conn = self.getConnection()
self.clearErrorMessage()
annotation = None
try:
print("before cursor")
cursor = self.conn.cursor()
print("after cursor",idAnnotationWebServer)
cursor.execute(self.SELECT_ANNWEBSERVER_PK_QUERY, (idAnnotationWebServer) )
print("after execute")
annotations = cursor.fetchall()
print("fetch annotations",annotations)
if(annotations is not None or len(annotations) > 0 ):
annotation = annotations[0]
else:
annotation = None
msg = "L'annotazione specifica non è presente."
self.setErrorMessage(msg)
except (MySQLdb.Error, MySQLdb.Warning) as e:
annotation = None
msg = "Error: Could not make connection to the MySQL database"
self.setErrorMessage(msg)
print("Error: Could not make connection to the MySQL database")
print(e)
cursor.close()
print("exit from getAnnWebServer")
return annotation
I call is this way
from etl_model_annotationwebserver_dao import AnnotationWebServerDAO
idAnnotationWebServer = 3
annotationWebServerDao = AnnotationWebServerDAO()
ann = annotationWebServerDao.getAnnWebServer(idAnnotationWebServer)
print("ann",ann)
It print this output:
idAnnotationWebServer 3
in getAnnWebServer
is none self.conn
before cursor
after cursor 3
Can you help me? I don't undestand