my application may have more then one users on the same phone, and i want each user to have its own
“ApplicationFriends” mysqlite table.
when the name of the table is only “ApplicationFriends” everything works great.
but when i’m trying to set the table name like this “ApplicationFriends2345” when 2345 is a uniqe user number, i always get an error when trying to run a select query, saying the table isn’t exist.
this is my table creation code:
public DatabaseManager(Context context, String id) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
DatabaseManager.TABLE_APPLICATION_FRIENDS = TABLE_APPLICATION_FRIENDS_BASE + id;
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_APPLICATIN_FRIENDS_TABLE = "CREATE TABLE IF NOT EXISTS "
+ TABLE_APPLICATION_FRIENDS + " (" + APPLICATION_FRIENDS_KEY_ID
+ " INTEGER PRIMARY KEY," + APPLICATION_FRIENDS_KEY_NAME
+ " TEXT" + ")";
String CREATE_CHAT_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_Chat
+ " (" + CHAT_KEY_SENDER_ID + " INTEGER," + CHAT_KEY_MESSAGE
+ " TEXT," + CHAT_KEY_TIME + " DateTime" + ")";
db.execSQL(CREATE_APPLICATIN_FRIENDS_TABLE);
db.execSQL(CREATE_CHAT_TABLE);
}
You can do it the way you were trying to (with a table for each user). You just have to move the table creation code into a method other than the
onCreatein your dbhelkper class, then call it as necessary (when you add a new user).Then wherever you need it:
This is what I would do as it would make removing a user much easier. You would just drop their table.