Confused regarding SQLiteOpenHelper and creating multiple tables

I believe that the recommendations of Android developers regarding data storage in SQLite are really absent. I am confused as to what the general rule is when it comes to multiple tables.

Currently, I have two Managers who open (CRUD) two different sets of objects in the user interface.

I have:

Create a private SQLiteOpenHelper class inside each Manager. Will each helper have their own onCreate for their own TABLE?

or

Create a single public class SQLiteOpenHelper that creates as TABLE?

I do not see any obvious advantages in using one over the other, but I see that both of them are used. What does Android say about this?

+7
source share
1 answer

The following code example creates two tables. I also added code to create and insert data.

import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class MyDB { public static final String KEY_ROWID = "_id"; public static final String KEY_FIRSTNAME = "ID"; public static final String KEY_LASTNAME = "CS"; public static final String KEY_DESIGNATION = "CN"; public static final String KEY_DN = "DN"; private static final String TAG = "MyDB"; private static final String DATABASE_NAME = "test.db"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_CREATE_ValidateUser_DriverInfo = "create table tabletest1 (_id integer primary key autoincrement, " + "ID text not null, CS text not null, CN text not null, DN text not null);"; private static final String DATABASE_CREATE_ValidateUser_TripInfo = "create table tabletest2 (_id integer primary key autoincrement, " + "TI text not null, PU text not null, LN text not null, FN text not null, Origin varchar not null, De text not null);"; private Context context; private DatabaseHelper DBHelper; private SQLiteDatabase db; public MyDB(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE_ValidateUser_DriverInfo); db.execSQL(DATABASE_CREATE_ValidateUser_TripInfo); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS tabletest1"); db.execSQL("DROP TABLE IF EXISTS tabletest2"); onCreate(db); } } public MyDB open() throws SQLException { db = DBHelper.getWritableDatabase(); return this; } //---closes the database--- public void close() { DBHelper.close(); } public long insertTitle(ContentValues initialValues, String TableName) { return db.insert(TableName, null, initialValues); } } 

Use the following code to insert data from the required activity.

 MyDB mmdb=new MyDB(getBaseContext()); mmdb.open(); initialValues = new ContentValues(); initialValues.put("ID", ID); initialValues.put("CS", CS); initialValues.put("CN", CN); initialValues.put("DN", DN); mmdb.insertTitle(initialValues, "tabletest1"); mmdb.close(); 
+8
source

All Articles