2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SQLiteで計算結果がエラーにならずにおかしくなる場合の対処法

Posted at

発生事象

以下のSQL式で、テーブルの列を直接使って計算した結果が、実際の値を直接記述して計算した場合と異なっていた。

abs((machines.normal_game_count * 250.0 / (machines.difference_ball - machines.total_balls_won)))

↓実際の値を直接記述した場合

abs((621 * 250.0 / (1990 - 6570)))

原因

INTEGER型として定義していた列に、カンマ(,)を含む文字列(例: 1,990)がTEXT型として保存されていた。

SQLiteは計算時にTEXT型をINTEGER型へ暗黙的に変換しようとする。その際、数値として解釈できない文字(今回の場合はカンマ)以降は無視される。そのため、1,990という文字列は1として扱われ、意図しない計算結果となっていた。

これは、SQLiteがINTEGERなどで定義した列にもTEXT型の値を許容してしまう仕様が原因。

参照

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?