Location of sqlite database on the device - android

I've created a sqlite database programmatically with the default way of extending SQLiteOpenHelper and overriding onCreate(). This way the db gets created on the fly when needed.
I'd like to check the contents of the db file on my OS X machine with a sqlite browser.
I know the name of the db file, but I can't find it on the device. I've connected to the device via USB and looked with finder and terminal, but I just can't find the db file.
What is the default location for a sqlite databases on an android device?

You can find your created database, named <your-database-name>
in
//data/data/<Your-Application-Package-Name>/databases/<your-database-name>
Pull it out using File explorer and rename it to have .db3 extension to use it in SQLiteExplorer
Use File explorer of DDMS to navigate to emulator directory.

For this, what I did is
File f=new File("/data/data/your.app.package/databases/your_db.db3");
FileInputStream fis=null;
FileOutputStream fos=null;
try
{
fis=new FileInputStream(f);
fos=new FileOutputStream("/mnt/sdcard/db_dump.db");
while(true)
{
int i=fis.read();
if(i!=-1)
{fos.write(i);}
else
{break;}
}
fos.flush();
Toast.makeText(this, "DB dump OK", Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(this, "DB dump ERROR", Toast.LENGTH_LONG).show();
}
finally
{
try
{
fos.close();
fis.close();
}
catch(IOException ioe)
{}
}
And to do this, your app must have permission to access SD card, add following setting to your manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Not a brilliant way, but works.

The Context contains many path functions: Context.getXXXPath()
One of them is android.content.Context.getDatabasePath(String dbname) that returns the absolute path of a database called dbname.
Context ctx = this; // for Activity, or Service. Otherwise simply get the context.
String dbname = "mydb.db";
Path dbpath = ctx.getDatabasePath(dbname);
The returned path, in this case, would be something like:
/data/data/com.me.myapp/databases/mydb.db
Note that this path is autogenerated if using SQLiteOpenHelper to open the DB.

If you're talking about real device /data/data/<application-package-name> is unaccessible. You must have root rights...

This is and old question, but answering may help others.
Default path where Android saves databases can not be accesed on non-rooted devices. So, the easiest way to access to database file (only for debugging environments) is to modify the constructor of the class:
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
MySQLiteOpenHelper(Context context) {
super(context, "/mnt/sdcard/database_name.db", null, 0);
}
}
Remember to change for production environments with these lines:
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
MySQLiteOpenHelper(Context context) {
super(context, "database_name.db", null, 0);
}
}

/data/data/packagename/databases/
ie
/data/data/com.example.program/databases/

A SQLite database is just a file. You can take that file, move it around, and even copy it to another system (for example, from your phone to your workstation), and it will work fine. Android stores the file in the /data/data/packagename/databases/ directory. You can use the adb command or the File Explorer view in Eclipse (Window > Show View > Other... > Android > File Explorer) to view, move, or delete it.

well this might be late but it will help.
You can access the database without rooting your device through adb
start the adb using cmd and type the following commands
-run-as com.your.package
-shell#android:/data/data/com.your.package $ ls
cache
databases
lib
shared_prefs
Now you can open from here on.

If you name your db as a file without giving a path then most common way to get its folder is like:
final File dbFile = new File(getFilesDir().getParent()+"/databases/"+DBAdapter.DATABASE_NAME);
where DBAdapter.DATABASE_NAME is just a String like "mydatabase.db".Context.getFilesDir() returns path for app's files like: /data/data/<your.app.packagename>/files/ thats why you need to .getParent()+"/databases/", to remove "files" and add "databases" instead.
BTW Eclipse will warn you about hardcoded "data/data/" string but not in this case.

By Default it stores to:
String DATABASE_PATH = "/data/data/" + PACKAGE_NAME + "/databases/" + DATABASE_NAME;
Where:
String DATABASE_NAME = "your_dbname";
String PACKAGE_NAME = "com.example.your_app_name";
And check whether your database is stored to Device Storage. If So, You have to use permission in Manifest.xml :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

You can access it using adb shell how-to
Content from above link:
Tutorial : How to access a Android database by using a command line.
When your start dealing with a database in your program, it is really
important and useful to be able to access it directly, outside your
program, to check what the program has just done, and to debug.
And it is important also on Android.
Here is how to do that :
1) Launch the emulator (or connect your real device to your PC ). I
usually launch one of my program from Eclipse for this. 2) Launch a
command prompt in the android tools directory. 3) type adb shell. This
will launch an unix shell on your emulator / connected device. 4) go
to the directory where your database is : cd data/data here you have
the list of all the applications on your device Go in your application
directory ( beware, Unix is case sensitive !! ) cd
com.alocaly.LetterGame and descend in your databases directory : cd
databases Here you can find all your databases. In my case, there is
only one ( now ) : SCORE_DB 5) Launch sqlite on the database you want
to check / change : sqlite3 SCORE_DB From here, you can check what
tables are present : .tables 6) enter any SQL instruction you want :
select * from Score;
This is quite simple, but every time I need it, I don't know where to
find it.

Do not hardcode path like //data/data/<Your-Application-Package-Name>/databases/<your-database-name>. Well it does work in most cases, but this one is not working in devices where device can support multiple users. The path can be like //data/user/10/<Your-Application-Package-Name>/databases/<your-database-name>. Possible solution to this is using context.getDatabasePath(<your-database-name>).

If your application creates a database, this database is by default saved in the directory DATA/data/APP_NAME/databases/FILENAME.
The parts of the above directory are constructed based on the following rules. DATA is the path which the Environment.getDataDirectory() method returns. APP_NAME is your application name. FILENAME is the name you specify in your application code for the database.

You can find your database file :
Context.getDatabasePath(getDatabaseName())
getDatabaseName from class SQLiteOpenHelper

In kotlin you can find it in this way:
val data: File = Environment.getDataDirectory()
val currentDBPath = "//data//$packageName//databases//"
val destDir = File(data, currentDBPath)

You can also check whether your IDE has a utility like Eclipse's DDMS perspective which allows you to browse through the directory and/or copy files to and from the Emulator or a rooted device.

Define your database name like :
private static final String DATABASE_NAME = "/mnt/sdcard/hri_database.db";
And you can see your database in :
storage/sdcard0/yourdatabasename.db

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
MySQLiteOpenHelper(Context context) {
super(context, "/mnt/sdcard/database_name.db", null, 0);
} }
Do not hardcode "/sdcard/"; use Environment.getExternalStorageDirectory().getPath() instead

#Shardul's answer is correct.
Besides that, new Android Studio has a tool called: App Inspection, which you can view database directly.
Here is the version of Android Studio:
Android Studio Arctic Fox | 2020.3.1 Patch 3
Build #AI-203.7717.56.2031.7784292, built on October 1, 2021

Related

Application crashes after clearing storage with error code 14 SQLITE_CANTOPEN: Could not open database

After clearing application storage & cache, started getting unknown error (code 14 SQLITE_CANTOPEN): Could not open database. Before clearing storage, everything was working properly, both Emulator and Device.
I tried deleting application from Emulator, wiped Emulator and cleaned project, but still crashing when opening MainActivity. For the sake of problem solving, I've put writing permissions in AndroidManifest but still the same error.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Instead of doing
this.baza_putanja = "/data/data/" + context.getPackageName() + "/" + "databases/";
I did
baza_putanja = context.getDatabasePath(baza_ime).getPath();
What bothers me is if I'm trying to open database using a path that does not include the database name - why was it working before clearing application storage & cache?
BazaPodatakaHelper.java
private String baza_putanja = null;
private static final String baza_ime = "eng_dictionary.db";
private SQLiteDatabase econDictionary;
private final Context mContext;
public BazaPodatakaHelper(Context context) {
super(context, baza_ime, null, 1);
this.mContext = context;
this.baza_putanja = "/data/data/" + context.getPackageName() + "/" + "databases/";
Log.e("Putanja 1", baza_putanja);
}
public void kreirajBazuPodataka() throws IOException {
boolean bazaPodatakaPostoji = provjeriBazuPodataka();
if ( !bazaPodatakaPostoji ) {
this.getReadableDatabase();
try {
kopirajBazuPodataka();
} catch (IOException e) {
throw new Error("Greška prilikom kopiranja baze podataka");
}
}
}
public boolean provjeriBazuPodataka() {
SQLiteDatabase provjera;
try {
String putanja = baza_putanja + baza_ime;
provjera = SQLiteDatabase.openDatabase(putanja, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
throw new Error("Greška prilikom provjeravanja baze podataka");
}
if (provjera != null) {
provjera.close();
}
return provjera != null;
}
Error Log:
2019-07-17 20:39:30.482 7885-7885/com.benjaminkljn.econdictionary E/Putanja 1: /data/data/com.benjaminkljn.econdictionary/assets/ 2019-07-17 20:39:30.485 7885-7885/com.benjaminkljn.econdictionary E/SQLiteLog: (14) cannot open file at line 36683 of [c255889bd9] 2019-07-17 20:39:30.485 7885-7885/com.benjaminkljn.econdictionary E/SQLiteLog: (14) os_unix.c:36683: (2) open(/data/data/com.benjaminkljn.econdictionary/assets/eng_dictionary.db)
- 2019-07-17 20:39:30.491 7885-7885/com.benjaminkljn.econdictionary E/SQLiteDatabase: Failed to open database '/data/data/com.benjaminkljn.econdictionary/assets/eng_dictionary.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14 SQLITE_CANTOPEN): Could not open database
The message is saying
/data/data/com.benjaminkljn.econdictionary/assets/eng_dictionary.db
Your code above does not include assets in the path, so your issue is probably based around that.
Typically, and specifically, if using context.getDatabasePath(your_database), the attempt would be to open
/data/data/com.benjaminkljn.econdictionary/databases/eng_dictionary.db
i.e. the databases folder not the assets.
Additionally in your comments you mention :-
>I've deleted and re-imported database
Typically, importing the database, at least for an App, would be copying the database to be imported (included with the app) to the assets folder.
As such it could be that you are trying to open the database from what you think is the assets folder, but you cannot use the included/imported assets folder directly as the assets are managed by AssetsManager.
You could probably use Device Explorer to create the assets folder in /data/data/com.benjaminkljn.econdictionary and then copy the database into that folder and then the open may then work.
If you did that previously then, after deleting/uninstalling the App would, if the database hasn't been manually copied into the assets folder (and thus the assets folder has be created), result in the issue you have encountered.
When an App is installed the /data/data/com.benjaminkljn.econdictionary folder will be created but not the sub folders databases not the sub folder named assets, Clearing the App's storage effectively returns to this point.
However, manually copying the database, will only work for the specific App. It wouldn't work if you published the App, as you would not have access to copy the file manually.
To use a pre-existing database in an App then the recommended way is to :-
Create the App or part of the App
Create the assets folder in main/src/ (same location as where the res/java folders and android.manaifest are located).
Note if using SQLiteAssetHelper then create the databases folder in the assets folder.
Copy the database into the assets
The App should then before attempting to use the database, copy the database from the assets folder, accessing the database using AssetManager, into the location from which the database will be accessed.
Typically this location is /data/data/the_package_name/databases/your_database
The reliable/recommended way of determening the location is to use context.getDatabasePath(your_database).
Note that the databases folder will not exist for a new App and must be created (SQLiteAssethelper will do this).
SQliteAssethelper simplifies this.

Writing a file to a phone's internal storage with Xamarin [duplicate]

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");

Can't create an SQLite table on Android

I have been following this tutorial in its entirety, on how to store data in a local database. On iOS everything works fine, unfortunately on android i get an exception upon creating a table in my database file. The method for creating the table is:
public CalcDatabase(string dbPath)
{
database = new SQLiteAsyncConnection(dbPath);
database.CreateTableAsync<Calculation>().Wait();
}
And my custom class for getting the database path on Android is as follows:
public class LocalFileHelper : IFileHelper
{
public string GetLocalFilePath(string filename)
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
path = Path.Combine(path, filename);
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
}
This is the error that im getting from SQLite:
Could not open database file: /data/user/0/com.companyname.XX/files/Calculation.db3 (CannotOpen)
I have tried targeting several different APIs, I have tried to clean and rebuild my solution, and I have tried to reinstall the SQLite-net-pcl NuGet packages across the whole solution. Nothing works.
I have read, that it has to do with permissions, still, I do however find it strange that the official Microsoft tutorial doesn't cover this if it was a thing.
You need to check out the Android Permissions declaration (and grants) for Write and Read to external storage on your device or emulator.
It must be enough.
Make sure to delete bin and obj folders, clean and rebuild solution to get rid of any cached data on app compilation.

Creating SQLite database on Internal storage ANDROID

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

Can not get Android to create a folder in public external storage

I'm using Android Studio. I have my project set to require API version 11. The Emulator is set for Nexus 5 API23 (standard default settings).
I want to have my application write a simple text file to a location where I can pull the text files created onto my computer by plugging in with a USB cable. So it needs to be in the public external storage.
For whatever reason I can't get the code to create a folder for my text files to go into. I have paired my code down to this little nugget in a "Utilities" class I have:
public static boolean createTheDangFolder(Context c, String fileName, String body) {
boolean saved = true;
//File dir = new File(c.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "MyCustomFolder");
File dir = new File(Environment.getExternalStorageDirectory(), "MyCustomFolder");
if (!dir.exists()) {
saved = dir.mkdirs();
}
return saved;
}
This function always returns false. If I trade comments on the "File" line it will return true but the commented out line is the app memory and I can't access the files via USB.
I have this line in my Manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I can't for the life of me figure out why it's not working and none of the other questions on the site have given me a solution that works.
They changed the permissions to external storage in KitKat. You can only write to public folders (like downloads) and to your own app's private directory on external storage.

Categories

Resources