在Python中将十进制值传递给MySQL

时间:2021-12-31 00:53:57

So currently my table is as follow:

所以目前我的表如下:

    name      ->      type 
--------------------------------
   gameid     ->   mediumint(7)
  timestamp   ->    timestamp
  duration    ->   decimal (5,5)
   winner     ->    varchar(20)
winner_score  ->     int(1)
    loser     ->    varvhar(20)
 loser_score  ->     int(1)

Now, I'm able to connect to the database, but I'm getting a warning when inserting a new row to the database:

现在,我可以连接到数据库,但是在向数据库插入新行时我收到警告:

Warning: Out of range value for column 'duration' at row 1

For the duration I am calculating the start and end time and subtracting it. I played around with it by rounding it (thought it was the decimal issues) and passing it as a string, but it looks like this:

在此期间,我正在计算开始和结束时间并减去它。我通过舍入它(认为这是十进制问题)并将其作为字符串传递来玩它,但它看起来像这样:

gametime = round((time.time() - start_time),5)

Tried playing around with what I have written for it, which looks like:

试着玩我写的东西,看起来像:

cur.execute("INSERT INTO scores (duration, winner, winner_score, loser, loser_score) \
         VALUES(%s, %s, %s, %s, %s)", (gametime, red_p, red_s, blue_p, blue_s))

For some reason, I keep getting the error. Not sure if the %s is what is affecting it. On the MySQL backend, everything is good, except that the duration (regardless of the length) will always be 0.99999. Any help?

出于某种原因,我不断收到错误。不确定%s是否会影响它。在MySQL后端,一切都很好,除了持续时间(无论长度)总是0.99999。有帮助吗?

1 个解决方案

#1


1  

decimal(5,5) means that it can store only with decimals. Any number with integer will always be truncated to 0.99999.

十进制(5,5)表示它只能存储小数。任何带整数的数字都将被截断为0.99999。

Demo:

mysql> select cast(2 as decimal(5,5));
+-------------------------+
| cast(2 as decimal(5,5)) |
+-------------------------+
|                 0.99999 |
+-------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+------------------------------------------------------------------+
| Level   | Code | Message                                                          |
+---------+------+------------------------------------------------------------------+
| Warning | 1264 | Out of range value for column 'cast(2 as decimal(5,5))' at row 1 |
+---------+------+------------------------------------------------------------------+
1 row in set (0.00 sec)

To make it work, you may change the column type to other precision, like decimal(10,5).

要使其工作,您可以将列类型更改为其他精度,如decimal(10,5)。

#1


1  

decimal(5,5) means that it can store only with decimals. Any number with integer will always be truncated to 0.99999.

十进制(5,5)表示它只能存储小数。任何带整数的数字都将被截断为0.99999。

Demo:

mysql> select cast(2 as decimal(5,5));
+-------------------------+
| cast(2 as decimal(5,5)) |
+-------------------------+
|                 0.99999 |
+-------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+------------------------------------------------------------------+
| Level   | Code | Message                                                          |
+---------+------+------------------------------------------------------------------+
| Warning | 1264 | Out of range value for column 'cast(2 as decimal(5,5))' at row 1 |
+---------+------+------------------------------------------------------------------+
1 row in set (0.00 sec)

To make it work, you may change the column type to other precision, like decimal(10,5).

要使其工作,您可以将列类型更改为其他精度,如decimal(10,5)。