Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need an example of sqlite with Monodroid

Can anyone point me to an example of using sqlite with Monodroid? I've been unable to find even one.

like image 888
basheps Avatar asked Feb 08 '11 21:02

basheps


People also ask

What is SQLite database explain with an example?

SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC,ODBC e.t.c.

How do I add SQLite to Visual Studio?

Getting Started with SQLite from a .Open Visual Studio, select new project, and, in Visual C#, select “Console Application” and provide the name as SQLiteDemo. Click OK. To connect SQLite with C#, we need drivers. Install all required SQLite resources from the NuGet package, as pictured in Figure 1.

Which database is best for Xamarin?

The most popular SQLite ORM for Xamarin is SQLite-net.


1 Answers

I obviously need to add a SQLite demo to the ApiDemo sample.

Since I don't know when that'll happen, here's the quick and dirty version:

However, to use the following code you must be targeting Android 2.2 or later to use Mono.Data.Sqlite. If you need to target an earlier Android version, you should look into a fully managed replacement, such as managed-sqlite.

Furthermore, this example is using Mono.Data.Sqlite.dll, which is included in the MonoDroid SDK.

First, edit your project assembly references and add a reference for Mono.Data.Sqlite.dll and System.Data.dll.

Second, within your source code, add:

using System.Data;
using Mono.Data.Sqlite;

Finally, use ye normal ADO.NET code:

string dbPath = Path.Combine (
        Environment.GetFolderPath (Environment.SpecialFolder.Personal),
        "items.db3");
bool exists = File.Exists (dbPath);
if (!exists)
    SqliteConnection.CreateFile (dbPath);
var connection = new SqliteConnection ("Data Source=" + dbPath);
connection.Open ();
if (!exists) {
    // This is the first time the app has run and/or that we need the DB.
    // Copy a "template" DB from your assets, or programmatically create one.
    var commands = new[]{
        "CREATE TABLE [Items] (Key ntext, Value ntext);",
        "INSERT INTO [Items] ([Key], [Value]) VALUES ('sample', 'text')"
    };
    foreach (var command in commands) {
        using (var c = connection.CreateCommand ()) {
            c.CommandText = command;
            c.ExecuteNonQuery ();
        }
    }
}
// use `connection`...
// here, we'll just append the contents to a TextView
using (var contents = connection.CreateCommand ()) {
    contents.CommandText = "SELECT [Key], [Value] from [Items]";
    var r = contents.ExecuteReader ();
    while (r.Read ())
        MyTextView.Text += string.Format ("\n\tKey={0}; Value={1}",
                r ["Key"].ToString (), r ["Value"].ToString ());
}
connection.Close ();
like image 77
jonp Avatar answered Sep 30 '22 02:09

jonp