1 つのデータベースに複数のデータベース テーブルがあります。ユーザーはデータベースに挿入するデータを入力します。スピナー値の 1 つは、データを挿入するテーブルを指定することです。
挿入は、1 つのテーブルに対して正常に機能します。ただし、if ステートメントを挿入してデータを正しいテーブルに配置するとすぐに、どこにも何も挿入されません。logcat に表示されるエラーは「no such table: DATABASE_TABLE」です。これは意味がありません。そこに if ステートメントがなければ挿入に問題がないからです。
これが私のコードです:
**完全なコードを含めるように更新
int id = 0;
//setting up columns
public static final String KEY_ID = "_id";
public static final String KEY_EXERCISE = "abs";
public static final String KEY_REPS = "reps";
public static final String KEY_TIMETYPE = "timetype";
//setting up the database
private static final String DATABASE_NAME = "ExerciseDB";
private static final String DATABASE_TABLE = "ExerciseTable";
private static final String DATABASE_BICEPSTABLE = "BicepsTable";
private static final int DATABASE_VERSION = 2;
//setting up tables
private static final String DATABASE_TABLE_ABS = "CREATE TABLE " +
DATABASE_TABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_REPS + " TEXT NOT NULL, " +
KEY_TIMETYPE + " TEXT NOT NULL, " +
KEY_EXERCISE + " TEXT NOT NULL);";
private static final String DATABASE_TABLE_BICEPS = "CREATE TABLE " +
DATABASE_BICEPSTABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_REPS + " TEXT NOT NULL, " +
KEY_TIMETYPE + " TEXT NOT NULL, " +
KEY_EXERCISE + " TEXT NOT NULL);";
private static String CHOSEN_TABLE = "";
private DbHelper ourHelper;
private static Context ourContext;
private SQLiteDatabase ourDatabase;
public static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// Only uses this method when the database is first created.
//Sets up the database - telling it the table name and all
//the column names.
db.execSQL(DATABASE_TABLE_ABS);
//syntax used to insert rows into a database on creation
db.execSQL("INSERT INTO " + DATABASE_TABLE + "(" + KEY_EXERCISE +", " + KEY_REPS +", " + KEY_TIMETYPE + ") VALUES ('Reverse Crunches', '25', 'Reps' );" );
//setting up Biceps Table
db.execSQL(DATABASE_TABLE_BICEPS);
//syntax used to insert rows into a database on creation
db.execSQL("INSERT INTO " + DATABASE_BICEPSTABLE + "(" + KEY_EXERCISE +", " + KEY_REPS +", " + KEY_TIMETYPE + ") VALUES ('Hammer Curls', '15', 'Reps' );" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// called when database has already been created.
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_BICEPSTABLE);
onCreate(db);
}
}
public ExerciseDatabase(Context c){
ourContext = c;
}
//to open the database
public ExerciseDatabase open()throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
//to close the database
public void close(){
ourHelper.close();
}
//to retrieve all the data from the database
public String getData() {
String[] columns = new String[]{KEY_REPS, KEY_EXERCISE, KEY_TIMETYPE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iReps = c.getColumnIndex(KEY_REPS);
int iExercise = c.getColumnIndex(KEY_EXERCISE);
int iTimeType = c.getColumnIndex(KEY_TIMETYPE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iExercise) + " "
+ c.getString(iReps) + " "
+ c.getString(iTimeType) + "\n";
}
return result;
}
public String getBicepsData() {
String[] columns = new String[]{KEY_REPS, KEY_EXERCISE, KEY_TIMETYPE};
Cursor c = ourDatabase.query(DATABASE_BICEPSTABLE, columns, null, null, null, null, null);
String result = "";
int iReps = c.getColumnIndex(KEY_REPS);
int iExercise = c.getColumnIndex(KEY_EXERCISE);
int iTimeType = c.getColumnIndex(KEY_TIMETYPE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iExercise) + " "
+ c.getString(iReps) + " "
+ c.getString(iTimeType) + "\n";
}
return result;
}
//to count the number of entries in the database
public int EntryCount(){
return (int) DatabaseUtils.queryNumEntries(ourDatabase,DATABASE_TABLE);
}
//adds an entry to the database that the user inputs into the text fields
public long addExercise(String exerciseadd, String repTimeAdd, String timetype, String exercisetype) {
if (exercisetype == "abs"){
CHOSEN_TABLE = "DATABASE_TABLE";
}else if (exercisetype == "biceps"){
CHOSEN_TABLE = "DATABASE_BICEPSTABLE";
}
ContentValues cv = new ContentValues();
cv.put(KEY_EXERCISE, exerciseadd);
cv.put(KEY_REPS, repTimeAdd);
cv.put(KEY_TIMETYPE, timetype);
return ourDatabase.insert(CHOSEN_TABLE, null, cv);
}
//gets an entry from the Reps column by chosen row number (randomly generated in main activity)
public String getReps(int chosenRow) {
// TODO Auto-generated method stub
if (chosenRow == 0){
++chosenRow;
}
String[] columns = new String[]{KEY_REPS, KEY_EXERCISE, KEY_TIMETYPE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ID + "=" + chosenRow, null, null, null, null);
if(c != null){
c.moveToFirst();
String exercise = c.getString(0);
return exercise;
}
return null;
}
//gets an entry from the Abs column by chosen row number (randomly generated in main activity)
public String getExercise(int chosenRow) {
// TODO Auto-generated method stub
if (chosenRow == 0){
++chosenRow;
}
String[] columns = new String[]{KEY_REPS, KEY_EXERCISE, KEY_TIMETYPE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ID + "=" + chosenRow, null, null, null, null);
if(c != null){
c.moveToFirst();
String reps = c.getString(1);
return reps;
}
return null;
}
//gets an entry from the timetype column by chosen row number (randomly generated in main activity)
public String getTimeType(int chosenRow) {
// TODO Auto-generated method stub
if (chosenRow == 0){
++chosenRow;
}
String[] columns = new String[]{KEY_REPS, KEY_EXERCISE, KEY_TIMETYPE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ID + "=" + chosenRow, null, null, null, null);
if(c != null){
c.moveToFirst();
String reps = c.getString(2);
return reps;
}
return null;
}
}