使用 SQLite 数据库的 Ruby 脚本 十进制设置为 (4,2) 但包含值 (10,8)

ruby script using sqlite database decimal set to (4,2) but contains value (10,8)

提问人:banditKing 提问时间:7/15/2013 最后编辑:mu is too shortbanditKing 更新时间:7/15/2013 访问量:158

问:

我有一个sql查询,它创建了一个表,如下所示:

 db.execute ("CREATE TABLE IF NOT EXISTS RATING (programCode INTEGER , fromDate INTEGER, 
 toDate INTEGER, programName TEXT, channelName TEXT, weekday TEXT, startTime TEXT, 
 endTime TEXT, duration TEXT, episodeName TEXT, Ind2 DECIMAL(4,2), A18 DECIMAL(4,2), M18 
 DECIMAL(4,2), F18 DECIMAL(4,2), A18_24 DECIMAL(4,2), M18_24 DECIMAL(4,2), A18_34 
 DECIMAL(4,2), M18_34 DECIMAL(4,2), F18_34 DECIMAL(4,2), A18_49 DECIMAL(4,2), M18_49 
 DECIMAL(4,2), F18_49 DECIMAL(4,2), A25_34 DECIMAL(4,2) , M25_34 DECIMAL(4,2), A25_49 
 DECIMAL(4,2), M25_49 DECIMAL(4,2), F25_49 DECIMAL(4,2), A25_54 DECIMAL(4,2), M25_54 
 DECIMAL(4,2), F25_54 DECIMAL(4,2), A35_plus DECIMAL(4,2), M35_plus DECIMAL(4,2), 
 F35_plus DECIMAL(4,2), M35_49 DECIMAL(4,2), F35_49 DECIMAL(4,2), A35_54 DECIMAL(4,2), 
 M35_54 DECIMAL(4,2), F35_54 DECIMAL(4,2), A35_64 DECIMAL(4,2), M35_64 DECIMAL(4,2), 
 F35_64 DECIMAL(4,2), A50_plus DECIMAL(4,2), M50_plus DECIMAL(4,2), F50_plus 
 DECIMAL(4,2), C2_11 DECIMAL(4,2), T12_17 DECIMAL(4,2), V12_24 DECIMAL(4,2), PRIMARY 
 KEY(programCode,fromDate , toDate))")

但是,当我检查表格数据时,除了 2 个单元格具有 (10,8) 外,所有行和列都有十进制值 (4,2)。

我不明白是什么原因导致了这种情况以及如何解决这个问题!

见附图

enter image description here


我将查询更改为以下内容:

 db.execute ("INSERT INTO RATING (programCode, fromDate, toDate, programName, channelName, weekday, startTime, endTime, duration, episodeName, 
   Ind2 , A18 , M18, F18, A18_24, M18_24, A18_34, M18_34 , F18_34, A18_49 , M18_49, F18_49, A25_34 , M25_34 , A25_49 , M25_49, F25_49 , A25_54 , 
   M25_54, F25_54, A35_plus , M35_plus , F35_plus , M35_49, F35_49 , A35_54 , M35_54 , F35_54 , A35_64 , M35_64 , F35_64 , A50_plus , M50_plus , 
   F50_plus , C2_11 , T12_17 , V12_24 ) VALUES ('#{progCode}', '#{fromD}', '#{toD}', '#{arr[0]}', '#{arr[1]}','#{arr[2]}','#{arr[3]}','#{arr[4]}','#{arr[5]}','#{arr[6]}',
   '#{arr[45].to_f.round(2)}','#{arr[46].to_f.round(2)}','#{arr[47].to_f.round(2)}','#{arr[48].to_f.round(2)}','#{arr[49].to_f.round(2)}','#{arr[50].to_f.round(2)}','#{arr[51].to_f.round(2)}','#{arr[52].to_f.round(2)}','#{arr[53].to_f.round(2)}','#{arr[54].to_f.round(2)}','#{arr[55].to_f.round(2)}',
   '#{arr[56].to_f.round(2)}','#{arr[57].to_f.round(2)}','#{arr[58].to_f.round(2)}','#{arr[59].to_f.round(2)}','#{arr[60].to_f.round(2)}','#{arr[61].to_f.round(2)}','#{arr[62].to_f.round(2)}','#{arr[63].to_f.round(2)}','#{arr[64].to_f.round(2)}','#{arr[65].to_f.round(2)}','#{arr[66].to_f.round(2)}',
  '#{arr[67].to_f.round(2)}','#{arr[68].to_f.round(2)}','#{arr[69].to_f.round(2)}','#{arr[70].to_f.round(2)}','#{arr[71].to_f.round(2)}','#{arr[72].to_f.round(2)}','#{arr[73].to_f.round(2)}','#{arr[74].to_f.round(2)}','#{arr[75].to_f.round(2)}','#{arr[76].to_f.round(2)}','#{arr[77].to_f.round(2)}',
  '#{arr[78].to_f.round(2)}','#{arr[79].to_f.round(2)}','#{arr[80].to_f.round(2)}','#{arr[81].to_f.round(2)}');")

但问题仍然存在

Ruby-on-Rails Ruby SQLite 点浮 精度

评论


答:

2赞 noz 7/15/2013 #1

SQLite不支持所有数据库类型 - decimal(...)被转换为实际类型,然后遇到舍入问题。您需要使用SQLite的语言来应对此问题。

SQLite 数据类型