Android - Show path to my sqlite database file in a Label - android

I am reading about Android and about how database is created and stored in application internal storage or sandbox.
Assuming I have created a database named "people.db" by calling SQLiteHelper.execSQL(peopleDB), how do I get this database file path and show it in a TextView?
To clarify, I am not asking how to create a database but how to get the path to my database file "people.db"?
String peopleDB = "people.db";
All I am trying to do is show in a Label where that application storage (or sandbox) is and to show my database location on device. This is just for personal learning, so I am not concerned about showing this information to the user or anything like that.

File dir = getDatabasePath("people.db");
textView1.setText(dir.getAbsolutePath());
File explorer apps have no acces as its your app' private internal storage.

You can find your created database, named
in
//data/data/<Your-Application-Package-Name>/databases/<your-database-name>
Use File explorer of DDMS to navigate to emulator directory.

You can get path by context.getDatabasePath(DataBaseHelper.dbName)
It is already answered here Get database path

Related

Is there any way to use a SQLite DB (r/w) with the Storage-Access-Framework?

I want the user to be able to access/copy a database file (along with more files) from a selected folder from their device.
So I ask the user to please select a folder on the device where I store files so that the user can access these. But SQLite Database needs direct file system access, so in other words, on android 11+. is it impossible to use a database file that the user can access?

On an Android Device where is located SQLite database, created by Room library

Where is located an app SQLite database created by Room library on a device?
Context:
I have an app which create and use a Room database. To debug the database I would like to open it in a SQLite viewer app but I don't know where the database is located on a testing device.
Whether you use Room, OrmLite or SQLite in Android all are located within databases folder of application package.
You can access the databases folder by following below steps.
View > Tool Windows > Device File Explorer
From Device File Explorer go to data folder in which all the application packages are stored. Make sure that Emulator or device is connected.
data > data
Then, find you application package and go to your database.
com.company.my > databases > yourdatabasename.db
You can then save your database in your computer and do anything you want with it.
I use SQLiteBrowser to browse the database.
Go to Tools -> DDMS or click the Device File Explorer below Layout Preview in right bar.
Device Monitor window will open.
In File Explorer tab, click data -> data -> your project name -> databases.
Click pull a file from device icon. Save the file using .db extension.
I suggest please use Stetho library provided by the Facebook, so you don't need to pull the database every time. You can check the database in browser.
http://facebook.github.io/stetho/
You can also debug your database on desktop browser with the help of this awesome library Android-Debug-Database. Note that your mobile device on which you are running the app and desktop/laptop must be connected to same network.
you can use :
String path = getDatabasePath("you_database_name").getAbsolutePath();
Other than #musooff answer, if you want to save the database to local storage or want to give a custom path to your database. Then you can use the following approach:
val DATABASE_NAME = Environment.getExternalStorageDirectory().toString() + File.separator + "MyDBFolder" + File.separator + "example.db"
Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME).build()
This will create a folder MyDBFolder containing example.db database in your local storage. From there you can easily access the database.
Related to [SQLiteBrowser]
An update to #musooff using SQLiteBrowser to browse the database.
SQLiteBrowser no longer works for Room database Inspection.
You can now use the official Database Inspector for Android Studio.
Select View > Tool Windows > App Inspection from the menu bar.

Sqlite database import export

Can I Import and export the SQLite database? means that I just want to carry the database. .DB file will be put on my sd card. The app will fetch data from a database at sd card and write the data on that database. After using the app. close the app and take a .db file to other devices and able to see the previous data and can manipulate that database.
You can get database file in SD-Card with use of ORM. But not using default SQLiteOPenHelper.
Check Litepal Framework: For more info check this https://github.com/LitePalFramework/LitePal
Define where the .db file should be. "internal" means the .db file
will be stored in the database folder of internal storage which no
one can access. "external" means the .db file will be stored in the
path to the directory on the primary external storage device where
the application can place persistent files it owns which everyone
can access. "internal" will act as default.
For example:
<storage value="external" />

Using Zumero to sync android application, change location of cache file

I am using Zumero to sync to a MS SQL server with my Android application, I have my SQLite db stored on the SD Card. When I run the sync it creates a cache file in the internal storage before updating the SQLite db. This is causing a internal storage issue and the operating system is deleting the cache file before updating the database due to running out of storage space. How can I force the app to use the SD Card for the cache file?
I have it working by replacing
context.getCacheDir().getAbsolutePath()
with
context.getExternalCacheDir().getAbsolutePath()
You'll need to use the Zumero Java files directly as part of your application (look for ZumeroClient.java, etc. in the SDK), and edit the temp path being used. Compiles these into your app instead of using zumero.jar (you'll still need the appropriate .so libraries).
Out-of-the-box, Zumero uses the cache path provided by the Android context. You'll want to edit calls to native_sync(), and replace the last parameter with a path on the SD card.
e.g.
native_sync(databasePath, encryptionKey, serverURL, dbFile, authScheme,
username, password, context.getCacheDir().getAbsolutePath());
would become
native_sync(databasePath, encryptionKey, serverURL, dbFile, authScheme,
username, password, myCacheDir);

Logger inside android based on database

I am working on logs inside android, I thought two ways for storing logs, and one is on external directory as a text file or a log file while other is to store in database. I found database method more useful in my case. My question is if I UN-install and reinstall the app will the database will be affected? In case of yes what should I do? I cannot place the logs online. How to take the backup or safe that database so it won’t be affected in case of UN- installation.
My question is if I uninstall and reinstall the app will the database will be affected?
Yes, your database will by default be stored in the application's data directory, which is deleted along with your application on uninstallation.
You can instead write a file of a filetype of your choosing (whether that's a simple text file, or a database file) to the external storage. You can obtain the directory path using:
Environment.getExternalStoragePublicDirectory(type) for obtaining a directory path of files of a specific type, such as images or videos;
Environment.getExternalStorageDirectory() for obtaining the primary external storage directory, under which you could create a new path.
I would nevertheless discourage you from doing this, because it would require your users to manually dispose of any files after uninstalling your application. Perhaps you should reconsider the justification of choosing for this solution.
If you uninstall the app, then the database (and all other data stored in the apps private storage for that matter) will be gone.
You could store your logs in the public external storage, but this will expose your logs to other applications as well as the user.
One possible approach could be to use application private storage for your 'live' logs, and make periodic backups to the public storage. In case of a new installation, you can check your designated backup location and attempt to restore previous logs from the backup.

Categories

Resources