我在控制台应用程序中使用 System.Data.SQLite 创建了 SQLite DB。然后我将其移至 Windows Phone 的 SD 卡。
我按照以下说明将 SQLite 支持添加到我的 WP8 应用程序:https://github.com/peterhuene/sqlite-net-wp8 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 表明找不到我的“tiles”表?
我怀疑ExternalStorageFile.Path返回的路径是SQLite无法解析为现有文件的路径,导致它创建一个新的数据库,因此当我尝试访问它时抱怨缺少表。
这篇 Microsoft 文章似乎建议我应该能够从我的应用程序访问 SD 卡中的文件:http://msdn.microsoft.com/library/windowsphone/develop/jj720573%28v=vs.105%29.aspx 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 应用程序可以使用以下命令从 SD 卡读取特定文件类型
Microsoft.Phone.Storage API。
我希望手机的 SQLite 实现尝试使用标准 C 文件 API 而不是使用存储对象来打开数据库,因此要求数据库位于 Xap 或独立存储中,并且无法访问 SD 卡上的数据库(这是对于 Windows 应用商店应用程序的 SQLite 来说绝对如此)。
理论上,可以更新 SQLite 以使用存储对象,但我怀疑这样做将是一个重要的项目。
简单项目示例:
我创建了一个简单的示例项目来突出我的问题,以防万一有人想要查看并可能快速尝试任何想法:
将 bx24.nztopomap 文件复制到 SD 卡根目录进行测试。
SQLite SDK 社区的反馈:
显然,对于具有一定 C++ 技能的人来说,添加对 SQLite SDK 的支持应该是相当简单的(我的有点生疏!):
回复:http://www.mail-archive.com/[电子邮件受保护]/msg81059.html http://www.mail-archive.com/sqlite-users@sqlite.org/msg81059.html
http://www.mail-archive.com/[电子邮件受保护]/msg81060.html http://www.mail-archive.com/sqlite-users@sqlite.org/msg81060.html
对于我原来的问题:http://www.mail-archive.com/[电子邮件受保护]/msg81055.html http://www.mail-archive.com/sqlite-users@sqlite.org/msg81055.html
有人知道 Windows Phone 上可以从 SD 卡读取的 SQLite 库吗?