C# - InvalidCastException when fetching double from sqlite
I keep getting a InvalidCastException when I'm fetching any double from my SQLite database in C#. The exception says "Specified cast is not valid."
I am able to see the value in a SQL manager so I know it exists. It is possible to fetch Strings (VARCHARS) and ints from the database. I'm also able to fetch the value as an object but then I get "66.0" when it's suppose to be "66,8558604947586" (latitude coordination).
Any one who knows how to solve this?
My code:
using System.Data.SQLite;
...
SQLiteConnection conn = 开发者_如何学Cnew SQLiteConnection(@"Data Source=C:\\database.sqlite; Version=3;");
conn.Open();
SQLiteDataReader reader = getReader(conn, "SELECT * FROM table");
//These are working
String name = reader.GetString(1);
Int32 value = reader.GetInt32(2);
//This is not working
Double latitude = reader.getDouble(3);
//This gives me wrong value
Object o = reader[3]; //or reader["latitude"] or reader.getValue(3)
You may want to check the data type you used to store the value in the database. It seems like you may be storing an integer but trying to retrieve it as a double.
Is the actual table schema defined to use a DOUBLE? SQLite will do some translation since the DB engine is typeless. If it is actually a SQL DOUBLE column, you could try a cast as well.
More information on SQLite type affinity.
精彩评论