forked from grifon/python-module-mysql
Compare commits
7 commits
Author | SHA1 | Date | |
---|---|---|---|
7446537cc9 | |||
3787329fd6 | |||
3b38eae5e3 | |||
5e68a9f7be | |||
64a935b301 | |||
cd8db520bc | |||
47fd8f3f30 |
2 changed files with 8 additions and 17 deletions
|
@ -17,7 +17,7 @@ import pymysql
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# Read conf.ini
|
# Read conf.ini
|
||||||
INI_CONF = "./config/sql_conf.ini"
|
INI_CONF = "./config/mysql_conf.ini"
|
||||||
|
|
||||||
if os.path.exists(INI_CONF) is False:
|
if os.path.exists(INI_CONF) is False:
|
||||||
print("MySQL configuration error, {}: file not found".format(INI_CONF))
|
print("MySQL configuration error, {}: file not found".format(INI_CONF))
|
||||||
|
@ -27,25 +27,15 @@ CONFIG = ConfigParser()
|
||||||
CONFIG.read(INI_CONF)
|
CONFIG.read(INI_CONF)
|
||||||
|
|
||||||
# DB parameters
|
# DB parameters
|
||||||
SQL_HOST = CONFIG.get('sql', 'host')
|
SQL_HOST = CONFIG.get('mysql', 'host')
|
||||||
SQL_DB = CONFIG.get('sql', 'database')
|
SQL_DB = CONFIG.get('mysql', 'database')
|
||||||
SQL_USER = CONFIG.get('sql', 'user')
|
SQL_USER = CONFIG.get('mysql', 'user')
|
||||||
SQL_PWD = CONFIG.get('sql', 'passwd')
|
SQL_PWD = CONFIG.get('mysql', 'passwd')
|
||||||
|
|
||||||
|
|
||||||
class HandleSql:
|
class HandleSql:
|
||||||
"""Class to handle sql connection."""
|
"""Class to handle sql connection."""
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def sql_checkcon():
|
|
||||||
"""Init sql connection."""
|
|
||||||
try:
|
|
||||||
global conn
|
|
||||||
conn = pymysql.connect(host=SQL_HOST, db=SQL_DB, user=SQL_USER, passwd=SQL_PWD)
|
|
||||||
cur = conn.cursor()
|
|
||||||
except pymysql.DatabaseError as error:
|
|
||||||
print(error)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def sql_conn():
|
def sql_conn():
|
||||||
"""Init sql connection."""
|
"""Init sql connection."""
|
||||||
|
@ -53,6 +43,7 @@ class HandleSql:
|
||||||
global conn
|
global conn
|
||||||
conn = pymysql.connect(host=SQL_HOST, db=SQL_DB, user=SQL_USER, passwd=SQL_PWD)
|
conn = pymysql.connect(host=SQL_HOST, db=SQL_DB, user=SQL_USER, passwd=SQL_PWD)
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
return True
|
||||||
except pymysql.DatabaseError as error:
|
except pymysql.DatabaseError as error:
|
||||||
print(error)
|
print(error)
|
||||||
|
|
4
test.py
4
test.py
|
@ -3,10 +3,10 @@
|
||||||
|
|
||||||
from manage_sql.manage_sql import HandleSql
|
from manage_sql.manage_sql import HandleSql
|
||||||
|
|
||||||
MY_SQL_CONN = HandleSql()
|
MYSQL_CONN = HandleSql()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
check_return_bdd = MY_SQL_CONN.sql_checkcon()
|
check_return_bdd = MYSQL_CONN.conn()
|
||||||
except OperationalError as error:
|
except OperationalError as error:
|
||||||
print(error)
|
print(error)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue