开发者

C# - InvalidCastException when fetching double from sqlite

开发者 https://www.devze.com 2022-12-30 02:46 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号