Potrzebny przykład sqlite z Monodroidem

Czy ktoś może wskazać mi przykład użycia SQLite z monodroidem? Nie udało mi się znaleźć nawet jednego.

Author: Yaqub Ahmad, 2011-02-09

1 answers

Oczywiście muszę dodać demo SQLite do próbki ApiDemo.

Ponieważ Nie wiem kiedy to się stanie, oto szybka i brudna wersja:

Jednak, aby użyć poniższego kodu musisz być celem Androida 2.2 lub nowszego, aby używać Mono.Data.Sqlite. Jeśli chcesz kierować na wcześniejszą wersję Androida, powinieneś przyjrzeć się w pełni zarządzanemu zamiennikowi, np. managed-SQLite.

Ponadto w tym przykładzie używa się Mono.Data.Sqlite.dll , który jest zawarty w MonoDroid SDK.

Najpierw edytuj referencje do montażu projektu i dodaj referencje dla Mono.Data.Sqlite.dll i System.Data.dll.

Po drugie, w kodzie źródłowym dodaj:

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

Wreszcie, użyj ye normal ADO.NET kod:

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 ();
 36
Author: jonp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-09 13:50:25