- Home /
Retrieving data from Sqlite database using Mono.Data.Sqlite
I have an example code, it is minimal so you can just copy and paste it and try. (You also need Mono.Data.Sqlite.dll, System.Data.dll and sqlite3.dll)
The errors I receive are given as comments.
using UnityEngine;
using Mono.Data.Sqlite;
public class Example : MonoBehaviour
{
void Start ()
{
using (var dbcon = new SqliteConnection("URI=file:example.db,version=3"))
{
dbcon.Open();
using (var dbcmd = dbcon.CreateCommand())
{
dbcmd.CommandText = "CREATE TABLE Example (value REAL);";
dbcmd.ExecuteNonQuery();
}
using (var dbcmd = dbcon.CreateCommand())
{
dbcmd.CommandText = "INSERT INTO Example (value) VALUES (3.141592653589793);";
dbcmd.ExecuteNonQuery();
}
using (var dbcmd = dbcon.CreateCommand())
{
dbcmd.CommandText = "SELECT * FROM Example";
using (var reader = dbcmd.ExecuteReader())
{
if (reader.Read())
{
Debug.Log(reader.GetDataTypeName(0)); // REAL -- OK
Debug.Log(reader.GetName(0)); // value -- OK
Debug.Log(reader.GetFloat(0)); // 3.141593 -- seems OK
Debug.Log(reader.GetDouble(0)); // 3.14159265358979 -- OK
Debug.Log(reader.GetValue(0).GetType()); // System.Single -- Why?
Debug.Log(reader.GetFieldType(0)); // System.Single -- Why?
Debug.Log((float)reader.GetValue(0)); // 3.141593 -- OK
Debug.Log((double)(float)reader.GetValue(0)); // 3.14159274101257 -- also OK
Debug.Log((double)reader.GetValue(0)); // InvalidCastException -- Why?
}
}
}
}
}
}
The problem is, GetFieldType()
and GetValue().GetType()
both return System.Single
(aka float
). GetValue()
return as a float
type. However, it is certain that there is a double value in there which I can't get with GetValue()
.
I am trying to write a method that takes the reader, and converts it to List<Dictionary<string,object>>
but GetValue()
returning a float is an issue for me. Is this a Unity bug? or Mono bug? on not a bug at all?
Comment