需要一个带有 Monodroid 的 sqlite 示例 [英] Need an example of sqlite with Monodroid

查看:25
本文介绍了需要一个带有 Monodroid 的 sqlite 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我指出一个在 Monodroid 中使用 sqlite 的例子?我什至找不到一个.

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

推荐答案

我显然需要在 ApiDemo 示例中添加一个 SQLite 演示.

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:

但是,要使用以下代码,您必须面向 Android 2.2 或更高版本才能使用 Mono.Data.Sqlite.如果您需要针对较早的 Android 版本,则应考虑完全托管的替代品,例如 managed-sqlite.

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.

此外,此示例使用 Mono.Data.Sqlite.dll,包含在 MonoDroid SDK 中.

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

首先,编辑您的项目程序集引用并添加对Mono.Data.Sqlite.dllSystem.Data.dll 的引用.

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;

最后,使用普通的 ADO.NET 代码:

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 ("
	Key={0}; Value={1}",
                r ["Key"].ToString (), r ["Value"].ToString ());
}
connection.Close ();

这篇关于需要一个带有 Monodroid 的 sqlite 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆