Create a general query in SQLite-Net C # using SQLiteAsyncConnection

I am using SQLite-net (https://github.com/praeclarum/sqlite-net) to implement a database using Mono on Android and have the following functions:

public class DatabaseTestASync
{
private SQLiteAsyncConnection m_db;

public delegate void StocksFound(List list);
public event StocksFound StocksFoundListener;

// Uninteresting parts remove, eg constructor

public void AddStock(Stock stock)
{
m_db.InsertAsync( stock).ContinueWith(t => {Debug.WriteLine(stock.Symbol + "added"); });
}

public void GetAllStocks()
{
AsyncTableQuery query = m_db.Table().Where(stock => true);
query.ToListAsync().ContinueWith(QueryReturns);
}
< br /> private void QueryReturns(Task> t)
{
if (StocksFoundListener != null)
StocksFoundListener(t.Result);
}

This is very suitable for giving me a stock list, but I envision it in my There is a table collection in my project, and I don’t want to create a separate AddX, GetAllX, QueryReturns(X), etc. for each table. I am performing these general methods of database operations and trying the following methods:

public class DBQuery
{
private SQLiteAsyncConnection m_db;
public delegate void OnRecordsFound(List list);
public event OnRecordsFound RecordsFoundListener;< br />
public DBQuery (SQLiteAsyncConnection db)
{
m_db = db;
}

public void GetAllRecords()
{< br /> AsyncTableQuery query = m_db.Table().Where(r => true); // COMPILE ERROR HERE
query.ToListAsync().ContinueWith(QueryReturns);
}

private void QueryReturns(Task> t)
{
if (RecordsFoundListener != null)
RecordsFoundListener(t.Result);< br /> }
}

However, it did not compile saying:'T' must be a non-abstract type with a public parameterless constructor, so that it can be used in a generic type or method'DatabaseUtility.AsyncTableQuery Use it as the parameter'T' in'.

Any ideas on how to make this kind of general database access work?

I don’t really know SQLite internals, but it’s all about generics...

< /p>

Because AsyncTableQuery is defined like this

public class AsyncTableQuery
where T: new ()
{

...or just based on your mistake, you need to add the same constraint to your T:

public class DBQuery where T: new ( )

If you're using a class or a method which has a constrained generic
parameter
– you need to do the same on your method (or a class,
depending on where the T is defined).

I am using SQLite-net( https:/ /github.com/praeclarum/sqlite-net) Use Mono to implement the database on Android and have the following functions:

public class DatabaseTestASync
{
private SQLiteAsyncConnection m_db;

public delegate void StocksFound(List list);
public event StocksFound StocksFoundListener;

// Uninteresting parts remove, eg constructor

public void AddStock(Stock stock)
{
m_db.InsertAsync(stock).ContinueWith(t => {Debug.WriteLine(stock.Symbol + "added"); });
}
< br /> public void GetAllStocks()
{
AsyncTableQuery query = m_db.Table().Where(stock => true);
query.ToListAsync(). ContinueWith(QueryReturns);
}

private void QueryReturns(Task> t)
{
if (StocksFoundListener != null)
StocksFoundListener(t.Result);
}

This is great for giving me a list of stocks, but I envision a collection of tables in my project, and I don’t want to create a separate table for each AddX, GetAllX, QueryReturns(X), etc. I am performing these general methods of database operations and trying the following methods:

public class DBQuery
{
private SQLiteAsyncConnection m_db;
public delegate void OnRecordsFound(List list);
public event OnRecordsFound RecordsFoundListener;

public DBQuery (SQLiteAsyncConnection db)
{
m_db = db;
}

public void GetAllRecords()
{< br /> AsyncTableQuery query = m_db.Table().Where(r => true); // COMPILE ERROR HERE
query.ToListAsync().ContinueWith(QueryReturns);
}

private void QueryReturns(Task> t)
{
if (RecordsFoundListener != null)
RecordsFoundListener(t.Result);< br /> }
}

However, it did not compile saying:'T' must be a non-abstract type with a public parameterless constructor, so that it can be used in a generic type or method'DatabaseUtility.AsyncTableQuery Use it as the parameter'T' in'.

Any ideas on how to make this kind of general database access work?

I really don’t know the SQLite internals, but this is all about generics...

Because AsyncTableQuery is defined like this

public class AsyncTableQuery
where T: new ()
{

…or just based on your mistake, You need to add the same constraints to your T:

public class DBQuery where T: new ()

If you're using a class or a method which has a constrained generic
parameter
– you need to do the same on your method (or a class,
depending on where the T is defined).

Leave a Comment

Your email address will not be published.