标签:password str fetch foreign col insert _for 打印 charset
select CAST(u.amount AS CHAR) from user u
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for amount
-- ----------------------------
DROP TABLE IF EXISTS `amount`;
CREATE TABLE `amount` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`amount` decimal(65, 15) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of amount
-- ----------------------------
INSERT INTO `amount` VALUES (1, ‘desire‘, 14.025415550000000);
INSERT INTO `amount` VALUES (2, ‘ronin‘, 0.253698741550000);
INSERT INTO `amount` VALUES (3, ‘tom‘, 0.000000000000005);
INSERT INTO `amount` VALUES (4, ‘aaa‘, 0.000000000000785);
SET FOREIGN_KEY_CHECKS = 1;
import pymysql
con = pymysql.connect(host=‘localhost‘,
user="root",
password="123456",
port=3306,
charset=‘utf8‘)
cur = con.cursor()
sql = "SELECT * FROM desire.amount a"
cur.execute(sql)
users = cur.fetchall()
print(users)
2.1、打印结果
((1, ‘desire‘, Decimal(‘14.025415550000000‘)), (2, ‘ronin‘, Decimal(‘0.253698741550000‘)), (3, ‘tom‘, Decimal(‘5E-15‘)), (4, ‘aaa‘, Decimal(‘7.85E-13‘)))
2.2 分析结果:从上面的打印结果可以看到,如果小数位过长,打印出来的数据就会变成科学计数法的形式打印,这样的数据看起来不是很清晰。
sql = "SELECT a.id,a.name,CAST(a.amount AS CHAR) FROM desire.amount a"
cur.execute(sql)
users = cur.fetchall()
print(users)
3.1 打印结果
((1, ‘desire‘, ‘14.025415550000000‘), (2, ‘ronin‘, ‘0.253698741550000‘), (3, ‘tom‘, ‘0.000000000000005‘), (4, ‘aaa‘, ‘0.000000000000785‘))
3.2 分析结果:这样打印出来的数据,就跟数据库存放的一致了,数据比对起来也是正确的,很清晰
解决python查询数据库字段为decimal类型的数据结果为科学计数法的问题
标签:password str fetch foreign col insert _for 打印 charset
原文地址:https://www.cnblogs.com/desireyang/p/13194648.html