Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit 124ffef

Browse files
sdvcrxbinux
authored andcommitted
Fix mysql return bytes as field name type (#787)
* use pip version of mysql-connector-python for testing * fix mysql return bytes as field names type * fix raise Unread result found error This error raise on mysql-connector-python with C extension * fix test Pure version raise InterfaceError, but C extension version raise DatabaseError
1 parent c350f62 commit 124ffef

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ before_script:
3636
- psql -c "CREATE DATABASE pyspider_test_resultdb ENCODING 'UTF8' TEMPLATE=template0;" -U postgres
3737
- sleep 10
3838
install:
39-
- pip install http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.4.zip#md5=3df394d89300db95163f17c843ef49df
39+
- pip install mysql-connector-python
4040
- pip install https://github.com/marcus67/easywebdav/archive/master.zip
4141
- pip install --no-use-wheel lxml
4242
- pip install --allow-all-external -e .[all,test]

pyspider/database/basedb.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
logger = logging.getLogger('database.basedb')
1212

1313
from six import itervalues
14+
from pyspider.libs import utils
1415

1516

1617
class BaseDB:
@@ -72,7 +73,10 @@ def _select2dic(self, tablename=None, what="*", where="", where_values=[],
7273
logger.debug("<sql: %s>", sql_query)
7374

7475
dbcur = self._execute(sql_query, where_values)
75-
fields = [f[0] for f in dbcur.description]
76+
77+
# f[0] may return bytes type
78+
# https://github.com/mysql/mysql-connector-python/pull/37
79+
fields = [utils.text(f[0]) for f in dbcur.description]
7680

7781
for row in dbcur:
7882
yield dict(zip(fields, row))

pyspider/database/mysql/mysqlbase.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def dbcur(self):
1717
try:
1818
if self.conn.unread_result:
1919
self.conn.get_rows()
20+
if hasattr(self.conn, 'free_result'):
21+
self.conn.free_result()
2022
return self.conn.cursor()
2123
except (mysql.connector.OperationalError, mysql.connector.InterfaceError):
2224
self.conn.ping(reconnect=True)

tests/test_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_20_cli_config(self):
7272
self.assertEqual(ctx.obj.debug, True)
7373

7474
import mysql.connector
75-
with self.assertRaises(mysql.connector.InterfaceError):
75+
with self.assertRaises(mysql.connector.Error):
7676
ctx.obj.taskdb
7777

7878
with self.assertRaises(Exception):

0 commit comments

Comments
 (0)