Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a PCL with sqlite support in a xamarin cross platform application

We try to make a cross platform application using Xamarin in Visual Studio. We need to add sqlite support for android, ios and windows phone inside our Pcl project. We added Sqlite-Net PCL via NuGet but there wasn't any of SQLiteDataReader,SQLiteParameter or SQLiteDataAdapter. Is there any way to create a Pcl with mono.data.sqlite support for all of the three platforms?

like image 541
Stam Avatar asked Oct 01 '22 17:10

Stam


1 Answers

Xamarin has a decent example of a program called Tasky which uses SQLite and a PCL: http://docs.xamarin.com/guides/cross-platform/application_fundamentals/pcl/introduction_to_portable_class_libraries/

Here's an excerpt pertaining to SQLite and PCL:

The Portable Class Library is limited in the .NET features that it can support. Because it is compiled to run on multiple platforms, it cannot make use of [DllImport] functionality that is used in SQLite-NET. Instead SQLite-NET is implemented as an abstract class, and then referenced through the rest of the shared code. An extract of the abstract API is shown below:

public abstract class SQLiteConnection : IDisposable {

public string DatabasePath { get; private set; }
public bool TimeExecution { get; set; }
public bool Trace { get; set; }
public SQLiteConnection(string databasePath) { 
     DatabasePath = databasePath; 
}
public abstract int CreateTable<T>();
public abstract SQLiteCommand CreateCommand(string cmdText, params object[] ps);
public abstract int Execute(string query, params object[] args);
public abstract List<T> Query<T>(string query, params object[] args) where T : new();
public abstract TableQuery<T> Table<T>() where T : new();
public abstract T Get<T>(object pk) where T : new();
public bool IsInTransaction { get; protected set; }
public abstract void BeginTransaction();
public abstract void Rollback();
public abstract void Commit();
public abstract void RunInTransaction(Action action);
public abstract int Insert(object obj);
public abstract int Update(object obj);
public abstract int Delete<T>(T obj);

public void Dispose()
{
    Close();
}
public abstract void Close();

}

The remainder of the shared code uses the abstract class to “store” and “retrieve” objects from the database. In any application that uses this abstract class we must pass in a complete implementation that provides the actual database functionality.

like image 93
Coridan Avatar answered Oct 06 '22 02:10

Coridan