您的列是一个int,因此MySQLdb在查询结果中返回一个整数值。但是,我认为您应该能够编写一个mySQLdb结果集包装器(或者可能找到其他人已经编写过的包装器),它检查结果集的列上的标志集并将其转换为适当的字符串。在

看看cursor.description和{}以及PEP-249。我认为(实际上我没有试过)应该从以下几方面着手:def get_result_set_with_db_specified_formatting(cursor):

integer_field_types = (MySQLdb.constants.FIELD_TYPE.TINY,

MySQLdb.constants.FIELD_TYPE.SHORT,

MySQLdb.constants.FIELD_TYPE.LONG,

MySQLdb.constants.FIELD_TYPE.LONGLONG,

MySQLdb.constants.FIELD_TYPE.INT24)

rows = cursor.fetchall()

for row in rows:

for index, value in enumerate(row):

value = str(value)

if (cursor.description[index][1] in integer_field_types

and cursor.description_flags[index] & MySQLdb.constants.FLAG.ZEROFILL):

if len(value) < cursor.description[index][2]:

value = ('0' * (cursor.description[index][2] - len(value))) + value

row[index] = value

return rows

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐