服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - mysql decimal数据类型转换的实现

mysql decimal数据类型转换的实现

2021-04-12 16:41诸葛老刘 Mysql

这篇文章主要介绍了mysql decimal数据类型转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

遇到的问题是:

当我使用python 读取到内存中时,总是带着 decimal字符, 再写入其它mysql表中时,数据类型为int型,导致数据入库不成功.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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方法来转换

?
1
2
3
select
cast(created_time as signed) AS created_time
from schma.table

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

原文链接:https://blog.csdn.net/weixin_39791387/article/details/84580136

延伸 · 阅读

精彩推荐