How to return str from MySQL using mysql.connector?

后端 未结 5 1051
慢半拍i
慢半拍i 2020-12-16 16:06

I\'m trying to use MySQL Connector/Python from mysql.com with Python 3.

I have tables in UTF-8 coding, and when I fetch the rows, all my chars columns returned like

相关标签:
5条回答
  • 2020-12-16 16:40

    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:

    With the use of “raw” cursors, the returned values is of the bytearray type. This is necessary for having both Python 2 and 3 return the same data.

    I addressed this issue using a list comprehension to decode each bytearray in the row:

    for row in cursor:
        type_fixed_row = tuple([el.decode('utf-8') if type(el) is bytearray else el for el in row])
        print( type_fixed_row )
    
    0 讨论(0)
  • 2020-12-16 16:41

    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:

    class MyConverter(mysql.connector.conversion.MySQLConverter):
    
        def row_to_python(self, row, fields):
            row = super(MyConverter, self).row_to_python(row, fields)
    
            def to_unicode(col):
                if isinstance(col, bytearray):
                    return col.decode('utf-8')
                return col
    
            return[to_unicode(col) for col in row]
    
    sql = mysql.connector.connect(converter_class=MyConverter, host=...)
    
    0 讨论(0)
  • 2020-12-16 16:48

    MySQL Connector returns strings (as stored using the CHAR, VARCHAR, and TEXT data types) as bytearrays when respective columns are defined with a binary collation (e.g. utf8_bin). You must call .decode() on values to get Python strings, e.g.:

    for row in cursor:
        caption = row[0].decode()
    

    That said, unless you have a specific requirement to use utf8_bin, it's a much better idea to use the utf8mb4 character set with the collation utf8mb4_unicode_ci on the database level. That would solve your problem and allow for full Unicode support. See this and this fore more details.

    0 讨论(0)
  • 2020-12-16 17:03

    Adding mysql-connector-python==8.0.17 to requirements.txt resolved this issue for me.

    0 讨论(0)
  • 2020-12-16 17:03

    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:

     # -*- coding: utf-8 -*-
    import mysql.connector
    con = mysql.connector.connect( user ="root", db = "vg_site_db", charset = 'utf8' )
    cursor = con.cursor()
    sql = "select CAST(caption as CHAR(50)) from domains"
    cursor.execute( sql )
    row = cursor.fetchone()
    while row is not None:
        print( row )
        row = cursor.fetchone()
    

    This should work for you.

    0 讨论(0)
提交回复
热议问题