饿虎岗资源网 Design By www.oxmxm.com

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

遇到的问题是:

当我使用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)

解决方案:

使用mysql的cast方法来转换

select
cast(created_time as signed) AS created_time 
from schma.table
饿虎岗资源网 Design By www.oxmxm.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
饿虎岗资源网 Design By www.oxmxm.com