0

Create table or the Insert query is not working!

public class dbHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "AppointmentsDB";
private static final String TABLE_APPOINTMENTS = "tblAppointments";
private static final String COLUMN_DATE = "dateofapp";
private static final String COLUMN_NAME = "appointment";
private static final String COLUMN_LOCATION = "location";
private static final String COLUMN_Priority = "priority";
private static final String COLUMN_STATUS = "status";

public dbHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE " + TABLE_APPOINTMENTS + "(" + COLUMN_DATE + " TEXT, "
            + COLUMN_NAME + " TEXT,"
            + COLUMN_LOCATION + " TEXT," +
            COLUMN_Priority + " TEXT," +
            COLUMN_STATUS + " TEXT);";

    db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE  IF EXISTS" + TABLE_APPOINTMENTS);
    onCreate(db);
}

//add new row
public void addAppointment(Appointments appointment) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_DATE, appointment.getDate());
    values.put(COLUMN_NAME, appointment.getName());
    values.put(COLUMN_LOCATION, appointment.getLocation());
    values.put(COLUMN_Priority, appointment.getPriority());
    values.put(COLUMN_STATUS, appointment.getStatus());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_APPOINTMENTS, null, values);
    db.close();
}

//delete
public void deleteAppointment(String appName) {
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM" + TABLE_APPOINTMENTS + "WHERE" + COLUMN_NAME + "=\"" + appName + "=\";");
}

//display
public String databaseToString() {

    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String sql = "SELECT * FROM " +TABLE_APPOINTMENTS+ "WHERE 1";

    Cursor c = db.rawQuery(sql,null);
    c.moveToFirst();
    while(!c.isAfterLast()){
        if(c.getString(c.getColumnIndex("appointment"))!= null){
            dbString += c.getString(c.getColumnIndex("appointment"));
            dbString += "\n";
        }
    }

    db.close();

    return dbString;
}

public Cursor getAllData (){
    SQLiteDatabase db = getWritableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM " + TABLE_APPOINTMENTS,null);
    return res;
}

}

Error says there is no column for date. But when i test it with some other string it gives some other column is missing so i'm guessing the table is not getting created

12-08 00:28:37.443 1436-1436/com.example.user.testerapp E/SQLiteLog: (1) table tblAppointments has no column named priority 12-08 00:28:37.445 1436-1436/com.example.nadeeshherath.singervistalife E/SQLiteDatabase: Error inserting priority=android.widget.EditText{304f0d2f VFED..CL ........ 0,372-600,496 #7f0c005e app:id/priText} dateofapp=android.widget.EditText{45cf210 VFED..CL ........ 0,0-630,124 #7f0c005c app:id/dateText} status=android.widget.EditText{9071f3c VFED..CL ........ 0,496-600,620 #7f0c005f app:id/statusText} location=android.widget.EditText{15b8ff0e VFED..CL ........ 0,248-1350,372 #7f0c005d app:id/locText} appointment=android.widget.EditText{35b42109 VFED..CL .F...... 0,124-1350,248 #7f0c005b app:id/appText} android.database.sqlite.SQLiteException: table tblAppointments has no column named priority (code 1): , while compiling: INSERT INTO tblAppointments(priority,dateofapp,status,location,appointment) VALUES (?,?,?,?,?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469) at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) at com.example.user.testerapp.dbHandler.addAppointment(dbHandler.java:52) at com.example.user.tester.events_activity.addDataButtonClick(events_activity.java:117) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.view.View$1.onClick(View.java:4002) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

thebeast
  • 108
  • 1
  • 11
  • 1
    Looks like `priority column` is not found in your Table. becoz may be you recently added this column. If yes then you should uninstall the older app and install a fresh built and if not then you should add new column in `onUpgrade(...)` and increase `DB Version` – M D Dec 08 '15 at 06:16
  • Just to extend the comment (as I believe this is exactly what is happening), increasing the DATABASE_VERSION is enough (no need of uninstalling the app) – JML May 26 '16 at 17:45

0 Answers0