如何解决错误的SQLite数据库路径? [英] How to resolve an incorrect path to SQLite Database?

查看:373
本文介绍了如何解决错误的SQLite数据库路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述-我添加了一些代码以将现有数据库复制到设备的本地文件夹中.到目前为止,如果现有数据库尚不存在,则第一个条件可以正常工作.

Overview - I've added some code to copy an existing database to the device's local folder. So far the first condition if the existing db doesn't already exist works fine.

问题- 但是,当执行将现有数据库从解决方案文件夹复制到设备文件夹的代码行执行时,出现了SQLite错误.该错误告诉我无法打开db文件.

Issue - But when the line of code to copy the existing db from the solution folder to the device folder executes, I get an SQLite error. The error tells me that the db file couldn't be opened.

在调试过程中,我看到DBPath与解决方案中的文件位置相同.因此,我不太确定路径可能出什么问题.

During debugging I see that the DBPath is the same as the file location in my solution. So I'm not too sure what could be wrong with the path.

(数据库附加为内容,并设置为始终复制".)

(The db is attached as content and set to "copy always".)

该软件包中db文件的完整路径为:

The full path to the db file in the package is :

C:\Users\Brian\Documents\Visual Studio 2013\Projects\Parking Tag Picker WRT\Parking Tag Picker WRT\Databases\ParkingZoneDatabase.db

问题:如何将数据库路径解析为SQLite类所需的正确路径?

Question: How can I resolve the db path to the correct path required by the SQLite class?

错误日志-在SQLite类中,抛出的异常的确切转储如下:

Error log - The exact dump of the exception that get's thrown here is as follows in the SQLite class:

SQLite.SQLiteException was unhandled by user code
  HResult=-2146233088
  Message=Could not open database file: C:\Data\Users\DefApps\APPDATA\Local\Packages\6d00c25c-39d2-443f-a29b-2c30c8ce7e99_gevy8cezwa384\LocalState\Databases\ParkingZoneDatabase.db (CannotOpen)
  Source=Parking Tag Picker WRT
  StackTrace:
       at SQLite.SQLiteConnection..ctor(String databasePath, SQLiteOpenFlags openFlags, Boolean storeDateTimeAsTicks)
       at SQLite.SQLiteConnection..ctor(String databasePath, Boolean storeDateTimeAsTicks)
       at Parking_Tag_Picker_WRT.Helpers.DatabaseHelper.ReadZones(String tableName)
       at Parking_Tag_Picker_WRT.ViewModel.TagRequestViewModel.InitZoneInfoAsync()
       at Parking_Tag_Picker_WRT.TagRequestPage.OnNavigatedTo(NavigationEventArgs e)
  InnerException: 

DBHelper代码 :(将现有数据库复制到设备上本地文件夹的代码)

DBHelper code: (code that copies the existing db to local folder on device)

    public const string DBPath = @"Databases\ParkingZoneDatabase.db";

    /// <summary>
    /// Load SQL_LiteTable from Solution   
    /// </summary>
    /// <param name="DBPATH"></param>
    /// <returns></returns>
    public async Task<bool> Init()
    {


        bool isDatabaseExisting = false;

        try
        {
            StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(DBPath);
            isDatabaseExisting = true;
        }
        catch
        {
            isDatabaseExisting = false;
        }

        if (!isDatabaseExisting)
        {
            //Fails at this line when retrieving the existing db
            StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(DBPath);
            await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
        }

        return true;          
    }

我还检查了不是设置为READ ONLY的db文件本身的权限-

I also checked the permissions on the db file itself which isn't set to READ ONLY -

推荐答案

首先,感谢您的回购.问题是您指定的路径.
使用await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);时,这意味着要将所选数据复制到本地文件夹(不在数据库文件夹内)以将其复制到数据库文件夹内,您需要先创建一个数据库文件夹,然后再复制该文件夹内的文件. br/> 现在,我提供了对您的AppDBPath进行更改以修复错误的简单解决方案.此解决方案仅将数据库复制到您的本地"文件夹中,而不是将其复制到数据库"文件夹中.

Firstly thanks for the repo. The problem is the path that you are specifying.
When you use await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);, that means you are copying the selected data to Local folder(not inside Database folder) to copy it inside a database folder you would require to create a Database folder first and then copy the file inside that folder.
For now I am giving the simple solution of making changes to your AppDBPath to fix the error. This solution copies the database to your Local folder only not inside Database folder.

public const string AppDBPath = @"ParkingZoneDatabase.db";
public const string PackageDBPath = @"Databases\ParkingZoneDatabase.db";


        /// <summary>
        /// Load SQL_LiteTable from Solution   
        /// </summary>
        /// <param name="DBPATH"></param>
        /// <returns></returns>
        public async Task<bool> Init()
        {


            bool isDatabaseExisting = false;

            try
            {
                StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(AppDBPath);
                isDatabaseExisting = true;
            }
            catch 
            {
                isDatabaseExisting = false;
            }


                if (!isDatabaseExisting)
                {

                    StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(PackageDBPath);
                    await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
                }


            return true;          
        }

由于您使用AppDBPath作为对数据库的引用,其余代码应该可以正常工作.

Since you are using AppDBPath as reference to your DB rest of code should work perfectly.

这篇关于如何解决错误的SQLite数据库路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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