Sqlite database import export - android

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" />

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?

Write access for SQLite database on sdcard

I have a SQLite database on my sdcard (/storage/sdcard/MyDir/mydb.sqlite) and try to write some data in it. Due to the Storage Access Framework I can not access the file directly and I need to get write access like it is described here. This grants me write access via a DocumentFile.
However, the database needs to accessed via JDBC. JDBC requires a String to the database file to establish a database connection. This leads to the following problems:
If I use the Java-Path I can access the database, but I have no writing permission. Even if the access was granted like described above.
If I use the Uri of the DocumentFile (having writing permission) JDBC does not find the database file.
I can place my database in the private directory of the application on the sdcard (/storage/sdcard/Android/data/my.application.package/) where I have writing permission. However, I would like to store the database outside that directory, since the database should be accessed by multiple applications.
I tried this suggestion, which works on the device storage, however does not work on the sdcard. On the sdcard I get this stacktrace:
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 1294 SQLITE_CANTOPEN_ENOENT[1294]): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:284)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:705)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:272)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:239)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:1292)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:1247)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:930)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:947)
When I access the database on the device storage (internal storage and external storage) I can write the database. Only if I access the database on the sdcard I have no writing permission.
Is there any possibility to get write access to specific files on the sdcard based on java.io.File like it is possible for DocumentFile?
Does anybody have a different idea how I can access the data on the sdcard with writing permission?
I have a SQLite database on my sdcard (/storage/sdcard/MyDir/mydb.sqlite)
Using removable storage as a transfer location for a SQLite database is OK. For example, using removable storage for backup and restore operations is fine.
However, for a live database, removable storage is very risky. On many devices, removable storage is removable, and you will suffer catastrophic problems if the user removes the storage while you are trying to write to your database.
And, as you are discovering, using removable storage for a live data is impractical.
the database needs to accessed via JDBC
Android has a built-in SQLite database API. Attempting to use Android's half-baked JDBC classes for accessing a SQLite database is very strange.
When I access the database on the device storage (internal storage and external storage) I can write the database
You will encounter problems on Android 10 and higher, as you no longer have direct filesystem access to most of external storage.
Is there any possibility to get write access to specific files on the sdcard based on java.io.File like it is possible for DocumentFile?
No, sorry. You do not have filesystem access to arbitrary locations on removable storage as of Android 4.4. Android 10 extends that to arbitrary locations on external storage. Either:
Limit yourself to the directories that you can access, or
Use removable storage only for backup/restore operations, with the live database in a more conventional location (getDatabasePath() on Context)

Is it safe to perform application database backup, by copy its SQLite file directly?

Currently, our app is using Room SQLite.
We need to let user to create application data backup, and export them as a single zip file.
Direct File Copy
I was wondering, is it safe, to perform direct File copy on application SQLite file to a temporary folder, for further zipping purpose? The reason I ask so is, I notice that sometimes the application database instead of appearing as single file like local-backup, it will have 2 additional files named local-backup-shm and local-backup-wal.
Read and Write to a temporary SQLite DB
Or, I should just create a temporary empty database, use Room to read application data and write to the temporary database? Then, zipping will be performed on the temporary database?
Copying the file is sufficient. That will copy the entire db.

Save information in local Storage

I'm using realm(also tried sharepreference) to save some information.But When I cleared app's data,All information has removed.
I have one question.How I can save some information to my device,witch always 'll be in storage? (if i cleared data or uninstalled my app)
Yes, clearing local data delete shared preferences and data in the local SQL Lite database as well. However you can make a folder for your app in local SD card or in phone's internal storage. These folders will not be deleted even if your app uninstalled. In these folders you can store your data in some sort of a format, it maybe in plain text or it maybe some encrypted files. It's your call.. hope this helps

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

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

Categories

Resources