-5

I’m basically developing a basic android-sqlite-database app. I have successfully run it. It gets “name” and “Phone number” and adds to the database at “data/data/com.android.databaseapp” Now I want to put the database into the app, I have followed the tutorials but not succeeded. Please give me some detailed directions here I have added the .db file in assets and followed 2 toturials for this but assets/.db file does not gets update. only “data/data/com.android.databaseapp/.db” file gets update My DatabaseHadler class is:

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "contactsManager";

private static final String TABLE_CONTACTS = "contacts";

private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
    db.close();
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
    db.close();
}

Plzzz guide me here.....

Husnain Iqbal
  • 83
  • 1
  • 3
  • 5

2 Answers2

2

Here is the best tutorial you can find for the database creation and execution in android

You can also download the example from this link and can execute it.

(If you wanna execute the database by keeping it in a asset folder just follow this link)

Kartheek Sarabu
  • 3,886
  • 8
  • 33
  • 66
1

If you put the database into the assets folder you need to specify the new path for it in your database connector class.

Create a class which will handle the connection to your database and use the openDatabase method from the SQLiteDatabase class:

if(File.exists(databasePath))
{
    this.yourDataBase = 
        SQLiteDatabase.openDatabase( databasePath , 
                                     null, 
                                     SQLiteDatabase.OPEN_READWRITE);
}

Make sure databasePath points the your ...app/assets/database.db3 file

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
Marius M
  • 496
  • 9
  • 16