Implementation of mysql decimal data type conversion

Implementation of mysql decimal data type conversion

Recently, I encountered a database with the following data type: decimal(14,4)

The problems encountered are:

When I use Python to read data into memory, it always has decimal characters. When I write it into other MySQL tables, the data type is int, which causes data to fail to be stored.

import pymysql
# Create a database connection 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)

Solution:

Use mysql's cast method to convert

select
cast(created_time as signed) AS created_time 
from schma.table

This is the end of this article about the implementation of MySQL decimal data type conversion. For more information about MySQL decimal data type conversion, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief introduction to the usage of decimal type in MySQL
  • The difference between Decimal type and Float Double in MySQL (detailed explanation)
  • Detailed explanation of the meaning of N and M in the MySQL data type DECIMAL(N,M)
  • Method to convert scientific notation numeric string to decimal type
  • Database data type float to C# type decimal, float data type conversion is invalid
  • Detailed explanation of the usage of DECIMAL in MySQL data type
  • Detailed explanation of the decimal padding problem of decimal data type in MySQL
  • Detailed explanation of the usage of MySQL data type DECIMAL
  • In-depth explanation of the use and implementation of the Decimal type in the database

<<:  Detailed explanation of the flexible use of CSS grid system in projects

>>:  Implementation of Docker deployment of Nuxt.js project

Recommend

How to use echarts to visualize components in Vue

echarts component official website address: https...

Specific use of GNU Parallel

what is it? GNU Parallel is a shell tool for exec...

Let's talk about the storage engine in MySQL

Basics In a relational database, each data table ...

avue-crud implementation example of multi-level complex dynamic header

Table of contents Preface Background data splicin...

Element Table table component multi-field (multi-column) sorting method

Table of contents need: Problems encountered: sol...

Implementation of Bootstrap web page layout grid

Table of contents 1. How the Bootstrap grid syste...

An article teaches you how to use Vue's watch listener

Table of contents Listener watch Format Set up th...

Summary of the use of Vue computed properties and listeners

1. Computed properties and listeners 1.1 Computed...

How to remove the header from the element table

Document hints using the show-header attribute sh...

Use of CSS3's focus-within selector

Pseudo-elements and pseudo-classes Speaking of th...

How to modify the root password of mysql under Linux

Preface The service has been deployed on MySQL fo...