Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8035411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:18:00+00:00 2026-06-05T02:18:00+00:00

I have several database tables in one database. The user inputs data to be

  • 0

I have several database tables in one database. The user inputs data to be inserted into the database, and one of the spinner values is to designate which table the data should go in.

The insert works fine for one table. However, as soon as I put an if statement in to have the data put in the correct table, nothing gets inserted anywhere. The error I get in logcat is “no such table: DATABASE_TABLE”, which doesn’t make sense to me because without the if statement in there it has no problems inserting.

Here is my code:

**Updated to include full code

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;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:18:02+00:00Added an answer on June 5, 2026 at 2:18 am

    As DATABASE_TABLE is a String variable, you should use CHOSEN_TABLE = DATABASE_TABLE; (without the quotes)….and the same for CHOSEN_TABLE = DATABASE_BICEPSTABLE; (no quotes).

    Also, you can’t use == for comparing strings. Use…

    if (exercisetype.equals("abs")){
        CHOSEN_TABLE = DATABASE_TABLE;
    }else if (exercisetype.equals("biceps")){
        CHOSEN_TABLE = DATABASE_BICEPSTABLE;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Access database (in Access 2003) with several tables, and data must
I have a database with several tables, each one containing columns that may not
I have a database which takes user submitted data, the entries from which I
I have a database with several tables, 5 of which are dedicated to specific
I'm implementing a database where several tables have string data as candidate keys (eg:
I have several database tables that just contain a single column and very few
Let's say we have system A comprising a MySQL database, with several tables. After
I need to write some data in several database. I choose sqlapi.com I have
I have a SQL Server database which is shared between several ASP.NET (VB.NET) websites,
I have a very large database with about 120 Million records in one table.I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.