Prologue:
I am writing SQLite GUI client for UWP. I use Microsoft.Data.Sqlite library for SQLite API with C#. Also I use a redirection table to be able to open database within my sandbox app which is published in Microsoft Store. Redirection table replaces CreateFileW to CreateFileFromAppW calls and similar.
Problem:
User has File -> Save as feature. When user creates a new database file is created inside app local directory. Next when user saves his/her database as I need to move this file. I use StorageFile API cause I cannot use any other file API within a sandbox app. So I call:
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
savePicker.FileTypeChoices.Add("SQLite3 Database", new List<string>() { ".sqlite", ".db" });
savePicker.SuggestedFileName = "Database";
var file = await savePicker.PickSaveFileAsync();
if(null != file)
{
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
sqliteConnection.Close(); // it is important cause if I skip this moving a file will fail
var localFolder = ApplicationData.Current.LocalFolder;
var currentStorageFile = await localFolder.GetFileAsync(App.UnsavedDatabaseFileName); // here I obtain StorageFile for opened database
await currentStorageFile.MoveAndReplaceAsync(file); // here I move a file
sqliteConnection = new SqliteConnection("Data Source=" + file.Path);
sqliteConnection.Open(); // this line fails with error 14: cannot open database file
}
I also tried to skip closing and reopening a connection -> then moving a file fails.
If I call FileOpenPicker between await currentStorageFile.MoveAndReplaceAsync(file); and sqliteConnection = new SqliteConnection("Data Source=" + file.Path); then everything will work fine but showing file open picker right after file save picker is a very bad user experience. I know that sandboxed app gives file access permission only after user selected a file manually. But it looks like that FileSavePicker does not give me a permission just like FileOpenPicker does. I could not find any info about it.
Epilogue:
This is the app https://sqliteman.dev. Please feel free to criticize cause it is what makes my app better.
If it is a permissions issue, then one solution would be to declare broadFileSystemAccess in your app's manifest.
https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions
I find it odd that you don't have write access to a file you create.
Try putting MoveAndReplaceAsync() in a using statement, and opening the SQLite connection outside of it.
Update:
I have tested with broadFileSystemAccess capbility, and it doesn't work and still report the exception "cannot open database file". Therefore, it isn't feasible to use broadFileSystemAccess capbility.
If you want to use Sqlite in uwp, there are only two location can be accessed. ApplicationData and Application install directory, the difference is that ApplicationInstallation can only be read, and ApplicationData can be read and written. So it reported the exception when you connnect the file located in another location.
My meaning is that you could store the database file anywhere, but when you operate this database file, you need to move this file to the location that can access.(ApplicationData,Application install directory). OtherWise, you can't insert data to the table of database file, even, you can't connect the database file.
Related
I have a database on "ms-appx:///Assets/mydbfile.db" . I want to copy it to the local application folder when the application will start the first time. Database Contain a large number of data.
I try with this Code. but it gives me an exception.
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///Assets/mydbfile.db"));
await file.CopyAsync(ApplicationData.Current.LocalFolder, "mydbfile.db");
Here Is My Db File
I tried with a text file and your code worked fine.
Do you have the Build Action in the file properties set to Content?
Where are you trying to run your code within your app?
Need help, I can't seem to access any other .sqlite database if its not located in the apps localfolder. Every tutorial I look at they always use
Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Example.sqlite");
I tried this:
const string testing = #"C:\Users\***\AppData\Local\Packages\*************\LocalState";
this.DBPath = Path.Combine(testing, "Example.sqlite");
using (var db = new SQLite.SQLiteConnection(this.DBPath))
{
db.CreateTable<Customer>();
}
and it worked. but when I change it to:
const string testing = #"C:\Databases";
It can't open the database even if I copied the database from the local folder of the app.
Any suggestions ? I'm still trying to learn.
You can't access the C: drive for windows store apps. It's part of the store's sandbox. Each app is limited to which files and folders can be viewed. If you have a local database file you need to access, define the file as content in your app and access it using the path "ms-appx:///..."
use file picker to select which folder you want to save the db file.
refer this article:
http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
Hi can someone recommend me how I can connect to sql lite db in Windows store app? I add my file called database.sqlite to my app like new item. But I cannot find how I can connect to my existing file. I find this:
StorageFile DataFile =
await ApplicationData.Current.LocalFolder.GetFileAsync("database.sqlite");
But I dont know how write correct path to file. Does anyone have some idea.
When you add a database file to your project, it is considered an asset of your project. The file is stored with your application, and is in a read-only location.
To access the read-only version, you have to use this:
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///relative/path/to/db/database.sqlite"));
Where relative/path/to/db is the path relative from the top level of your project. For example if your project has a folder called Data that has your db file in it, then you would use "ms-appx:///Data/database.sqlite".
Since the file is read-only, you cannot write to it. If you need to do so, you must first make a copy of it into a writeable location (e.g. ApplicationData.Current.LocalFolder):
// get reference to read-only copy
StorageFile readonlyDb = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///Data/database.sqlite"));
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
// make a copy in a writeable location
StorageFile DataFile = await readonlyDb.CopyAsync(localFolder, "database.sqlite");
// now open a sqlite connection to DataFile...
In my case, I want to build my own "drop box" like application which I am going to use as a part of my another project.
Discription:
When a word file is opened in the "drop box" folder(inside the folder where changes to the files, file creations deletions ect.. are identified). pictures, txts, txt updates are uploaded to the server without any issue.
But when it comes to office documents. office document creation is uploaded.
Problem:
when the word file is opened, and do some update and save it. the file can not be uploaded due to permission error. even the opened file can not be copied to another place and then uploaded.
Any one faced this kind of issue, and any sugessions.
But we can manually copy and save a opened and saved(but not closed) to another location
But in the program it is not allowed.
You can create another copy of file, this is important because uploading may be slower and reading shared file may lead to conflicts for Word, so what you can do is, you can create a copy quickly on temp file and upload the temp file.
string tmp = Path.GetTempFileName();
using(Stream s = new FileStream(filePath,
FileMode.Open, FileAccess.Read,
// following option will let you open
// opened file by other process
FileShare.ReadWrite)){
using(FileStream fs = File.OpenWrite(tmp)){
// this will copy file to tmp
s.CopyTo(fs);
}
}
// upload tmp file...
your problem is similar to what we faced. In our case we are all connected to a domain directory and the problem was the antivirus installed on our server gives read/write permissions to users (executing exe, installing apps). so you specifically need to give a user the right to execute an app that wants to use another app, in this case office docs.
The problem extended to asp apps using Crystal Reports. hope it helps.
I am having issues connecting to my sqlite database. The file is located in the application's folder. Here is the connection string
string path = "Data Source=MY.db";
I can get it to work if I use the absolute path, but it gives me a "table not found" error if I try to use a relative path. Any ideas?
You are opening up a different -- perhaps a new -- database that does not have said table. (Yes, SQLite will happily create a new database with the default connection settings.)
Make sure the correct database is opened. Remember, relative path is relative to the Current Working Directory, which is likely not that which is expected.
(The working directory is influenced from where, and how, the process is loaded. The working directory for a "Debug" session can be set under Project Settings / Debug / Start Options, for instance.)
Happy coding.
See also:
Make SQLite connection fail if database is missing? (deleted/moved)
Defining a working directory for executing a program (C#) (Shows how to set the current working directory to the directory containing the executing assembly.)
How do I get/set a winforms application's working directory?
Getting path relative to the current working directory?
This happened when you haven't saved the database and its table while using GUI Manager for SQLite .
Two solution;
1) Save your database and its table with CTR+S in GUI Manager
2) Or Simply Just close your GUI manager of SQlite and save all .
Important ! I am using GUI manger for SQLITE (DB Browser for SQLITE) and its all about that.
I've had the same problem for both my windows application (C#) and web application (ASP.net). I usually use SQLite because I found it more easier, especially when I worked with connection strings. But the main obstacle for me was to put a relative path in my code, so I can publish it without worrying about being unable to find the database. I've tried many things(using "|Data Directory|", "~/", "./", ...), and none of them works until I found these solutions. It seems the code is working for me, but wonder if I'm using them right?!
Web Application:
SQLiteConnection sql_con = new SQLiteConnection("Data Source =" + Server.MapPath("~/") + "mydb.db; Version = 3; New = false;);
Windows App:
SQLiteConnection sql_con = new SQLiteConnection("Data Source =" + System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "mydb.db; Version = 3; New = false; Read Only = true");
just replace your .database file into \bin\Debug in project folder, because in your case compiler creates DB file with same name but its totally empty 0bytes