环境:Python3.5.2,MySQL.Connector 2.0.4,在MariaDB服务器10.1.14上
为什么准备好的游标返回字节数组中的字符串值,而标准游标返回普通字符串?
对于同一个实例的同一个调用,我的准备了游标输出:
[(11, bytearray(b'1234567890'), None),
(17, bytearray(b'1234567799'), bytearray(b'abc@outlook.com'))]虽然标准版本提供了所需的输出:
[(11, '1234567890', None),
(17, '1234567799', 'abc@outlook.com')]标准版本代码:
def query_userdb(query, arg):
retVal = None
cnx = mariadb.connect(**DB_CONFIG_USERS)
cursor = cnx.cursor()
cursor.execute(query, arg)
if cursor.rowcount != 0:
retVal = cursor.fetchall()
cnx.commit()
cnx.close()
return retVal对于准备的版本,我只将游标声明更改为
cursor = cnx.cursor(prepared=True)如何使准备好的游标正确返回字符串?
发布于 2020-03-13 15:00:33
要获得字符串,可以使用:
my_bytearray.decode("utf-8")在您的例子中,使用列表理解:
retVal = [(id,col2.decode("utf-8"),col3.decode("utf-8") for id,col2,col3 in retVal]或者更好地处理第三列中的None值:
retVal = [(id,col2.decode("utf-8"),(col3 if col3 is None else col3.decode("utf-8")) for id,col2,col3 in retVal]https://stackoverflow.com/questions/38288862
复制相似问题