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
可能,在这种情况下,简单的零填充可以吗?在
据我所知,这是数字的附加部分。如果它的长度不是常量,则需要将DB中的数据类型更改为VARCHAR。在
您的列是一个
int
,因此MySQLdb在查询结果中返回一个整数值。但是,我认为您应该能够编写一个mySQLdb结果集包装器(或者可能找到其他人已经编写过的包装器),它检查结果集的列上的标志集并将其转换为适当的字符串。在看看}以及PEP-249。我认为(实际上我没有试过)应该从以下几方面着手:
cursor.description
和{相关问题 更多 >
编程相关推荐