使用在Xamarin本地数据库 [英] Use a local Database in Xamarin

查看:840
本文介绍了使用在Xamarin本地数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用Xamarin插件的Visual Studio来创建一个Android应用程序。

I have started using Xamarin plugin for Visual Studio to create an Android App.

我有我想要调用显示数据的本地SQL数据库。我不看我怎么能做到这一点。这可能吗?

I have a local sql database I want to call to display data. I don't see how I can do this. Is it possible?

推荐答案

想这是一个简单的事情后,我被证明是错误的,当我试图建立一个快速的测试项目。这篇文章将载有关于建立一个数据库的Andr​​oid应用程序在Xamarin会派上用场,作为未来Xamarin用户参考完整的教程。

After thinking this was a trivial thing to do, I was proven wrong when I tried setup a quick test project. This post will contain a full tutorial on setting up a DB for an Android App in Xamarin that will come in handy as a reference for future Xamarin users.

概览:

  1. 添加Sqlite.cs到项目中。
  2. 添加你的数据库文件作为资产。
  3. 设置你的数据库文件构建作为AndroidAsset。
  4. 手动复制数据库文件从你的apk到另一个目录。
  5. 在使用Sqlite.SqliteConnection打开数据库引黄联接。
  6. 在操作使用SQLite数据库上。

首先要这个仓库并下载Sqlite.cs;这提供了SQLite的API,你可以用它来对你的数据库运行查询。将文件添加到您的项目作为源文件。

Setting up a local database for a Xamarin Android project

1. Add Sqlite.cs to your project.

Start by going to this repository and downloading Sqlite.cs; this provides the Sqlite API that you can use to run queries against your db. Add the file to your project as a source file.

接下来,让你的数据库,并把它复制到你的Andr​​oid项目的资产目录,然后,使其出现下将其导入到你的项目中的资产的解决方案中的文件夹:

Next, get your DB and copy it into the Assets directory of your Android project and then import it into your project so that it appears beneath the Assets folder within your solution:

我用改名的本网站在本例子来db.sqlite的Chinook_Sqlite.sqlite数据库样本

I'm using the Chinook_Sqlite.sqlite database sample renamed to db.sqlite from this site throughout this example.

右键单击该数据库文件,并将其设置为建设行动 AndroidAsset 。这将确保将其纳入APK的资产的目录。

Right click on the DB file and set it to build action AndroidAsset. This will ensure that it is included into the assets directory of the APK.

由于DB是作为一种资产(APK内包装),您将需要提取出来。

As the DB is included as an Asset (packaged within the APK) you will need to extract it out.

您可以做到这一点用下面的code:

You can do this with the following code:

string dbName = "db.sqlite";
string dbPath = Path.Combine (Android.OS.Environment.ExternalStorageDirectory.ToString (), dbName);
// Check if your DB has already been extracted.
if (!File.Exists(dbPath))
{
    using (BinaryReader br = new BinaryReader(Assets.Open(dbName)))
    {
        using (BinaryWriter bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
        {
            byte[] buffer = new byte[2048];
            int len = 0;
            while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
            {
                bw.Write (buffer, 0, len);
            }
        }
    }
}

该提取DB从APK二进制文件,并将其放置到系统外部存储路径。现实的DB可以去任何你想要的,我只是选择了在这里坚守。

This extracts the DB as a binary file from the APK and places it into the system external storage path. Realistically the DB can go wherever you want, I've just chosen to stick it here.

我也看到了Android有这将直接存储数据库的数据库文件夹;我无法得到它的工作,所以我只是跑了使用现有数据库的这种方法。

I also read that Android has a databases folder that will store databases directly; I couldn't get it to work so I've just ran with this method of using an existing DB.

现在打开通过Sqlite.SqliteConnection类到数据库的连接:

Now open a connection to the DB through the Sqlite.SqliteConnection class:

using (var conn = new SQLite.SQLiteConnection(dbPath))
{
        // Do stuff here...
}

6。在操作数据库。

最后,作为Sqlite.net是一个ORM,你可以在数据库上使用自己的数据类型进行操作:

6. Operate on DB.

Lastly, as Sqlite.net is an ORM, you can operate on the database using your own data types:

public class Album
{
    [PrimaryKey, AutoIncrement]
    public int AlbumId { get; set; }
    public string Title { get; set; }
    public int ArtistId { get; set; }
}

// Other code...

using (var conn = new SQLite.SQLiteConnection(dbPath))
{
    var cmd = new SQLite.SQLiteCommand (conn);
    cmd.CommandText = "select * from Album";
    var r = cmd.ExecuteQuery<Album> ();

    Console.Write (r);
}

摘要

这就是如何将现有的SQLite数据库添加到针对Android的Xamarin的解决方案!欲了解更多信息,请查阅href="https://github.com/praeclarum/sqlite-net/tree/master/examples">范例附带Sqlite.net库unit测试和的范例的Xamarin文档。

Summary

And that's how to add an existing Sqlite database to your Xamarin solution for Android! For more information check out the examples included with the Sqlite.net library, its unit tests and the examples in the Xamarin documentation.

这篇关于使用在Xamarin本地数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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