简体   繁体   中英

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 = new 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM