mysql decimal數據類型轉換的實現
最近在工作遇到數據庫中存的數據類型是: decimal(14,4)
遇到的問題是:當我使用python 讀取到內存中時,總是帶著 decimal字符, 再寫入其它mysql表中時,數據類型為int型,導致數據入庫不成功.
import pymysql# 創建數據庫連接con = pymysql.connect()sql = ’’’selectcreated_timefrom schma.tableLIMIT 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方法來轉換
selectcast(created_time as signed) AS created_time from schma.table
到此這篇關于mysql decimal數據類型轉換的實現的文章就介紹到這了,更多相關mysql decimal數據類型轉換內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
