I want to access a hsqldb database using android, but i get this error everytime java.sql.SQLException: Database lock acquisition failure: lockFile: org.hsqldb.persist.LockFile#dc61f9b4[file =/home/user/db/jade.lck,
this is my connexion class:
private static final String BDD_DRIVER = "org.hsqldb.jdbcDriver";
private static final String BDD_URI = "jdbc:hsqldb:file:home/user/db/jade;shutdown=true";
private static final String BDD_LOGIN = "sa";
private static final String BDD_PASS = "";
and this is how i get the connection:
Class.forName(BDD_DRIVER).newInstance();
connexion = DriverManager.getConnection(BDD_URI, BDD_LOGIN, BDD_PASS);
thank you for helping me.
When you get the error that you reported, it means the hsqldb.jar is present and an attempt was made to connect to the database. You must report the full error because after the file name, it includes the reason why there was a failure.
The full error means the directory indicated by jdbc:hsqldb:file:home/user/db/ does not exist, or it is a read-only directory where the .lck file cannot be created.
In order to narrow down the cause, you should find the correct absolute path by trying to list the existing .script file using a Java file method. An absolute path starts with the / character. You can then try to open the database as readonly, which does not create a .lck file. If this is successful, you can try to open the database normally.
Related
I want to save my logs to a folder which I can access with windows explorer. For example I want to create my log in the following path
This PC\Galaxy A5 (2017)\Phone\Android\data\MyApp\files
So I tried to use Environment variables... I get such as
/data/user/...
But here i cannot see the file what I created (using code I can access the path but I want to see in the explorer).
how I can create a path like above with code?
When I tried this code
var finalPath2 = Android.OS.Environment.GetExternalStoragePublicDirectory
(Android.OS.Environment.DataDirectory.AbsolutePath);
I get the path "/storage/emulated/0/data"
and
If i use the code
var logDirectory =Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.ApplicationData),"logs");
I get the following path like:
/data/user/0/MyApp/files/.config/logs
and
var logDirectory =Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.MyDocuments),"logs");
"/data/user/0/IM.OneApp.Presentation.Android/files/logs"
but unfortunately I cannot access this folder by explorer....
This PC\Galaxy A5 (2017)\Phone\Android\data\MyApp\files
So how to find out this path in c# by using environments?
Update:
when I give the following path hardcoded, it creates the file where I want..
logDirectory = "/storage/emulated/0/Android/data/MyApp/files/logs";
is there any environment to create this path? I can combine 2 environments and do some string processing in order to create this path. But maybe there is an easier way?
You are looking for the root of GetExternalFilesDir, just pass a null:
Example:
var externalAppPathNoSec = GetExternalFilesDir(string.Empty).Path;
Note: This is a Context-based instance method, you can access it via the Android application context, an Activity, etc... (see the link below to the Android Context docs)
Shared storage may not always be available, since removable media can be ejected by the user. Media state can be checked using Environment.getExternalStorageState(File).
There is no security enforced with these files. For example, any application holding Manifest.permission.WRITE_EXTERNAL_STORAGE can write to these files.
re: https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String)
string docFolder = Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.MyDocuments), "logs");
string libFolder = Path.Combine(docFolder, "/storage/emulated/0/Android/data/MyApp/files/logs");
if (!Directory.Exists(libFolder))
{
Directory.CreateDirectory(libFolder);
}
string destinationDatabasePath = Path.Combine(libFolder, "temp.db3");
db.Backup( destinationDatabasePath, "main");
I am trying to create a SQLIte database on Internal Storage in my Xamarin application. I am creating a system for an offline environment where at least 3 applications are inter-connected with shared data. So if one application creates some data the other application should be able to read that.
The Database project is a Portable Class Library which I plan to include in all three applications.
My DbHelper is as follows:
public Database()
{
string folder = "/data/data/Anchor.Main";
_dbPath = System.IO.Path.Combine(folder, "Anchor.db");
try
{
if (!System.IO.Directory.Exists(folder))
{
System.IO.Directory.CreateDirectory(folder); //Getting error here
}
_connection = new SQLiteConnection(_dbPath);
_connection.CreateTable<Orders>();
_connection.CreateTable<Items>();
}
catch(Exception ex)
{ }
}
I get error which says
Access to the path "/data/data/Anchor.Main" is denied.
Following is the stack trace
at System.IO.Directory.CreateDirectoriesInternal (System.String path)
[0x0004b] in <3fd174ff54b146228c505f23cf75ce71>:0 at
System.IO.Directory.CreateDirectory (System.String path) [0x00075] in
<3fd174ff54b146228c505f23cf75ce71>:0 at
anchorapp.db.Helper.Database..ctor () [0x0002e]
I understand this is because of the permissions, but what permissions do I need to set in order to write to the Internal storage from a PCL?
You are accessing Android system folder of "/data/data/Anchor.Main".
Your app internal storage will be in
/data/data/#PACKAGE_NAME#/files
You can use the following code to get the folder to store the database:
// Equal to /data/data/#PACKAGE_NAME#/files
var homePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
_dbPath = System.IO.Path.Combine(homePath, "Anchor.db");
_connection = new SQLiteConnection(_dbPath);
Android and iOS does not work that way. Each platform keeps apps in a sandboxed environment. If you want to create and store data in your app you need to create the path like the following sample:
var docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
_dbPath = System.IO.Path.Combine(docFolder, "Anchor.db");
Also set the permissions ReadExternalStorage and WriteExternalStorage in your Android project
Instead of writing to a private folder, you could create the database in a public one. To do so the docFolder would change to:
var docFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "YourDirectoryName");
Please be advised, that if you go that way everyone can see and open the database.
I did this by creating a sharedId within my projects and using the SharedContext to access the databases created by other apps
I am using google cloud storage to store images on android..I created project in google developers console and gave all id's from that project.
private static final String PROJECT_ID_PROPERTY = "xxxxxxxxxxxxxx"; //project ID
private static final String APPLICATION_NAME_PROPERTY = "refined"; //application name, can be any
private static final String ACCOUNT_ID_PROPERTY = "xxxxxxxxxxxxxxx-n5kqcd842faki0se7s82vpcf9l1rbvui#developer.gserviceaccount.com"; //user account email
Downloaded P12 file and accessing from the code for setting p12 key.
When I tried on 2 days back , images got uploaded correctly and I could see them in the browser..But when I try today the same code, it is giving forbidded issue.
{"code":403,"errors": [{"domain":"global","message":"Forbidden","reason":"forbidden"}],"message":"Forbidden"}
What could be the reason?
I could solve the issue...Issue was with permissions...
I gave write permissions on the bucket for the client ID, then it is working now.
Thanks for all
I am coding a Xamarin Android application, and am getting an error when trying to create a SQLite database.
Here is my code:
string applicationFolderPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "CanFindLocation");
string databaseFileName = System.IO.Path.Combine(applicationFolderPath, "CanFindLocation.db");
SQLite.SQLite3.Config(SQLite.SQLite3.ConfigOption.Serialized);
var db = new SQLiteConnection (databaseFileName);
Here is the error that I am getting:
SQLite.SQLiteException: Could not open database file: /data/data/com.xamarin.docs.android.mapsandlocationdemo2/files/CanFindLocation/CanFindLocation.db (CannotOpen)
I have the same code working in another Xamarin application and would like to know if the exception has something to do with the name of the package?
Thanks in advance
Does the path folder path that you are providing to SQLite exist? If you haven't created the CanFindLocation folder then opening a connection to that path will fail.
Try:
string applicationFolderPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "CanFindLocation");
// Create the folder path.
System.IO.Directory.CreateDirectory(applicationFolderPath);
string databaseFileName = System.IO.Path.Combine(applicationFolderPath, "CanFindLocation.db");
SQLite.SQLite3.Config(SQLite.SQLite3.ConfigOption.Serialized);
var db = new SQLiteConnection (databaseFileName);
My problem is related to added the option of "SQLiteOpenFlags.Create" during the initialization.
SQLiteAsyncConnection database = new SQLiteAsyncConnection(dbPath, SQLiteOpenFlags.Create);
public static Intelligence systemLogic;
/**
* #param args
*/
public String getNumber(String filepath) throws Exception{
String number1 = null;
Intelligence grabNumber = null;
grabNumber = new Intelligence(false);
number1 = grabNumber.recognize(new CarSnapshot(filepath));
}
It is failing while create instance itself
EDIT: See the answer on another similar question.
I assume the error happens while you're trying to initialize Intelligence object:
grabNumber = new Intelligence(false);
It happens because the Intelligence object in it's constructor is looking for ./config.xml and cannot find it in your project.
One possible solution would be to find the config.xml file in the JavaANPR jar file and put it in your classpath (if you're using eclipse, your project's main directory).
Another solution would be to create your own project with the JavaANPR source code, and edit the loading of config.xml as I had done. I will opensource my version of JavaANPR shortly.