How to use(create db, create table, query, etc) praeclarum sqlite-net?

Syaiful Nizam Yahya picture Syaiful Nizam Yahya · Nov 8, 2013 · Viewed 21.5k times · Source

I want to use sqlite-net available from this link https://github.com/praeclarum/sqlite-net.

Unfortunately, the getting started documentation are not enough. It doesnt even mention how to create a database. I tried looking at the examples, unfortunately, the examples are broken(unable to compile, run time error etc).

The most practical tutorial i can find on the net is http://blog.tigrangasparian.com/2012/02/09/getting-started-with-sqlite-in-c-part-one/

Unfortunately, sqlite-net doesnt fully support sqlite.org sqlite implementation, thus making the tutorial useless for praeclarum sqlite-net.

What is the equivalent method to do the same thing from the tutorial but in praeclarum sqlite-net?

From the tutorial

Create database(Here is where i stuck)

SQLiteConnection.CreateFile("MyDatabase.sqlite");

Connect to database

SQLiteConnection m_dbConnection;
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();

Create table

string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();

Fill table

string sql = "insert into highscores (name, score) values ('Me', 3000)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "insert into highscores (name, score) values ('Myself', 6000)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "insert into highscores (name, score) values ('And I', 9001)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();

Query database

string sql = "select * from highscores order by score desc";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
    Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);

Answer

Slack Shot picture Slack Shot · Nov 8, 2013

In your Where you can use lambdas. The classes are strongly typed.

Makes things a lot cleaner.

If you get into any amount of data caching, you'll end up wishing you had something like Microsoft's sync framework to use in Mono. I'm really guessing by your post, that you are looking at using Xamarin. Take a look at their SQLCipher component, if you are going to be caching data locally.

Also, if you do use SQLCipher via the component store.. it works in Android 2.3 on up. So don't expect a fully backward compatible system even with the Support Library added to your project.

var db = new SQLiteConnection("sqllite.db")

db.CreateTable<SyncRecord> ();

db.Insert (new SyncRecord () { SyncDate = DateTime.UtcNow });

var query = db.Table<SyncRecord> ().Where( /* your lambda to filter*/);