I am doing an app in android, I have .sqlite file in my system which is called 'ff.sqlite'. In the .sqlite file I have 10 tables, very large no of records for tables. In my app(android) I place the .sqlite file in raw folder and I created a database called 'festival.db'. Now I do not know how to import the ff.sqlite file into the festival.db. please help me.
res/raw/ff.sqlite
to create database I use the following code in android:
SQLiteDatabase dbs;
dbs = openOrCreateDatabase("festival.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
My problem is I need all tables in the festival.db from the .sqlite file please help me.
Copy your DB file from assets folder to internal memory, then open DB and do CRUD from there:
private void copyDB() throws IOException{
InputStream myInput = m_Context.getAssets().open(DB-SQLITE_FILE_NAME_KEPT_IN_ASSET_FOLDER);
String outFileName = "/data/data/your.package.name/databases/";
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
Related
in my project i have imported a sqlite database using this..
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
It Works fine. But When I delete the database file from the DDMS File Explorer and try to import it again, the code breaks down.
it says no such file found and create a empty database...
Is there any way to overcome this problem?
You Have to delete the previous database before import it again.
use this Context.deleteDatabase("DB_NAME")
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();
}
I think that this is rather easy question. I am too young in android stuff already. I want to prepare application which will be using database. In every example I've shown, there is an empty database where application is firstly started and after that there are some inserts. I want to have app with rather big db so I want to have filled db when app is started. How can I prepare db and attach it to program?
put your filled database in Package's Asset directory,
at application runtime just copy that database to application's internal storage like
data/data/<package name>/database directory.
then use it.
EDIT: this for copy database from asset directory to database directory,
private void copyDataBase() throws IOException {
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/<package name>/databases/";
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());
}
}
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!
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?