1

我在一个数据库中有几个数据库表。用户输入要插入数据库的数据,微调器值之一是指定数据应该进入哪个表。

插入适用于一张桌子。但是,只要我放入 if 语句以将数据放入正确的表中,就不会在任何地方插入任何内容。我在 logcat 中得到的错误是“没有这样的表: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;
}

}

4

1 回答 1

0

作为DATABASE_TABLE一个字符串变量,你应该使用CHOSEN_TABLE = DATABASE_TABLE;(不带引号)......和相同的CHOSEN_TABLE = DATABASE_BICEPSTABLE;(不带引号)。

此外,您不能==用于比较字符串。采用...

if (exercisetype.equals("abs")){
    CHOSEN_TABLE = DATABASE_TABLE;
}else if (exercisetype.equals("biceps")){
    CHOSEN_TABLE = DATABASE_BICEPSTABLE;
}
于 2012-06-03T23:21:48.267 回答