- Home /
Generic sqlite query method
I'm using an SQLite database in my game. Right now, I have a generic query method that returns the IDataReader so I can then interpret the data.
public IDataReader Query(string _q)
{
IDbCommand dbcmd = dbconn.CreateCommand();
dbcmd.CommandText = _q;
IDataReader reader = dbcmd.ExecuteReader();
dbcmd.Dispose();
dbcmd = null;
return reader;
}
IDataReader reader = Query("SELECT FirstName FROM Person ORDER BY Age DESC");
while (reader.Read())
{
string firstname = reader.GetString(0);
Debug.Log(firstname);
}
reader.Close();
I wonder whether it's possible to adapt the function to return a (twodimensional if needed) array instead and skipping the interpreting the reader part after every query. The problem is that you can't just read the data, you need to tell the reader what kind of data it is by using the methods GetString or GetInt32,...
I guess that would mean you need to supply the data types you're querying?
I'd like to type something like:
var results = Query("SELECT FirstName, LastName, Age FROM Person", [string, string, int]);
And var would contain something like:
[ ["Testy", "McTest", 42],
["Arthur", "Dent", 36],
...]
Or any container that allows me to access all the data inside, as I suspect you can't have an array with different data types in it...
Is this possible at all in c#? Or will I have to be happy with what I have now and do that interpreting manually every time I query something?
Your answer
![](https://koobas.hobune.stream/wayback/20220612132828im_/https://answers.unity.com/themes/thub/images/avi.jpg)