Android prefilled DB - android

Is it possible to supply prefilled SQLite DB to my app? I want to use db as ordinary SQLite db which will have tables filled manually and I want to include it into my .apk file.

Yes, include it in your assets folder and copy it into the /databases folder when your application first launches.

try this
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}

Including in the assets folder is one option (Apk size will be increased.)
How about storing the pre-filled db on any cloud storage service provider and downloading the file on the first run of the app?

Related

how to decrypt sqlite database android using sqlcipher?

I need a local database for my application.I created one and i encrypted it.(I know the pw).Now i want to load this db to my original application's assets folder.I want to decrypt it before copy.
I had a copy code like this this is working for unencrpyted db.
How can I translate this for my encrypted db.Thanks
private void copyDataBase() throws IOException {
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[5120];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
The proper way to do this would be to copy the encrypted database into a local folder. Then you can open it with SQLCipher and use sqlcipher_export() to convert it into a standard (non-encrypted) SQLite database.

android database copy error through programming

I have a strange problem I am trying to copy database from asset folder to app database folder "/data/data/YOUR_PACKAGE/databases/" by the below code. this is working fine for Samsung mobile not working for gionee mobile I don't know why please help
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
How do you generate the DB_PATH? Maybe it is not available on the gionee mobile device (because the manufacturer modded the OS).
You should use the code below to determine outFileName:
String outFileName = context.getDatabasePath(DB_NAME).getPath();
If that still throws an exception, please update your post with more details.

How to put .db file from assets to data/data/packagename/ android [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to use an existing database with an Android application
how to put .db file from assets to data/data/packagename/ without using copying content of .db file in assets. I don't want to create database because its useless to put the .db file in assets. I explore on it but all are again creating databse but i only want to put that .db file directly.
use this
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = "/data/data/"
+getApplicationContext().getPackageName()
+ "/databases/" + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}

Copy two SQLite Database in asset folder in Android

For the last few days I've been trying to copy two databases in the asset folder, but could not get any success.
One database I managed to copy and access it. But the second one, I need your help.
private void copydatabase() throws IOException {
//Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DB_NAME);// Path to the just created empty db
String outfilename = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream("/data/data/(packagename)/databases /(datbasename).sqlite");
// transfer byte to inputfile to outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer))>0)
{
myoutput.write(buffer,0,length);
}
//Close the streams
myoutput.flush();
myoutput.close();
myinput.close();
}
Do this for your second database.

problem in copying data from assets to application

For my application i need to read data from my own data base. I put my db in "ASSETS" folder and using following code to copy into the path
DB_PATH = "/data/data/com.android.example/databases/"
But the table is not getting created.And its throwing exception and forced close.
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
In above code "buffer" is not getting data . so its not writing data to myOutput. I found this code in net and modified . could any body help me to read the file in assets folder to SD card.I need to store data in SD card from external file and read from it.
Check you database file size, there is a limitation around 1.2 Mb for files in the "assets" folder.
One way around it is to use the Unix "split" command, add the split files in the res/raw folder, and then splice them back together into the db by altering the code you have slightly.
Read this blog (http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/) for more information.
You must put data not in ASSETS folder but into assets - please double check!

Categories

Resources