Title-Android-read database in raw folder..
My raw folder contains a database 'abc' & abc database contains one table say- xyz
i want to open 'abc' database for fetching data in it..
i want to use something like
SQLiteDatabase db=SQLiteDatabase.openDatabase(path, factory, flags);
1) how to give path to access database?
2) i want to open table & perform operation
Cursor c=db.query("xyz",null,null,null,null,null);
Raw has no path: It is a resource, so you need to reference is as such
using getResources().
You should, at the first run, copy it to the
/data/data/YOUR_APP_FOLDER/databases/YOUR_DB.db
Follow this tutorial.
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Copy your database file from /res/raw or /asset in /data/data/<application_package_name>/database/ directory and give path of that directory.
Now you can easily access your database file from /data/data//database/ directory..
Look at Ship an application with a database
Related
Using room in android for database. When I tried to see the data in sqlviewer then no tables found in database file
Myapp.db file is empty.
Data/data/packageName/databases/Myapp.db
Go to folder Data/data/packageName/databases/ There has to be three files .db, .db-shm, .db-wal, copy all three files to one location and then open your Myapp.db these two extra files are needed to open db file
in android studio 3.1.*
in tool window bar click on "Device File explorer" generally you can find this in bottom right corner of the screenn
open directory in data/data/your-application-package/databases
with new architecture 3 files is created in databases directory
your-database-name
your-database-name-shm
your-database-name-wal
you have to export all 3 in the same directory
then open first one file (that is with your-database-name only ) in any sqlite browser.
and now you can see all your data .......
your-database-name-shm
your-database-name-wal
these two extra files are needed to open db file
if you will open database file only, than you will not found any table in that file
Make sure your database is closed when you exit your app, which will ensure that all outstanding transactions are committed:
#Override
protected void onDestroy() {
super.onDestroy();
// close the primary database to ensure all the transactions are merged
MyAppDatabase.getInstance(getApplicationContext()).close();
}
Then you can copy the database off your device using the Device File Explorer in Android Studio (look under /data/data/<your app package name>/databases).
You are also able to force a WAL checkpoint instead of closing the database, but in my humble opinion this option is easier (unless you are trying to backup your database within the app programmatically or something like that) and closing databases is a good idea (even if it's not really mentioned in the Android Room documentation).
Read more about sqlite WAL here: https://www.sqlite.org/wal.html
I saved the whole folder and that's it, I could browse the table.
Once check your Database class,
#Database(entities = arrayOf(Test::class), version = 1, exportSchema = false)
abstract class MyDatabase:RoomDatabase() {
private var INSTANCE: MyDatabase? = null
abstract fun testDao(): TestDao
}
Here, MyDatabase is my Database class, Test is the table name and TestDao is the Dao class.
In my app I have to create a new SQL database each time a user does a specific action. Sometimes though I need to change the name of the db file but I don't see how I could do that. So instead I thought of creating a new database with the new name and transfer the old database's data into the new one.
So, to be more clear, I have database A with some data in it. I want to create database B with the same data as A. Any ideas how to do that?
See this question: instead of copy your database on SD Card, you can copy it in the same path of the original one and change the name of the new database.
Use the following path
path = "/data/data/<you package name>/databases/<your db>.db";
Get inputstream using above path, And create new file with <another name>.db on the same path.
then create outputstream using <another name>.db
and copy inputstream into outputstream.
then delete existing file... may solve your problem.
I have a question about SQLite. I'm trying to save a custum objects (let's say ObjectA) on my phone's db. ObjectA contains an ArrayList.
I'll probably have to save the images as BLOBs but how should I design my database. Should I create a new table with all my pictures (BLOB byte[], INTEGER objectA_row_id) ? or is there another way to make this association?
May be create table with path to the pictures.
You shouldn't store file path directly in database, because if you delete the file then you will not get image, so you have 2 options:
1) Store image to your cache/internal and then save its file path.
2) Use database table containing BLOB type.
Hi im trying to read sqlite file which is placed in assets/databases folder
i followed this link to read data from sqlite file
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
im getting error
no such table : while compiling
SELECT _id, name, address FROM
stores
Is there any permission i need to write in manifest to read sqlite file data?
Please let me know how i can solve this issue. Or else please give me any reference link to follow.
Thanks in advance
The way you'd want to do this is to make a new DBManager object that creates the database in its onCreate method--that way when the DatabaseHelper is first instantiated, the table will be created for you. Then you'd instantiate one and call getReadableDatabase() on it to get a DB object which you can then query.
This tutorial may help you more, it's more succinct and up to date: http://developer.android.com/guide/topics/data/data-storage.html#db
After that, to set up the list view in a ListActivity, you can call setListAdapter and pass in a SimpleCursorAdapter. Here's a tutorial on that: http://developer.android.com/guide/appendix/faq/commontasks.html#binding
Check if database already exists or not.
If you are running on emulator.
write following on terminal/shell:
adb shell
cd data/data/your package name(ex. com.android.etc)
ls
if there exists databases directory then may be database is created
cd databases
ls
it will show your database if exists;
sqlite3 "your db name"
then write
.tables
it will show the name of table if exist:
now write your query over here to check for errors for ex:
sqlite> SELECT _id, name, address FROM stores
hope it helps.....
and yes there are no as such required permissions for this.
So far we have developed apps in android that create database on runtime. We like to know how can we access a pre-built or existing database/sqlite file in our android app? Please provide detail
Take a look at the documentation for android.database.sqlite.SQLiteDatabase.
In particular, there's an openDatabase() command that will access a database given a file path.
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, 0);
Just specify the path to your database as the path variable, and it should work.
I have followed the same road and found two issues:
Include the _id field on any table you create and make sure it is
part of the columns to return when you query any table.
You may run into an error like Failed to open the database. closing
it.. This is because we are copying the database from an existing
file instead of creating tables on top of a db created by the sqlite
android API and the table android_metadata may not exist. For
creating it just run the following query on your db:
CREATE TABLE android_metadata (locale TEXT);
And add the corresponding locale, for example en_US
First copy your SDCARD and give it's path in the variable "DBNAME" in the following example.
it will be something like "/sdcard/persons.db" if you are directly pulling it to sdcard.
Use this for accessing database in application :
Context context = getApplicationContext();
context.getDatabasePath(DataBaseHelper.database_Name);