需要源码与Monodroid一个例子 [英] Need an example of sqlite with Monodroid
问题描述
任何人都可以点我使用SQLite与Monodroid的例子吗?我一直无法找到,即使之一。
Can anyone point me to an example of using sqlite with Monodroid? I've been unable to find even one.
推荐答案
我显然需要一个SQLite演示添加到ApiDemo样本。
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:
不过,C您的使用下面的$ C $必须的是针对Android 2.2或更高版本才能使用Mono.Data.Sqlite。如果您需要针对较早的Android版本,你应该考虑完全托管的替代品,如托管源码。
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.dll
和 System.Data.dll中
。
First, edit your project assembly references and add a reference for Mono.Data.Sqlite.dll
and System.Data.dll
.
二,内源$ C $ C,加入:
Second, within your source code, add:
using System.Data;
using Mono.Data.Sqlite;
最后,用你们正常的ADO.NET code:
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 ();
这篇关于需要源码与Monodroid一个例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!