I need a local database for my application.I created one and i encrypted it.(I know the password).Now i want to load this db to my original application.I want to decrypte 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[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
Looking on the wiki on DB Browser for SQLite repo, DB Browser uses SQLCipher as a default encryption extension, so I assume that you have used the default SQLCipher extension. This means that to use that encrypted DB file on Android, you need to use SQLCipher's Android Port.
More examples and documentation can be found in their Android Repo and here.
Update:
This update explains a sample setup for using SQLiteCipher.
Add SQLCipher dependency in your application's build.gradle file:
compile net.zetetic:android-database-sqlcipher:3.5.7#aar
Initialize SQLCipher's native library before performing any db ops by calling:
net.sqlcipher.database.SQLiteDatabase.loadLibs(context)
(optional, recommended) Create an implementation of:
net.sqlcipher.database.SQLiteOpenHelper or, better yet, make your existing SQLiteOpenHelper extend from this class
Then whenever you request a database (getReadableDatabase() or getWriteableDatabase()), supply your encryption password with it, e.g.,
SQLiteDatabase db = helper.getReadableDatabase(_PASSWORD_)
See more details on this gist.
Also, in your case you need to copy the encrypted database from the raw assets folder to the path returned by calling Context.getDatabasePath() for the first time your app is run.
Hope this explains the things to you,
Regards
Related
I create a table in sqlite database I am doing all the CRUD operation but I want to see my database schema . I try through DDMS tool of eclipse IDE but there is no database. How can I see my database.i am using real device. Please Help me.
Thanks in advance
You can use this method:
STEP 1: First export the database by writing function :
public static void backupDatabase() throws IOException {
//Open your local db as the input stream
String inFileName = "/data/data/com.myapp.main/databases/MYDB";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String outFileName = Environment.getExternalStorageDirectory()+"/MYDB";
//Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
//Close the streams
output.flush();
output.close();
fis.close();
}
Taken from Stack overflow link
Then download DB Browser for SQLite and open database file there.DB Browser for SQLite
And you are good to go.Inform me if you found any difficulties while implementing this.
In a real device you cannot access the SQLite DB unless ur phone is rooted.. in a virtual device however you can access it. but there is no way u can see the actual schema. all you see is some text files and once you open them up your columns(data fields) are separated by tabs or something. so
How can I see my database? YOU CAN'T
Only rooted real device can be access using some jar in eclipse. Other wise you can seed database structure in emulator from DDMS Respective. To know how to do this.
Please red this to solve your problem.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android backup/restore: how to backup an internal database?
I am developing an app in which there is SQL lite db. I need to give the user the ability to back up the database and restore it again. How can I do that? Sample code?
And in case my user saved the db, and then upgraded the app to a new DB version code, will restore work? How do I go about that?
In the folder "/data/data/'your.app.package'/databases/" you have a .db file that is your database. You can copy that file, save it, and then place it back there again.
One example on how to backup the database to the external storage:
final String inFileName = "/data/data/<your.app.package>/databases/foo.db";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String outFileName = Environment.getExternalStorageDirectory()+"/database_copy.db";
// Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
// Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
Can you clarify what you mean by "back up"? SQLite doesn't have a specific backup method; you have to read rows from the db and save them somehow. Many developers use XML to do this.
If the user saves the db and you then move to a new DB version code, you have to decide how you'll do the restore. Again, you have to do the work to read the backup and put it back into the database; neither SQLite nor Android does this for you.
The package android.app.backup contains classes for working with the Android Backup Agent, but they implement a general framework. It's up to you to implement the specifics for each file or database you're backing up.
I am developing my android application using Netbeans and java. When I am using the emulator I can access the File explorer and insert an SQLite database in to device internal memory by accessing the following path, data/data/com.example.helloandroid/database
But I can not access this location to push the SQLite File in to the phone's internal storage (location) when I am using the real device.
Can someone please help me how to add the file in to phones internal storage. Thanks
I think the device doesn't have root permission, that's why you can't access it. If you want to do in your app with programmatically then it is possible. If anybody knows better then this please share it.
EDIT: ok, first of all,
1. copy your Database.db file in your projects assets folder.
2. now using code copy database file from /asset to device's internal storage
(data/data/<package name>/database folder).
For copy file use below code,
try {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open("your database file name");
// Path to the just created empty db
String outFileName = "/data/data/<your_app_package_name>/databases/<database_file_name>";
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();
}
catch (Exception e)
{
Log.e("error", e.toString());
}
All i need to bind .db and .apk at run time in single .apk so user can download and run the application. i don't neeed to . db in assets ,raw ,URL path because we need to Change the .db at run time and we Have so many user and for different user we have different .db. it is ERP based Application. thanks in advance
You put the .db into your /raw/ directory.
Then at runtime you copy this into a new database that is located in your applications memory, which is then modifiable.
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
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();
}
The code is not mine and is an exert from the full tutorial at: Using your own DB in Android
If your looking to put different database files in your /raw/ you might want to use multiple APK's now that the market allows it or make an ant script for selecting a different DB when the APK is built.
I guess many people already read this article:
Using your own SQLite database in Android applications: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-2/#comment-12368
However it's keep bringing IOException at
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
I’am trying to use a large DB file. It’s as big as >8MB
I built it using sqlite3 in Mac OS X, inserted UTF-8 encoded strings (for I am using Korean),
added android_meta table with ko_KR as locale, as instructed above.
However, When I debug, it keeps showing IOException at
length=myInput.read(buffer)
I suspect it’s caused by trying to read a big file. If not, I have no clue why.
I tested the same code using much smaller text file, and it worked fine.
Can anyone help me out on this? I’ve searched many places, but no place gave me the clear answer, or good solution.
Good meaning efficient or easy.
I will try use BufferedInput(Output)Stream, but if the simpler one cannot work, I don’t think this will work either.
Can anyone explain the fundamental limits in file input/output in Android, and the right way around it, possibly?
I will really appreciate anyone’s considerate answer. Thank you.
WITH MORE DETAIL:
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();
}
Can anyone help me out on this?
Use a smaller file. Or a set of smaller files that you stitch together into a large file as you are unpacking them. Or download the database on the first run of your application.
I've been there. I believe the RAW resource type is free of maximum size constraints (whereas Assets, or more specifically, the stream connected to an asset, is limited in size).
Once you get past that hurdle you should be fine - I've had databases on my phone well over 100MB.
Don't know if it would help you, but if other approaches fail you could try using NIO. For example, here's an implementation of copying inputstream to fileoutput. Alternatively, you could try zipping your file and see if the Android unzipping tools can unzip directly to your db folder.