【问题标题】:Local SQLite Database in Xamarin FormsXamarin 表单中的本地 SQLite 数据库
【发布时间】:2020-04-17 11:28:40
【问题描述】:

所以我使用“DB Browser for SQLite”做了一个示例数据库outside Xamarin Forms,并将其放入Assets File

我正在尝试学习如何将我的数据库连接到它,所以我这样做了:

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# 按钮代码:

    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 在 Mainactivity 和 AppDelegate 中定义:

在主要活动中:

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

在 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) 这部分我在 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;
    }

但是当我运行应用程序并按下按钮时,会显示:

【问题讨论】:

  • 你希望它显示什么?
  • 如果我在条目中写了“自动”之类的东西,它应该将标签文本更改为数据库中 Wort == Auto 的行。我的意思是这就是我想要做的。 .但不知何故我做错了! @杰森
  • 您在查询上调用 ToString(),而不是结果。如果您的查询返回多行怎么办?您打算如何在单个标签中显示整行数据?我建议对结果调用 ToList() 并将其存储在变量中。然后检查它是否至少有一个结果,然后提取要显示的行和列并更新Label
  • 我按照你说的做了,但也没有用..我的问题是我的应用程序找不到数据库或看到数据库为空..!我必须做些什么来让应用程序从资产文件中读取数据库吗?! @杰森
  • 如果在Assets中,可能需要先拷贝到app文件夹中

标签: database sqlite xamarin xamarin.forms connection-string


【解决方案1】:

xamarin.forms.samples 中有一个Todo 示例项目,展示了如何在本地 SQLite 数据库中存储和访问数据。

Android项目中,他将db file放入Todo/Todo.Android/Resources/raw/

iOS项目中,他将db file放入Todo/Todo.iOS/Resources/

然后他通过以下方式获得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);
        }
    }
}

您可以下载它并尝试使用项目中的代码。欲了解更多信息,您可以阅读document

如果你不能解决问题,你可以在这里分享你的项目,我会检查它:)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多