Skip to content

Commit 31ffc61

Browse files
authored
Merge pull request #52 from allenpc/master
Fix the Unicode issue for all connection code paths
2 parents 15511b9 + 2aa12c1 commit 31ffc61

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

pandasql/sqldf.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
from warnings import catch_warnings, filterwarnings
77
from sqlalchemy.exc import DatabaseError, ResourceClosedError
88
from sqlalchemy.pool import NullPool
9-
9+
from sqlalchemy.event import listen
1010

1111
__all__ = ['PandaSQL', 'PandaSQLException', 'sqldf']
1212

13-
1413
class PandaSQLException(Exception):
1514
pass
1615

@@ -24,6 +23,10 @@ def __init__(self, db_uri='sqlite:///:memory:', persist=False):
2423
:param persist: keep tables in database between different calls on the same object of this class.
2524
"""
2625
self.engine = create_engine(db_uri, poolclass=NullPool)
26+
27+
if self.engine.name == 'sqlite':
28+
listen(self.engine, 'connect', self._set_text_factory)
29+
2730
if self.engine.name not in ('sqlite', 'postgresql'):
2831
raise PandaSQLException('Currently only sqlite and postgresql are supported.')
2932

@@ -77,7 +80,6 @@ def conn(self):
7780
else:
7881
# create the connection
7982
conn = self.engine.connect()
80-
conn.text_factory = str
8183
self._init_connection(conn)
8284
try:
8385
yield conn
@@ -89,6 +91,9 @@ def _init_connection(self, conn):
8991
if self.engine.name == 'postgresql':
9092
conn.execute('set search_path to pg_temp')
9193

94+
def _set_text_factory(self, dbapi_con, connection_record):
95+
dbapi_con.text_factory = str
96+
9297

9398
def get_outer_frame_variables():
9499
""" Get a dict of local and global variables of the first outer frame from another file. """

0 commit comments

Comments
 (0)