mysql decimal數(shù)據(jù)類型轉(zhuǎn)換的實現(xiàn)
更新時間:2021年02月06日 16:35:27 作者:諸葛老劉
這篇文章主要介紹了mysql decimal數(shù)據(jù)類型轉(zhuǎn)換的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
最近在工作遇到數(shù)據(jù)庫中存的數(shù)據(jù)類型是: decimal(14,4)
遇到的問題是:
當我使用python 讀取到內(nèi)存中時,總是帶著 decimal字符, 再寫入其它mysql表中時,數(shù)據(jù)類型為int型,導致數(shù)據(jù)入庫不成功.
import pymysql
# 創(chuàng)建數(shù)據(jù)庫連接
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方法來轉(zhuǎn)換
select cast(created_time as signed) AS created_time from schma.table
到此這篇關(guān)于mysql decimal數(shù)據(jù)類型轉(zhuǎn)換的實現(xiàn)的文章就介紹到這了,更多相關(guān)mysql decimal數(shù)據(jù)類型轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

