简体   繁体   English

Xamarin 表单中的本地 SQLite 数据库

[英]Local SQLite Database in Xamarin Forms

So I did an example Database outside Xamarin Forms using "DB Browser for SQLite" and put it in Assets File所以我Xamarin Forms之外使用“DB Browser for SQLite”做了一个示例数据库并将其放入资产文件中

示例数据库

and I'm trying to learn how to connect my Database to it so i did this :我正在尝试学习如何将我的数据库连接到它,所以我这样做了:

XAML : XAML:

 <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Behoerdensprache.Pages.FinanzAmt"> <ContentPage.Content> <StackLayout> <Label Text="Welcome to Xamarin.Forms!" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" x:Name="LabelControl"/> <Entry Placeholder="Search" x:Name="SearchEntry"/> <Button Text="Press me" Clicked="Button_Clicked"/> </StackLayout> </ContentPage.Content> </ContentPage>

C# ButtonCode : C# 按钮代码:

    private void Button_Clicked(object sender, EventArgs e)
    {
        SQLiteConnection conn = new SQLiteConnection(App.DataBaseLocation);
        conn.CreateTable<Wortschatz>();
        var gets = conn.Table<Wortschatz>().Where(i => i.Wort == SearchEntry.Text).ToString();
        LabelControl.Text = gets;

        conn.Close();

    }

App.DataBaseLocation is defined in the Mainactivity and AppDelegate : App.DataBaseLocation在 Mainactivity 和 AppDelegate 中定义:

in Mainactivity :在主要活动中:

        string dbName = "BeamtenSprache.sqlite";
        string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        string fullPath = Path.Combine(folderPath, dbName);
        LoadApplication(new App(fullPath));

in AppDelegate :在 AppDelegate 中:

        string dbName = "wortschatz.sqlite";
        string folderPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "..", "Library");
        string fullPath = Path.Combine(folderPath, dbName);
        LoadApplication(new App(fullPath));

new App(fullPath) this part i defined in App.cs : new App(fullPath)这部分我在 App.cs 中定义:

 public static string DataBaseLocation = string.Empty;
    public App()
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage());
    }

    public App(string databaseLocation)
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage());
        DataBaseLocation = databaseLocation;
    }

but when i run the app and i press the Button this shows :但是当我运行应用程序并按下按钮时显示:

ON_ANDROID

There is an Todo sample project in xamarin.forms.samples shows how to store and access data in a local SQLite database. xamarin.forms.samples 中有一个Todo示例项目,展示了如何在本地 SQLite 数据库中存储和访问数据。

In Android project, he put the db file in Todo/Todo.Android/Resources/raw/Android项目中,他把db file放在Todo/Todo.Android/Resources/raw/

In iOS project, he put the db file in Todo/Todo.iOS/Resources/iOS项目中,他把db file放在Todo/Todo.iOS/Resources/

And then he get the db path by:然后他通过以下方式获取db path

public static class Constants
{
    public const string DatabaseFilename = "TodoSQLite.db3";

    public static string DatabasePath
    {
        get
        {
            var basePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            return Path.Combine(basePath, DatabaseFilename);
        }
    }
}

You can download it and try with the code there in your project.您可以下载它并尝试使用项目中的代码。 For more information, you can read the document .有关更多信息,您可以阅读文档

If you can't solve the problem, you can share your project here and I will check it:).如果你不能解决问题,你可以在这里分享你的项目,我会检查它:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM