Receive textual data from mysql with python

Premise

How to receive MySQL data in text due to compatibility issues with older systems or other systems (This is my memo)

Method

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)]

reference

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

Receive textual data from mysql with python
Notes on importing data from MySQL or CSV with Python
[Note] Get data from PostgreSQL with Python
Let's do MySQL data manipulation with Python
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Use MySQL from Python
Use MySQL from Python
Data analysis with Python
Extract data from a web page with Python
[Data science basics] I tried saving from csv to mysql with python
Sample data created with python
How to scrape image data from flickr with python
Receive dictionary data from a Python program in AppleScript
Get Youtube data with python
Use MySQL from Anaconda (python)
[Basics of data science] Collecting data from RSS with python
With skype, notify with skype from python!
Get data from database via ODBC with Python (Access)
Connection pooling with Python + MySQL
Read json data with python
A memo that reads data from dashDB with Python & Spark
Xpath summary when extracting data from websites with Python Scrapy
Get data from analytics API with Google API Client for python
Collecting information from Twitter with Python (MySQL and Python work together)
Python: Exclude tags from html data
Call C from Python with DragonFFI
Hit treasure data from Python Pandas
Using Rstan from Python with PypeR
Get data from Quandl in Python
Install Python from source with Ansible
Create folders from '01' to '12' with python
Build Mysql + Python environment with docker
[Python] Get economic data with DataReader
Python data structures learned with chemoinformatics
Run Aprili from Python with Orange
Call python from nim with Nimpy
Easy data visualization with Python seaborn.
Process Pubmed .xml data with python
Data analysis starting with python (data visualization 1)
Read fbx from python with cinema4d
Data analysis starting with python (data visualization 2)
Python application: Data cleansing # 2: Data cleansing with DataFrame
Introduction to Data Analysis with Python P17-P26 [ch02 1.usa.gov data from bit.ly]
Manipulating kintone data with Python & C Data ODBC Driver from AWS Lambda
Get additional data in LDAP with python
Data pipeline construction with Python and Luigi
Get html from element with Python selenium
INSERT into MySQL with Python [For beginners]
Play audio files from Python with interrupts
Create wordcloud from your tweet with python3
Process Pubmed .xml data with python [Part 2]
Use PostgreSQL data type (jsonb) from Python
Add a Python data source with Redash
Retrieving food data with Amazon API (Python)
Python: Reading JSON data from web API
Try working with binary data in Python
Generate Japanese test data with Python faker
POST variously with Python and receive with Flask
Tweet from python with Twitter Developer + Tweepy
Get data from Cloudant with Bluemix flask
Convert Excel data to JSON with python