繁体   English   中英

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

[英]Unable to retrieve data from SQLite DB on SD Card on WP8

我在控制台应用程序中使用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应用程序可以使用Microsoft.Phone.Storage API从SD卡读取特定文件类型。

我希望手机的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>

暂无
暂无

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

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