微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 无法从WP8上的SD卡上的SQLite DB中检索数据

我在控制台应用程序中使用System.Data.sqlite创建了一个sqlite数据库.然后我把它移到了 Windows Phone的SD卡上.

我按照这些说明为我的WP8应用添加sqlite支持
https://github.com/peterhuene/sqlite-net-wp8

我找到了DB文件,然后像这样打开它:

ExternalStorageFile file = null;    
IEnumerable<ExternalStorageDevice> storageDevices = await ExternalStorage.GetExternalStorageDevicesAsync();
    foreach (ExternalStorageDevice storageDevice in storageDevices)
    {
        try
        {
            file = await storageDevice.GetFileAsync("northisland.nztopomap");
        }
        catch
        {
            file = null;
        }
        if (file != null) break;
    }

    sqliteConnection conn = new sqliteConnection("Data Source=" + file.Path + ";Version=3;Read Only=True;FailIfMissing=True;");

    sqliteCommand command = new sqliteCommand(_dbnorthIsland);
    command.CommandText = "SELECT COUNT(*) FROM tiles";
    int count = (int)command.ExecuteScalar<int>();

这会导致以下错误

{sqlite.sqliteException: no such table: tiles
   at sqlite.sqlite3.Prepare2(Database db,String query)
   at sqlite.sqliteCommand.Prepare()
   at sqlite.sqliteCommand.ExecuteScalar[T]()}

有趣的是,我还尝试了以下sql语句:

"SELECT COUNT(*) FROM sqlite_master WHERE type='table'"

给出0的结果表明我的“瓷砖”表无法找到?

我怀疑ExternalStorageFile.Path正在返回一个sqlite无法解析为现有文件的路径,导致它创建一个数据库,因此在我尝试访问它时会抱怨丢失的表.

这篇微软文章似乎暗示我应该可以从我的应用程序访问SD卡中的文件
http://msdn.microsoft.com/library/windowsphone/develop/jj720573%28v=vs.105%29.aspx

Microsoft员工提供的反馈:

您的应用无法直接访问SD卡上的文件.它无法直接使用文件系统API打开它们,但需要使用Windows.Storage中的ExternalStorageFile和ExternalStorageFolder接口.从Windows Phone 8上的SD卡读取引用:

Windows Phone apps can read specific file types from the SD card using
the Microsoft.Phone.Storage APIs.

我希望手机的sqlite实现尝试使用标准C文件API而不是使用存储对象来打开数据库,因此要求数据库位于Xap或独立存储中,并且无法访问SD卡上的数据库(这是绝对适用于Windows Store应用程序的sqlite).

从理论上讲,可以更新sqlite以使用存储对象,但我怀疑这将是一个重要的项目.

示例裸骨项目:

我已经创建了一个简单的示例项目,突出了我的问题,以防万一有人想要查看并可能快速尝试任何想法:

https://skydrive.live.com/?cid=de82af8533ac6d28&id=DE82AF8533AC6D28!242&ithint=file,.zip&authkey=!AF4IwcI0G7bsDFE

将bx24.nztopomap文件复制到SD卡的根目录进行测试.

来自sqlite SDK社区的反馈:

显然,为具有一些C技能的人添加sqlite SDK的支持应该是相当直接的(我的有点生锈!):

回复
http://www.mail-archive.com/sqlite-users@sqlite.org/msg81059.html
http://www.mail-archive.com/sqlite-users@sqlite.org/msg81060.html

我原来的问题:
http://www.mail-archive.com/sqlite-users@sqlite.org/msg81055.html

有人知道可以从SD卡读取的Windows Phone sqlite库吗?

解决方法

您不需要用于只读访问的库,只需在清单中注册文件扩展名即可.这可能看起来像这样:
<Extensions>
   <FileTypeAssociation Name="sqlite"
                        TaskID="_default"
                        NavUriFragment="filetoken=%s">
       <logos>
           <logo Size="small" IsRelative="true">...</logo>
           <logo Size="medium" IsRelative="true">...</logo>
           <logo Size="large" IsRelative="true">...</logo>
       </logos>
       <SupportedFileTypes>
         <FileType ContentType="application/sqlite">.sqlite</FileType>
       </SupportedFileTypes>
   </FileTypeAssociation>
</Extensions>

原文地址:https://www.jb51.cc/csharp/99716.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐