我有一个目前正常的应用程序。它基本上是一个练习应用程序,其中所有用户的练习条目都保存在表格中。问题是,此应用程序目前只适用于一个用户,我想让它适用于多个用户,但为此,我需要为每个用户提供一个单独的练习数据库表。我怎么能这样做?
我不知道它是否相关,但以下是我当前数据库代码的重要部分:
private static final String USER_TABLE_CREATE =
"create table if not exists UserProfile ( id integer primary key autoincrement,"
+ "age VARCHAR not null, weight VARCHAR not null, height VARCHAR not null, sex VARCHAR not null);";
private static final String RUNNER_TABLE_CREATE =
"create table if not exists RunnerHistoryTable ( id integer primary key autoincrement,"
+ "recommendation VARCHAR , activitydate VARCHAR , activity_distance VARCHAR , activity_time VARCHAR, activity_velocity VARCHAR, monitor VARCHAR, calories VARCHAR);";
private static final String WALKER_TABLE_CREATE =
"create table if not exists WalkerHistoryTable ( id integer primary key autoincrement,"
+ "recommendation VARCHAR , activitydate VARCHAR , activity_distance VARCHAR , activity_time VARCHAR, activity_velocity VARCHAR, monitor VARCHAR, calories VARCHAR);";
private static final String WEIGHT_LOSS_TABLE_CREATE =
"create table if not exists WeightLossHistoryTable (id integer primary key autoincrement, "
+ " recommendation VARCHAR , activitydate VARCHAR, activity_distance VARCHAR , activity_time VARCHAR, activity_velocity VARCHAR, monitor VARCHAR, calories VARCHAR);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DatabaseAdapter(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)
{
try {
db.execSQL(USER_TABLE_CREATE);
db.execSQL(RUNNER_TABLE_CREATE);
db.execSQL(WALKER_TABLE_CREATE);
db.execSQL(WEIGHT_LOSS_TABLE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//---insert a record into the database---
public long insertUser(String age, String weight, String height,String sex){
ContentValues initialValues = new ContentValues();
initialValues.put(AGE,age);
initialValues.put(WEIGHT,weight);
initialValues.put(HEIGHT,height);
initialValues.put(SEX, sex);
return db.insert(USER_PROFILE_TABLE, null, initialValues);
}
public long insertActivity(String tableName, String recommendation, String activityDate, String activityDistance, String activityTime, String activityVelocity, String monitor, String calories){
ContentValues initialValues = new ContentValues();
initialValues.put(RECOMMENDATION,recommendation);
initialValues.put(ACTIVITY_DATE,activityDate);
initialValues.put(ACTIVITY_DISTANCE,activityDistance);
initialValues.put(ACTIVITY_TIME, activityTime);
initialValues.put(ACTIVITY_VELOCITY, activityVelocity);
initialValues.put(MONITOR, monitor);
initialValues.put(CALORIES, calories);
return db.insert(tableName, null, initialValues);
}
答案 0 :(得分:1)
您的数据库应该只有两个表,一个用于存储用户,另一个用于存储活动(将activitytype列添加到“活动”表)。然后,您需要使用外键约束将用户绑定到活动。
private static final String USER_TABLE_CREATE =
"create table if not exists Users ( id integer primary key autoincrement,"
+ "age text not null, weight text not null, height text not null, sex text not null);";
private static final String ACTIVITIES_TABLE_CREATE =
"create table if not exists Activities ( id integer primary key autoincrement,"
+ "userid int, activitytype text, recommendation text, activitydate text, activity_distance text,"
+ "activity_time text, activity_velocity text, monitor text, calories text,"
+ "FOREIGN KEY(userid) REFERENCES Users(id));";