1

I want to use SQLite and i have written the Database class and trying to insert the values in the database table but i am getting an Exception which is :

I/Database(958): sqlite returned: error code = 1, msg = no such table: Exercise   

and when i fixed the log it points to this statement :

long d= dbAdapter.SaveExecise(date, exercise_Time, ex_Name, minutes, burned_cals);

why this exception is raised ? i have read and tried solutions of such problems but the error still how i can solve it ? this is my code:

public class DBAdapter {

  private static final String DB_NAME = "MYDB";
  private static final int DB_VERSION = 1;
  private static final String EXERCISE_TABLE_NAME = "Exercise";
  private static final String TAG = "DBAdapter";
  private DatabaseHelper DBHelper;
  private SQLiteDatabase db;
  private Context context;

  // table Exercise columns name
  public static final String KEY_DATE = "Date";
  public static final String KEY_TIME = "Time";
  public static final String KEY_NAME = "Name";
  public static final String KEY_PERIOD = "Period";
  public static final String KEY_BURNEDCALS = "Burned_Calories";

  private static final String EXERCISE_TABLE_CREATE =
      "create tables Exercise (Date text not null  , "
      + "Time  text not null  ,Name  text not null,"
      + "Period  REAL not null, Burned_Calories  REAL  not null,"
      + " primary key(Date,Time ) );";

  public DBAdapter(Context ctxt) {
    this.context = ctxt;
    DBHelper = new DatabaseHelper(context);
  }

  private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper(Context context) {
      super(context, DB_NAME, null, DB_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
      try {
        db.execSQL(EXERCISE_TABLE_CREATE);
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w(TAG, "Upgrading database from version"
          + oldVersion + " to " + newVersion
          + ", which will destroy all old data");
      db.execSQL("DROP TABLE IF EXISTS Exercise");
      onCreate(db);
    }

    public DBAdapter open() throws SQLException {
      db = DBHelper.getWritableDatabase();
      return this;
    }

    // ---closes the database---
    public void close() {
      DBHelper.close();
    }

    // ---insert Exercise info to the Exercise table---
    public long SaveExecise(String date, String time, String name, float period,
        float BurnedCalories) {
      ContentValues content = new ContentValues();
      content.put(KEY_DATE, date);
      content.put(KEY_TIME, time);
      content.put(KEY_NAME, name);
      content.put(KEY_PERIOD, period);
      content.put(KEY_BURNEDCALS, BurnedCalories);

      return db.insert(EXERCISE_TABLE_NAME, null, content);
    }
  }
}

and here where i insert the exercise information in the Exercise table:

save_exercise_btn = (Button) findViewById(R.id.save_exercise_Btn);
save_exercise_btn.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
    showSavingDialog();
    // save the name,ime,burnedcals of the exercise in the DB
    int year, month, day;
    year = localCalendar.get(1);
    month = localCalendar.get(2) + 1;
    day = localCalendar.get(5);
    String date = year + "/" + month + "/" + day;
    DBAdapter dbAdapter = new DBAdapter(SelectedExerciseInfo.this);
    dbAdapter = dbAdapter.open();
    long d = dbAdapter.SaveExecise(date, exercise_Time, ex_Name, minutes, burned_cals);
    dbAdapter.close();
    hideSavingDialog();
  }
});
Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
user
  • 621
  • 15
  • 46

1 Answers1

0
 private static final String EXERCISE_TABLE_CREATE ="create tables Exercise (Date text not null  , "+
    "Time  text not null  ,Name  text not null," + "Period  REAL not null, 
     Burned_Calories  REAL  not null," +" primary key(Date,Time ) );"  ;

You have put tables instead of table. The query becomes incorrect and no table is created. change it and the table would get created and data could then be added

 private static final String EXERCISE_TABLE_CREATE ="create table Exercise (Date text not null  , "+
    "Time  text not null  ,Name  text not null," + "Period  REAL not null, 
     Burned_Calories  REAL  not null," +" primary key(Date,Time ) );"  ;

Check this code for DBAdapter:

public class DBAdapter
{
    private static final String DB_NAME="MYDB3131";
    private static final int DB_VERSION= 1 ;
    private static final String EXERCISE_TABLE_NAME="Exercise";
    private static final String TAG = "DBAdapter";
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;
    private Context context;
    // table Exercise columns name
    public static final String KEY_DATE="Date";
    public static final String KEY_TIME="Time";
    public static final String KEY_NAME="Name";
    public static final String KEY_PERIOD="Period";
    public static final String KEY_BURNEDCALS="Burned_Calories";
    /*private static final String EXERCISE_TABLE_CREATE ="create table Exercise ( Date text not null  , "+
    "Time  text not null  ,Name  text not null," + "Period  REAL not null, Burned_Calories  REAL  not null );"  ;

*/
    private static final String EXERCISE_TABLE_CREATE ="create table Exercise (Date text not null  , "+
    "Time  text not null  ,Name  text not null," + "Period  REAL not null, Burned_Calories  REAL  not null," +" primary key(Date,Time ) );"  ;

public DBAdapter(Context ctxt)
    {      this.context=ctxt;
    DBHelper= new DatabaseHelper(context);
    }
    private static class DatabaseHelper extends SQLiteOpenHelper
    {      
        DatabaseHelper(Context    context)
        {
            super(context, DB_NAME, null, DB_VERSION);
        } 
        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(EXERCISE_TABLE_CREATE);
            System.out.println("hereeeee");
            }
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG, "Upgrading database from version" + oldVersion +" to "+    newVersion + ", which will destroy all old data");     
            db.execSQL("DROP TABLE IF EXISTS Exercise");                 
            onCreate(db);
        }
    }   

    //--open the DB
    public DBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---
    public void close()
    {
        DBHelper.close();
    }
    //---insert   Exercise info to the Exercise table---
    public long SaveExecise(String date ,String  time,String name ,float  period, float BurnedCalories)
    {
        ContentValues content = new ContentValues();
        content.put(KEY_DATE, date);
        content.put(KEY_TIME, time);
        content.put(KEY_NAME, name);
        content.put(KEY_PERIOD,period);
        content.put(KEY_BURNEDCALS, BurnedCalories);

        return db.insert(EXERCISE_TABLE_NAME, null, content);
    }}

The code for the sample Activity used :

public class MyA extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int year,month,day;
    Calendar localCalendar =  Calendar.getInstance();
    year = localCalendar.get(1);
 month = localCalendar.get(2)+1;
 day = localCalendar.get(5);
 String   date= year +"/"+month+"/"+day;
               DBAdapter    dbAdapter=new  DBAdapter(this);
              dbAdapter=dbAdapter.open();
 long d= dbAdapter.SaveExecise(date, 10034+"", 100+"", 100f, 100f);
               dbAdapter.close();
Toast.makeText(this, "text"+d, Toast.LENGTH_SHORT).show();
}
}
Akhil
  • 13,888
  • 7
  • 35
  • 39
  • I do what you said " change tables to table " but the error still !? – user Mar 27 '12 at 18:16
  • did the code work for you? I just changed the database name to a new name. It seems to work for me.Very strange. I am also adding the code which I used to add data from an Activity – Akhil Mar 27 '12 at 19:13
  • i have changed the database name before posting the code and the problem still ! why its worked for you but not for me !? you change only the name of the data base – user Mar 27 '12 at 19:17
  • Did u try the new dbAdapter? Are you still getting no such table exists? – Akhil Mar 27 '12 at 19:21
  • OH thank you vvvvvvvvvvvvvery much ,its work, the exception removed but why when i follow this path in the file explorer data > data > your-package-name > databases > your-database-file. i dont find my package name – user Mar 27 '12 at 19:39
  • I think you just need to reset your adb. Try the answer at http://stackoverflow.com/questions/9886644/adb-rejected-shell-command-ls-l-closed/9887017#9887017. Also please upvote and mark this answer as the right one :) – Akhil Mar 27 '12 at 19:43