2014-03-12 24 views
1

我在MySQL的列,其意在以十進制值(例如0.00585431)蟒MySQL的 - 的unicode

在Python中,我有從網頁獲取此值的功能,然後返回它到變量中。當我打印這個變量,我得到[u'0.00585431'](這很奇怪)

然後我嘗試插入這個設置爲取小數點(10,0)值的mysql列。然而,數據庫存儲,把它當成一個0

代碼插入是沒有什麼特別適用於其他的東西:

cur.execute("""INSERT INTO earnings VALUES (%s)""", (variable)) 

如果我更改列字符串類型,然後它存儲了整個[ü '0.00585431']。所以我想,當我試圖將它作爲一個十進制存儲它實際上並沒有採取適當的十進制值,並存儲一個0呢?

有關如何解決此問題的任何想法?

回答

1

DECIMAL(10,0)將給出逗號右側的0。

The declaration syntax for a DECIMAL column remains DECIMAL(M,D), although the range of values for the arguments has changed somewhat:

M is the maximum number of digits (the precision). It has a range of 1 to 65. This introduces a possible incompatibility for older applications, because previous versions of MySQL permit a range of 1 to 254. (The precision of 65 digits actually applies as of MySQL 5.0.6. From 5.0.3 to 5.0.5, the precision is 64 digits.)

D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

試圖將列數據類型更改爲DECIMAL(10,8)

如果你的價值觀總是會在相同的格式,然後0.00585431DECIMAL(9,8)就足夠了。

https://dev.mysql.com/doc/refman/5.0/en/precision-math-decimal-changes.html

+0

所以我做了改變,但它仍然給我0,與此錯誤一起: 「警告:不正確的十進制值:‘[u'0.00585431’]」列」 – Simon

+0

我想象它有更多的問題與unicode方面的問題相比,mysql表的結構 – Simon

+0

你不能有[u'作爲十進制值。試試只有整數 –