I'm trying to store a local SQLite database in the internal storage of the device. When I was using an emulator , this:
static string dbName = "totems.sqlite";
string dbPath = Path.Combine (Android.OS.Environment.ExternalStorageDirectory.ToString (), dbName);
worked fine. But when I tried to debug on my Nexus 5, it didn't work, because it doesn't have external storage. I searched where to store it so it could run on my Nexus as well. I replaced it with:
static string dbName = "totems.sqlite";
string dbPath = Path.Combine ("/data/data/com.companyname.totem/databases/", dbName);
But now it doesn't work on my Nexus 5 and it doesn't work on my emulator. It says it can't find the path.
What am I doing wrong?
Thanks in advance.
I know this is an old thread, but the old answer has a typo. The working syntax should be :
string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "DatabaseName.txt");
Im using the following code:
string path = Path.Combine(System.Enviroment.GetFolderPath(System.Enviroment.SpecialFolder.Personal), "data.txt");
I think that should write into the internal storage like you want
From docs.com
LocalApplicationData: The directory that serves as a common
repository for application-specific data that is used by the current,
non-roaming user.
Personal: The directory that serves as a common repository for
documents. This member is equivalent to MyDocuments.
From this I use:
var db_directory = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData),"databases"))
var db_path = Path.Combine(db_directory,"data.db");
Also from this sample (XF)
docs.com xamarin tutorial
public static Database Database
{
get
{
if (database == null)
{
database = new Database(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "people.db3"));
}
return database;
}
}
Related
I always had a working version in Azure but somehow it does not work anymore, I copied the database and I created new connections. When I run in localhost all works fine, however when I publish it does not work anymore, unless I delete the analytics section of the REST
this is what I got:
// GET PROGRAM (IF EXIST)
[Route("api/Program/{ProgramCode}/{id_user}/{id_client}")]
public object GetProgramSettingsFromFile(string ProgramCode, string id_user, int id_client)
{
AnalyticsUser newEntry = new AnalyticsUser
{
id_user = id_user,
ProgramCode = ProgramCode,
DateTime = DateTime.Now,
id_client = id_client
};
db.AnalyticsUsers.Add(newEntry);
db.SaveChanges();
string secondPart = "api/program/" + ProgramCode + ".json";
var allText = (new WebClient()).DownloadString(uriPath + secondPart);
object jsonObject = JsonConvert.DeserializeObject(allText);
return jsonObject;
}
the above works fine in local host, but when published it does not work. Error 500 internal server error. So when i escape the analytics section like this:
// GET PROGRAM (IF EXIST)
[Route("api/Program/{ProgramCode}/{id_user}/{id_client}")]
public object GetProgramSettingsFromFile(string ProgramCode, string id_user, int id_client)
{
//AnalyticsUser newEntry = new AnalyticsUser
//{
// id_user = id_user,
// ProgramCode = ProgramCode,
// DateTime = DateTime.Now,
// id_client = id_client
//};
//db.AnalyticsUsers.Add(newEntry);
//db.SaveChanges();
string secondPart = "api/program/" + ProgramCode + ".json";
var allText = (new WebClient()).DownloadString(uriPath + secondPart);
object jsonObject = JsonConvert.DeserializeObject(allText);
return jsonObject;
}
it works fine on localhost and on published. So what is going in here? I can't seem to replicate the error locally since it all works fine. Is there any idea?
alright it was something in the Azure SQL server that was producing the error. For other that might encounter the same problem, cause localhost connecting to the database was working fine, also using the webconfig connection string, means that something is blocking it when published.
In Azure SQL Database Firewall settings there is a switch:
Allow Azure services and resources to access this server
This one had to be to yes (was on No)
Now it's working fine (the SAVE analytics setting is an internal connection) that is why ta was working without fine but with it got blocked.
Because you can run normally locally, you can debug or find the ConnectionString in the project (appconfig.json or web.config file).
Check your ConnectionString and replace the SQL Server connection string in the production environment.
You can configure the strings of the production environment in the configuration file or on the portal.
Tips:
If you don't have azure sql server, as long as the sql server that can be accessed on the public network is also possible.
Note that the table structure and stored procedures or functions of the database in the production environment must be consistent.
I am using SQLite-net nuget package in my UWP application. I want to create a local database file to use as such:
var s = new SQLiteConnection("myDbSQLite.db3", SQLiteOpenFlags.Create);
But it throws the error:
Could not open database file:
C:\Path\MyProject\bin\x86\Debug\AppX\myDbSQLite.db3 (Misuse)
I see in other posts they suggest to use SQLiteConnection.CreateFile("MyDatabase.sqlite"); but I don't see that method?
EDIT
The code
FileStream fs = File.Create(path);
Throws the exception:
UnauthorizedAccessException access to the path is denied
So I think this is a permission issue I am having with UWP. Is there something in the capabilities that I need to set?
Check your permissions on the folder, and also try using this for the constructor
_database = new SQLiteAsyncConnection(
"myDbSQLite.db3",
SQLiteOpenFlags.Create |
SQLiteOpenFlags.FullMutex |
SQLiteOpenFlags.ReadWrite );
Because creating a file in UWP must be done with the UWP API, if you're going to use this nuget library, you have to accommodate by creating it yourself first:
// Create the empty file; replace if exists.
Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
storageFolder.CreateFileAsync("myDbSQLite.db3", Windows.Storage.CreationCollisionOption.ReplaceExisting);
My UWP app is actually part of a Xamarin.Forms app that is using shared code, so if your app is solely UWP there's probably a better library, such as this one that Codexer referred.
You should use a folder wher you have write-access to. So please try the following code:
String path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string dbFile = Path.Combine( path, "myDbSQLite.db3");
var s = new SQLiteConnection( dbFile, SQLiteOpenFlags.Create);
This worked for me:
var databasePath = Path.Combine(GetLocalFileDirectory(), "MyData.db");
try
{
// Create the empty file; replace if exists.
db = new SQLiteAsyncConnection(databasePath,
SQLiteOpenFlags.Create |
SQLiteOpenFlags.FullMutex |
SQLiteOpenFlags.ReadWrite);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
public string GetLocalFileDirectory()
{
var docFolder = FileSystem.AppDataDirectory
var libFolder = Path.Combine(docFolder, "Databases");
if (!Directory.Exists(libFolder))
{
Directory.CreateDirectory(libFolder);
}
return libFolder;
}
i have the following code for the interface in xamarin.
public interface ISQLiteDb
{
SQLiteAsyncConnection GetConnection();
}
then if i go to Android in my DB folder i have this
var documentsPath =
Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, "shopItems.db3");
return new SQLiteAsyncConnection(path);
in IOS its pretty much the same
The problem that i'm having is that i cant seem to be able to setup a table that i can use.
As you can see im trying to setup Async DB, i know that for sync one you need to do this
static SQLite.Net.SQLiteConnection database;
//creates the database itself
public DBFunc()
{
database = new SQLite.Net.SQLiteConnection(DependencyService.Get<ISQLitePlatform>(),
DependencyService.Get<SqlSync.IFileHelper>().GetLocalPath("shopItems.db3"));
database.CreateTable<ShopItems>();
}
So the question that i got confused with is how can i setup the same table with async methods? How can i the path where it should be located and how can i connect to it when i need to get data from there?
Hi first time using stackoverflow. I apologize in advance if I did something wrong. The below code works perfectly in windows in regards to returning the path, however in Ubuntu using mono 2.10.8.1
the path should be /home/procon/Plugins/BF3/filename.cfg
instead it's looking for the file at /home/procon/home/procon/Plugins/BF3/filename.cfg
I have narrowed down the issue to this code below, and I've tried changing Application.ExecutablePath to Environment.CurrentDirectory and it's still returning the wrong path. Any ideas?
public static String makeRelativePath(String file)
{
String exe_path = Directory.GetParent(Application.ExecutablePath).FullName;
String dll_path = Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName;
String rel_path = dll_path.Replace(exe_path, "");
rel_path = Path.Combine(rel_path.Trim(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }), file);
return rel_path;
}
I'm trying to get the installation path of winrar (if someone installs it on C:\users\admin\ for example) within my application using C#, I found this method:
http://www.dreamincode.net/code/snippet1995.htm
It works for many programs, but it didn't work for winrar. Does anybody know how?? Thanks!!
string GetPath(string extension)
{
var appName = (string)Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extension).GetValue(null);
var openWith = (string)Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(appName + #"\shell\open\command").GetValue(null);
var appPath = System.Text.RegularExpressions.Regex.Match(openWith, "[a-zA-Z0-9:,\\\\\\. ]+").Value.Trim();
return new FileInfo(appPath).Directory.FullName;
}
GetPath(".rar");