you can put the database file on assets or res/raw and copy it to
directories data/data/app_name/databases/
and open it (notice if the databases file more than 1 MB and you work
with an api <10 you shoud split it like i do in this example )
package com.androdev.ommek_sannefa;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class myDatabaseHelper extends SQLiteOpenHelper {
// DBPATH uses the default system path for a given application
// /data/data/<app namespace> , which in our example will be
// com.example.seinfeld
private static String DBPATH = "/data/data/com.androdev.ommek_sannefa/
databases/";
private static String DBNAME = "recette.db";
private SQLiteDatabase myDatabase;
private final Context myContext;
private static final String REQUETE_CREATION_BD = "create table "
+ " TABLE_RECETTES ( ID integer primary key, nom
text,complexity
integer,prep_time integer,cuisson integer,repos integer,nbr_personne
integer ,type_recette integer,ingredients text, preparation text );";
// constructor
public myDatabaseHelper(Context context) {
super(context, DBNAME, null, 1);
this.myContext = context;
}
// create an empty db, and replace with our chosen db
// public void createDatabase() throws IOException {
// if (!checkDatabase()) {
// this.getWritableDatabase();
// try {
// copyDatabase();
// } catch (IOException e) {
// throw new Error("Error copying database from system assets");
// }
// }
// }
// Check if our database already exists
// private boolean checkDatabase() {
// SQLiteDatabase checkableDatabase = null;
// try {
// checkableDatabase = SQLiteDatabase.openDatabase(DBPATH + DBNAME,
// null, SQLiteDatabase.OPEN_READONLY);
// } catch (SQLiteException e) {
//
// // our database doesn't exist, so we'll return false below.
// }
// if (checkableDatabase != null) {
// checkableDatabase.close();
// }
// return checkableDatabase != null ? true : false;
// }
// Copy our database from the Application's assets
// over the empty DB for use
private void copyDatabase() throws IOException {
if (new File(DBPATH).exists() == false)
new File(DBPATH).mkdir();
OutputStream myOutput = new FileOutputStream(DBPATH + DBNAME);
byte[] buffer = new byte[1028];
InputStream databaseInputStream = myContext.getResources()
.openRawResource(R.raw.recetteaa);
int length;
while ((length = databaseInputStream.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
myOutput.flush();
}
databaseInputStream.close();
databaseInputStream = myContext.getResources().openRawResource(
R.raw.recetteab);
while ((length = databaseInputStream.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
myOutput.flush();
}
databaseInputStream.close();
myOutput.close();
}
public SQLiteDatabase openDatabase() {
if (new File(DBPATH + DBNAME).exists() == false
) {
try {
copyDatabase();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("erreur", "copying databases !!");
Log.d("bd",
"taille de db :" + new
File(DBPATH + DBNAME).length());
e.printStackTrace();
}
}
myDatabase = SQLiteDatabase.openDatabase(DBPATH + DBNAME, null,
SQLiteDatabase.OPEN_READONLY);
return myDatabase;
}
@Override
public synchronized void close() {
if (myDatabase != null)
myDatabase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(REQUETE_CREATION_BD);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
// Handle upgrade tasks, etc.
}
} // end of myDatabaseHelper.java
On 29 sep, 06:07, Devendran Raju <[email protected]> wrote:
> ya. I got... Thank u......
>
>
>
>
>
>
>
> On Thu, Sep 29, 2011 at 6:58 AM, lbendlin <[email protected]> wrote:
> > upon first use you need to copy the database from the package to the data
> > folder.
>
> >http://www.remyvd.com/node/39
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en