首页 > 数据库 > MySQL

mysql decimal数据类型转换的实现

最近在工作遇到数据库中存的数据类型是: 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

到此这篇关于mysql decimal数据类型转换的实现的文章就介绍到这了,更多相关mysql decimal数据类型转换内容请搜索潘少俊衡以前的文章或继续浏览下面的相关文章希望大家以后多多支持潘少俊衡!

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
本文地址:/shujuku/MySQL/101143.html

留言与评论(共有 0 条评论)
   
验证码:

潘少俊衡

| 桂ICP备2023010378号-4

Powered By EmpireCMS

谷姐神农

使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

感谢潘少俊衡友情技术支持