Newbie question - regarding connection

96 views
Skip to first unread message

Nejc Nečemer

unread,
Mar 7, 2017, 4:06:43 PM3/7/17
to PyMySQL Users
Hello folks,

relatively simple for you. I have a class where i connect my connection to server through pymysql.connect(), where i define everything except name of the database. Hence, within this class I have Methond called ConnectToDatabase(), where i' would like to try connect to specific database (define at the end of program)...in case if this database does not exists on server i have prepared Method called CreateDatabase()

Thank you for your time.

Here is code:
import pymysql
from datetime import datetime


class DatabaseUtil:
def __init__(self, database, tableName):
self.db = database
self.tableName = tableName

self.cnx = pymysql.connect(
host='localhost',
user='root',
port=3306,
password='nece22',
charset='utf8mb4'
)

self.cur = self.cnx.cursor()
self.ConnectToDatabase()
#self.CreateTable()

def ConnectToDatabase(self):
try:
self.cnx.database = self.db
except pymysql.DataError as error:
print('Error Masage' + str(error))
print('With')
self.CreateDatabase()
self.cnx.database = self.db
except pymysql.OperationalError as err:
print('Napaka')
print(err)
self.CreateDatabase()
self.cnx.database = self.db
except pymysql.IntegrityError as err:
print('Napaka')
print(err)
self.CreateDatabase()
self.cnx.database = self.db
except pymysql.NotSupportedError as err:
print('Napaka')
print(err)
self.CreateDatabase()
self.cnx.database = self.db
except pymysql.ProgrammingError as err:
print('Napaka')
print(err)
self.CreateDatabase()
self.cnx.database = self.db
Reply all
Reply to author
Forward
0 new messages