【问题标题】:Wrong path to SQLiteDB (or copy) on WinPhone. Xamarin.Forms PCLWinPhone 上 SQLiteDB(或副本)的路径错误。 Xamarin.Forms PCL
【发布时间】:2017-01-30 21:46:21
【问题描述】:

已为 Xamarin.Forms for PCL 安装 NUGet 包。我有 4 个适用于 Droid、iOS、Win 8.1 和 WinPhone 8.1 的项目。尝试连接我的数据库,但遇到问题:我的 Win 和 WinPhone 项目看不到它或返回错误的路径。关注官方 Xamarin.Forms 论坛。

界面:

public interface ISQLite
{
    string GetDatabasePath(string filename);
}

WInPhone 类:

using System.IO;
using Windows.Storage;
using Baumizer.WinPhone;
using Xamarin.Forms;

[assembly: Dependency(typeof(SQLite_WinPhone))]
namespace Baumizer.WinPhone
{
    public class SQLite_WinPhone:ISQLite
{
    public SQLite_WinPhone() { }
    public string GetDatabasePath(string filename)
    {
        return Path.Combine(ApplicationData.Current.LocalFolder.Path, filename);
    }
}}

这个类用于选择信息:

public class DataBase
{
    SQLiteConnection connection;

    public DataBase()
    {
        connection= new SQLiteConnection(DependencyService.Get<ISQLite>().GetDatabasePath("Database.db"));
    }

    public IEnumerable<Group> GetGroups()
    {
        return connection.Query<Group>("SELECT * FROM [Scedule] WHERE [facultyName] =" + "'" + Data.CurrentFaculty + "'");
    }
}

它在 Android 上运行良好。在 WinPhone 上,我得到 SQLite 异常 - 没有这样的表:Scedule。我打开 db 所在的模拟器的本地目录 - 0Kb。 我将 db 放入 Assets 并将 BuildInAction 设置为 Content。怎么了?需要帮助

【问题讨论】:

  • 您是否在某处创建表?还是现有的数据库?
  • 现有数据库

标签: c# .net sqlite xamarin xamarin.forms


【解决方案1】:

在论坛上找到这个代码,放到WinPhone App.cs中的OnLaunched(...)

if(await ApplicationData.Current.LocalFolder.GetItemAsync(Data.DataBaseName) == null)
        {
            StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync($"Assets\\{Data.DataBaseName}");
            await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
        }

如果数据库不存在,它会复制它,但它确实存在。可能我需要删除 DB 0Kb,但我不知道该怎么做。

成功了,OnLaunched():

try
        {
            await ApplicationData.Current.LocalFolder.GetItemAsync("Scedule.db");
        }
        catch (System.IO.FileNotFoundException)
        {
            StorageFile databaseFile =
                    await Package.Current.InstalledLocation.GetFileAsync($"Assets\\{"Scedule.db"}");
            await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
        }

DB 被错误地复制。可能还有其他方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多