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

Detailed explanation of table return and index coverage examples in MySQL

Table of contents Index Type Index structure Nonc...

React-Native environment setup and basic introduction

Environment Preparation 1. Environment Constructi...

If I change a property randomly in Vue data, will the view be updated?

Interviewer: Have you read the source code of Vue...

MySQL Database Indexes and Transactions

Table of contents 1. Index 1.1 Concept 1.2 Functi...

Detailed explanation of the benefits of PNG in various network image formats

BMP is an image file format that is independent o...

js canvas realizes slider verification

This article example shares the specific code of ...

5 Reasons Why Responsive Web Design Isn’t Worth It

This article is from Tom Ewer's Managewp blog,...

Implementation of webpack-dev-server to build a local server

Table of contents Preface webpack-deb-server webp...

How to use Spark and Scala to analyze Apache access logs

Install First you need to install Java and Scala,...

A Brief Analysis of Patroni in Docker Containers

Table of contents Create an image File Structure ...

Analysis and solution of flex layout collapse caused by Chrome 73

Phenomenon There are several nested flex structur...

Detailed explanation of how to access MySQL database remotely through Workbench

Preface Workbench is installed on one computer, a...