简体   繁体   English

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

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

I've created a SQLite DB using System.Data.SQLite in a console app. 我在控制台应用程序中使用System.Data.SQLite创建了一个SQLite数据库。 I've then moved this to the Windows Phone's SD card. 然后我把它移到了Windows Phone的SD卡上。

I followed these instructions to add SQLite support to my WP8 app: https://github.com/peterhuene/sqlite-net-wp8 我按照这些说明为我的WP8应用添加了SQLite支持: https//github.com/peterhuene/sqlite-net-wp8

I locate the DB file and open it like so: 我找到了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>();

This results in the following error: 这会导致以下错误:

{SQLite.SQLiteException: no such table: tiles
   at SQLite.SQLite3.Prepare2(Database db, String query)
   at SQLite.SQLiteCommand.Prepare()
   at SQLite.SQLiteCommand.ExecuteScalar[T]()}

Interestingly, I've also tried the following SQL statement: 有趣的是,我还尝试了以下SQL语句:

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

Which gives a result of 0 suggesting my "tiles" table cannot be found? 给出0的结果表明我的“瓷砖”表无法找到?

I suspect that ExternalStorageFile.Path is returning a path that SQLite is unable to resolve as an existing file, leading it to create a new database and so complain about the missing table when I try to access it. 我怀疑ExternalStorageFile.Path正在返回一个SQLite无法解析为现有文件的路径,导致它创建一个新数据库,因此在我尝试访问它时会抱怨丢失的表。

This Microsoft article seems to suggest that I should be able to access files from the SD card from my app: http://msdn.microsoft.com/library/windowsphone/develop/jj720573%28v=vs.105%29.aspx 这篇微软文章似乎暗示我应该可以从我的应用程序访问SD卡中的文件: http//msdn.microsoft.com/library/windowsphone/develop/jj720573%28v=vs.105%29.aspx

Feedback provided by a Microsoft Employee: Microsoft员工提供的反馈:

Your app doesn't have direct access to the files on the SD card. 您的应用无法直接访问SD卡上的文件。 It can't open them directly with file system API, but needs to use the ExternalStorageFile and ExternalStorageFolder interfaces from Windows.Storage. 它无法直接使用文件系统API打开它们,但需要使用Windows.Storage中的ExternalStorageFile和ExternalStorageFolder接口。 To quote from Reading from the SD card on Windows Phone 8: 从Windows Phone 8上的SD卡读取引用:

Windows Phone apps can read specific file types from the SD card using the Microsoft.Phone.Storage APIs. Windows Phone应用程序可以使用Microsoft.Phone.Storage API从SD卡读取特定文件类型。

I expect that the SQLite implementation for the phone tries to open the database using standard C file API rather than using the Storage objects and so requires that the database be in the Xap or isolated storage and cannot access a database on the SD card (this is definitely the case for SQLite for Windows Store apps). 我希望手机的SQLite实现尝试使用标准C文件API而不是使用存储对象来打开数据库,因此要求数据库位于Xap或独立存储中,并且无法访问SD卡上的数据库(这是绝对适用于Windows Store应用程序的SQLite)。

In theory it would be possible to update SQLite to use Storage objects, but I suspect it would be a significant project to do so. 从理论上讲,可以更新SQLite以使用存储对象,但我怀疑这将是一个重要的项目。

Example bare-bones project: 示例裸骨项目:

I've created a bare-bones example project that highlights my issue, just in case anyone wants to look and potentially try out any ideas quickly: 我已经创建了一个简单的示例项目,突出了我的问题,以防万一有人想要查看并可能快速尝试任何想法:

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

Copy the bx24.nztopomap file to the root of your SD card to test. 将bx24.nztopomap文件复制到SD卡的根目录进行测试。

Feedback from SQLite SDK community: 来自SQLite SDK社区的反馈:

Apparently it should be fairly straight forward to add support to the SQLite SDK for someone with some C++ skills (mine are a bit rusty!): 显然,为具有某些C ++技能的人添加对SQLite SDK的支持应该是相当直接的(我的有点生锈!):

Replies: 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/msg81059.html http://www.mail-archive.com/sqlite-users@sqlite.org/msg81060.html

To my original question: http://www.mail-archive.com/sqlite-users@sqlite.org/msg81055.html 至于我原来的问题: http//www.mail-archive.com/sqlite-users@sqlite.org/msg81055.html

Anyone know of a SQLite library for Windows Phone that can read from the SD card? 有人知道可以从SD卡读取的Windows Phone SQLite库吗?

You don't need a library for read-only access, you just need to register the files extension in a manifest. 您不需要用于只读访问的库,只需在清单中注册文件扩展名即可。 That might look something like this: 这可能看起来像这样:

<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