mysql decimal数据类型转换

版权声明:诸葛老刘所有 https://blog.csdn.net/weixin_39791387/article/details/84580136

最近在工作遇到数据库中存的数据类型是: decimal(14,4)
遇到的问题是:

  1. 当我使用python 读取到内存中时,总是带着 decimal字符, 再写入其它mysql表中时,数据类型为int型,导致数据入库不成功.
import pymysql
# 创建数据库连接
con = pymysql.connect()

sql = '''select
created_time
from schma.table
LIMIT 10'''

try:
    cur = con.cursor(cursor=pymysql.cursors.DictCursor)
    cur.execute(sql)
except Exception as e:
	print(e)
else:
	data = cur.fetchall()
finally:
	cur.close()
	con.close()

for d in data:
	created_time = d.get('created_time')
	print(created_time)

  1. 解决方案:
    使用mysqlcast方法来转换
select
cast(created_time as signed) AS created_time 
from schma.table

猜你喜欢

转载自blog.csdn.net/weixin_39791387/article/details/84580136