简体   繁体   English

SQLite Xamarin 中的数据库问题 Forms(sqlite-net-pcl)-“必须指定数据库路径”

[英]SQLite Database problem in Xamarin Forms (sqlite-net-pcl) - "DatabasePath must be specified"

I'm writing a simple Xamarin Forms mobile app (Android and iOS, but focusing on Android for now) as a school project.我正在编写一个简单的 Xamarin Forms 移动应用程序(Android 和 iOS,但现在专注于 ZE84E30B9390CDB46DZDB6 学校项目)。 I'm using Visual Studio 2019 Community and I have installed the NuGet package sqlite-net-pcl v1.7.335我正在使用 Visual Studio 2019 社区并且我已经安装了 NuGet package sqlite-net-pcl v1.7.335

Everything was moving along when suddenly the application started throwing errors any time database access was attempted, saying "DatabasePath must be specified".当尝试访问数据库时突然应用程序开始抛出错误时,一切都在进行,说“必须指定数据库路径”。

Concerned I might have made an error in my code, I created a blank application and attempted only a simple database connection.担心我的代码可能出错,我创建了一个空白应用程序并仅尝试了一个简单的数据库连接。 The same error occurs.发生同样的错误。 Please see my code below.请在下面查看我的代码。

MainActivity.cs MainActivity.cs

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        string fileName = "datahold.db";
        string folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        string completePath = Path.Combine(folderPath, fileName);

        LoadApplication(new App(completePath));
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

App.xaml.cs App.xaml.cs

public App(string filePath)
    {
        InitializeComponent();

        MainPage = new MainPage();
        dbPath = filePath;
    }

MainPage.xaml.cs主页.xaml.cs

 public partial class MainPage : ContentPage
{
    public MainPage()
    {
        
        InitializeComponent();
        using (SQLiteConnection myCon = new SQLiteConnection(App.dbPath))
        {
            myCon.CreateTable<term>();
        }
    }
}

Upon attempting to run the program, it highlights the using line above and gives:在尝试运行程序时,它会突出显示上面的 using 行并给出:

System.InvalidOperationException Message=DatabasePath must be specified System.InvalidOperationException Message=必须指定数据库路径

I can correct this issue only by moving the definition of the database into MainPage itself.我只能通过将数据库的定义移动到 MainPage 本身来纠正这个问题。 From there the rest of my program seems to be able to access that database file by referencing MainPage.dbPath;从那里开始,我的程序的 rest 似乎能够通过引用 MainPage.dbPath 来访问该数据库文件; however, the lack of communication between App.xaml and MainPage.xaml is very concerning and is likely to have further ramifications I am too inexperienced to predict.但是,App.xaml 和 MainPage.xaml 之间缺乏沟通是非常令人担忧的,并且可能会产生进一步的后果,我经验不足,无法预测。 Is this something I can fix with some sort of patch?这是我可以用某种补丁解决的问题吗? Should I change out sqlite-net-pcl for something else?我应该把 sqlite-net-pcl 换成别的东西吗?

you are creating MainPage before you assign dbPath , so when the MainPage constructor executes dbPath is null您在分配dbPath之前创建MainPage ,因此当MainPage构造函数执行时dbPathnull

MainPage = new MainPage();
dbPath = filePath;

assign dbPath first首先分配dbPath

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

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