How to receive MySQL data in text due to compatibility issues with older systems or other systems (This is my memo)
Specify raw = True when initializing the connection
import mysql.connector
db = mysql.connector.connect(
user="user",
password="pwd",
host="server",
database="database",
raw=True)
cursor = db.cursor()
cursor.execute("SELECT * FROM test LIMIT 10")
print cursor.fetchall()
result
[('1', 'name1', '20140419'), ('2', 'name2', '20140106'), ('3', 'name3', '20140214'), ('4', 'name4', '20140212'), ('5', 'name5', '20140110'), ('6', 'name6', '20130930'), ('7', 'name7', '20140421'), ('8', 'name8', '20120507'), ('9', 'name9', '20131127'), ('10', 'name10', '20130918')]
When row = True is not added
[(1, u'name1', 20140419), (2, u'name2', 20140106), (3, u'name3', 20140214), (4, u'name4', 20140212), (5, u'name5', 20140110), (6, u'name6', 20130930), (7, u'name7', 20140421), (8, u'name8', 20120507), (9, u'name9', 20131127), (10, u'rname10', 20130918)]
https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
Argument Name | Default | Description |
---|---|---|
raw | False | Whether MySQL results are returned as is, rather than converted to Python types. |
Recommended Posts