mysql decimal數據類型轉換的實現
發(fā)布日期:2022-02-19 18:53 | 文章來源:腳本之家
最近在工作遇到數據庫中存的數據類型是: decimal(14,4)
遇到的問題是:
當我使用python 讀取到內存中時,總是帶著 decimal字符, 再寫入其它mysql表中時,數據類型為int型,導致數據入庫不成功.
import pymysql
# 創(chuàng)建數據庫連接
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數據類型轉換內容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網友推薦、互聯網收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯系alex-e#qq.com處理。
相關文章
關注官方微信