I use Python3, and try use MySQL Connector/Python from mysql.com
I have tables in UTF-8 coding, and when I fetch the rows, all my chars columns returned like bytearray. This is make some confusion.
How I can fetch directly str?
UPD:
# -*- coding: utf-8 -*-
import mysql.connector
con = mysql.connector.connect( user ="root", db = "vg_site_db", charset = 'utf8' )
cursor = con.cursor()
sql = """select caption from domains
"""
cursor.execute( sql )
row = cursor.fetchone()
while row is not None:
print( row )
row = cursor.fetchone()
output:
(bytearray(b'ezsp.ru'),)
(bytearray(b'eazyshop.ru'),)
(bytearray(b'127.0.0.1:8080'),)
(bytearray(b'rmsvet.ru'),)
I want:
(('ezsp.ru'),)
(('eazyshop.ru'),)
(('127.0.0.1:8080'),)
(('rmsvet.ru'),)
UPD2:
My tables use COLLATE utf8_bin
An easy way to solve this issue is to make sure that you are retrieving 'strings' from your MySQL table. To do so, you just have to add a CAST in your query as follows:
This should work for you.
Seems like this happens when you use binary collation, at least the same happened to me. To convert the bytearrays to Unicode strings, you can add a custom converter class:
Based on your SQL query:
You can read
caption
column directly from the fetchedrow
Example 1:
Example 2:
Documentation Reference:
I don't think that you can get the cursor to return strings. The MySQL Connector Documentation says that they chose to return bytearrays so that they only have to maintain one codebase for both Python2 and Python3:
I addressed this issue using a list comprehension to decode each bytearray in the row: