Tags
python
Asked 2 years ago
6 Feb 2022
Views 284
jagdish

jagdish posted

create table only in first time install app android

I am developing the app, and it should create a table in SQLite when the first time installs the app. after that it should not create the table again and again when the app starts so only create a table only for install time. is that possible with andorid by code.
Phpworker

Phpworker
answered Feb 6 '22 00:00

try to extend the SQLiteOpenHelper class
and try to override the onCreate and place the query in the onCreate method

Java version

public class DatabaseHelper extends SQLiteOpenHelper {
 ....
public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        SQLiteDatabase db = this.getWritableDatabase();
}

@Override
    public void onCreate(SQLiteDatabase db) {
        // creating required tables
        db.execSQL(create table query goes here);

     }


 @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // on upgrade drop older tables
        db.execSQL("DROP TABLE IF EXISTS tablename" ); 
        // create new tables
        onCreate(db);
    }
...
...
 
}


SQLiteOpenHelper helper class to manage database creation and version management.
You create a subclass implementing
onCreate , onUpgrade and
optionally onOpen , and this class takes care of opening the database
if it exists, creating it if it does not, and upgrading it as necessary.

onCreate
Called when the database is created for the first time.
onUpgrade
Called when the database needs to be upgraded. this method should be used to drop tables, add tables
Post Answer