Skip to content

Commit

Permalink
Set charset when connecting via pyodbc
Browse files Browse the repository at this point in the history
  • Loading branch information
amochin committed Jun 23, 2023
1 parent d980ad6 commit a943dcc
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/DatabaseLibrary/connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def connect_to_database(self, dbapiModuleName=None, dbName=None, dbUsername=None
if dbapiModuleName in ["MySQLdb", "pymysql"]:
dbPort = dbPort or 3306
logger.info('Connecting using : %s.connect(db=%s, user=%s, passwd=%s, host=%s, port=%s, charset=%s) ' % (dbapiModuleName, dbName, dbUsername, dbPassword, dbHost, dbPort, dbCharset))
self._dbconnection = db_api_2.connect(db=dbName, user=dbUsername, passwd=dbPassword, host=dbHost, port=dbPort, charset=dbCharset)
self._dbconnection = db_api_2.connect(db=dbName, user=dbUsername, passwd=dbPassword, host=dbHost, port=dbPort, charset='utf8mb4' or dbCharset)
elif dbapiModuleName in ["psycopg2"]:
dbPort = dbPort or 5432
logger.info('Connecting using : %s.connect(database=%s, user=%s, password=%s, host=%s, port=%s) ' % (dbapiModuleName, dbName, dbUsername, dbPassword, dbHost, dbPort))
Expand All @@ -106,7 +106,7 @@ def connect_to_database(self, dbapiModuleName=None, dbName=None, dbUsername=None
dbPort = dbPort or 1433
dbDriver = dbDriver or "{SQL Server}"
logger.info('Connecting using : %s.connect(DRIVER=%s;SERVER=%s:%s;DATABASE=%s;UID=%s;PWD=%s)' % (dbapiModuleName, dbDriver, dbHost, dbPort, dbName, dbUsername, dbPassword))
self._dbconnection = db_api_2.connect('DRIVER=%s;SERVER=%s:%s;DATABASE=%s;UID=%s;PWD=%s' % ( dbDriver, dbHost, dbPort, dbName, dbUsername, dbPassword))
self._dbconnection = db_api_2.connect('DRIVER=%s;SERVER=%s:%s;DATABASE=%s;UID=%s;PWD=%s;charset=%s' % (dbDriver, dbHost, dbPort, dbName, dbUsername, dbPassword, 'utf8mb4' or dbCharset))
elif dbapiModuleName in ["excel"]:
logger.info(
'Connecting using : %s.connect(DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%s;ReadOnly=1;Extended Properties="Excel 8.0;HDR=YES";)' % (
Expand Down
4 changes: 1 addition & 3 deletions test/resources/common.resource
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ ${DB_PORT} 5432
${DB_USER} db_user

# used for MySQL via PyODBC only
${DB_CHARSET} ${None}
${DB_DRIVER} ODBC Driver 18 for SQL Server


Expand All @@ -42,8 +41,7 @@ Connect To DB
... ${DB_PASS}
... ${DB_HOST}
... ${DB_PORT}
... ${DB_CHARSET}
... ${DB_DRIVER}
... dbDriver=${DB_DRIVER}
ELSE
Connect To Database
... ${DB_MODULE}
Expand Down
2 changes: 1 addition & 1 deletion test/tests/common_tests/encoding.robot
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Test Teardown Drop Tables Person And Foobar


*** Test Cases ***
Character In Values
Non ASCII Characters In Values
Execute Sql String INSERT INTO person VALUES(1,'Jürgen','Gernegroß')
${results}= Query
... SELECT LAST_NAME FROM person WHERE FIRST_NAME='Jürgen'
Expand Down

0 comments on commit a943dcc

Please sign in to comment.