[英]MBTiles db with SQLite in a c# windows store app
I am doing a windows store (8.1) app in c# Xaml to generate maps using the Bing Maps for c# extension with a mbtiles file, I've already used the project Portable Basemap Server to do the work but now I'm trying access the data in the mbtiles file by myself with SQLite. 我正在c#Xaml中执行Windows商店(8.1)应用程序,以使用mbtiles文件使用Bing Maps for c#扩展名生成地图,我已经使用项目Portable Basemap Server进行了工作,但是现在我正在尝试访问我自己使用SQLite在mbtiles文件中存储数据。
I managed to get tiles from that kind of files in WPF but I don't know how to do it in a windows store project; 我设法从WPF中的此类文件中获取切片,但我不知道如何在Windows Store项目中进行操作。
My code in WPF : 我在WPF中的代码:
SQLiteConnection _sqlcon;
using (_sqlcon = new SQLiteConnection(String.Format("Data Source={0};Version=3;", "PATH_TO_MBTILES")))
{
_sqlcon.Open();
SQLiteCommand cmd = new SQLiteCommand(string.Format("SELECT tile_data FROM tiles WHERE tile_column={0} AND tile_row={1} AND zoom_level={2}", 2, 5, 3), _sqlcon);
object o = cmd.ExecuteScalar();
if (o != null)
{
byte[] c = (byte[])o;
using (MemoryStream stream = new MemoryStream(c))
{
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.StreamSource = stream;
bmp.EndInit();
img.Source = bmp;
}
}
}
With that code I get the tile at column 2 row 5 and zoom level 3. 使用该代码,我将获得第2列第5行和缩放级别3的图块。
But in windows store app I get a SQLiteException "Could not open database file" when I try to create a SQLiteConnection
with the same file (I am using the NuGet sqlite-net and the SqLite for Windows Runtime 8.1 extension) 但是在Windows应用商店应用程序中,当我尝试使用同一文件创建
SQLiteConnection
时,我得到了SQLiteException“无法打开数据库文件”(我使用的是NuGet sqlite-net和Windows Runtime 8.1扩展的SQLiteConnection
)。
My code in Windows Store App : 我在Windows Store App中的代码:
SQLiteConnection _sqlcon;
using (_sqlcon = new SQLiteConnection(String.Format("Data Source={0};Version=3;", "PATH_TO_MBTILES"))) //SQLiteExeption here
{
SQLiteCommand cmd = new SQLiteCommand(string.Format("SELECT tile_data FROM tiles WHERE tile_column={0} AND tile_row={1} AND zoom_level={2}", 2, 5, 3), _sqlcon);
byte[] o = cmd.ExecuteScalar<byte[]>();
//etc...
}
The debugger of Visual Studio send me to the SQLite.cs file of the sqlite-net NuGet : Visual Studio的调试器将我发送到sqlite-net NuGet的SQLite.cs文件:
public SQLiteConnection (string databasePath, SQLiteOpenFlags openFlags, bool storeDateTimeAsTicks = false)
{
if (string.IsNullOrEmpty (databasePath))
throw new ArgumentException ("Must be specified", "databasePath");
DatabasePath = databasePath;
#if NETFX_CORE
SQLite3.SetDirectory(/*temp directory type*/2, Windows.Storage.ApplicationData.Current.TemporaryFolder.Path);
#endif
Sqlite3DatabaseHandle handle;
#if SILVERLIGHT || USE_CSHARP_SQLITE
var r = SQLite3.Open (databasePath, out handle, (int)openFlags, IntPtr.Zero);
#else
// open using the byte[]
// in the case where the path may include Unicode
// force open to using UTF-8 using sqlite3_open_v2
var databasePathAsBytes = GetNullTerminatedUtf8 (DatabasePath);
var r = SQLite3.Open (databasePathAsBytes, out handle, (int) openFlags, IntPtr.Zero);
#endif
Handle = handle;
if (r != SQLite3.Result.OK) {
throw SQLiteException.New (r, String.Format ("Could not open database file: {0} ({1})", DatabasePath, r));
}
_open = true; // Debugger Stops here !
StoreDateTimeAsTicks = storeDateTimeAsTicks;
BusyTimeout = TimeSpan.FromSeconds (0.1);
}
I have found many examples of mbtiles files used in WPF but none in a windows store app. 我发现在WPF中使用过许多mbtiles文件的示例,但在Windows store应用程序中却没有。
Do the SQLite extensions for Windows store dev support MBTiles files as databases ? Windows应用商店的SQLite扩展是否支持MBTiles文件作为数据库? If yes what am I doing wrong ?
如果是,我在做什么错?
Windows Store apps can access only their own directories, but not the private files of the admin user. Windows应用商店应用只能访问自己的目录,而不能访问管理员用户的私人文件。
Read the documentation , but you probably want to install the data as part of your app. 阅读文档 ,但是您可能希望将数据安装为应用程序的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.