samedi 13 juin 2015

Column not found in SQLite databse

I had created a column name called _id, but when i run and view the database in listview by using the getAllRows() method. But it show me an error say (no such column: _id (code 1): , while compiling: SELECT DISTINCT _id, eventTitle, date, destination, durationTime, alarmTime FROM Event.). Below is my code and my error message. Please help me and teach what happen with my code. Thanks alot!

public class DBHandler{

private static final String TAG = "DBHandler";

//Field Names
public static final String COLUMN_ID = "_id";
public static final String COLUMN_EVENTTITLE = "eventTitle";
public static final String COLUMN_Date = "date";
public static final String COLUMN_DESTINATION = "destination";
public static final String COLUMN_DURATION = "durationTime";
public static final String COLUMN_ALARMTIME = "alarmTime";
public static final String[] ALL_KEYS = new String[]{COLUMN_ID, COLUMN_EVENTTITLE, COLUMN_Date, COLUMN_DESTINATION, COLUMN_DURATION,COLUMN_ALARMTIME};

//Column Number for each Field Name:
public static final int COL_ID = 0;
public static final int COL_EventTITLE = 1;
public static final int COL_DATE = 2;
public static final int COL_DESTINATION = 3;
public static final int COL_DURATION = 4;
public static final int COL_ALARMTIME = 5;

//Database Info:
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "SP";
public static final String TABLE_EVENT = "Event";


//SQL Statament to create database
private static final String DATABASE_CREATE_SQL =
        "CREATE TABLE " + TABLE_EVENT + "(" +
        COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COLUMN_EVENTTITLE + " TEXT NOT NULL, " +
        COLUMN_Date + " TEXT NOT NULL, " +
        COLUMN_DESTINATION + " TEXT NOT NULL, " +
        COLUMN_DURATION + " TEXT NOT NULL, " +
        COLUMN_ALARMTIME + " TEXT NOT NULL" +
        ");";

private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;


public DBHandler(Context ctx){
    this.context = ctx;
    myDBHelper = new DatabaseHelper(context);
}

public DBHandler open(){
    db = myDBHelper.getWritableDatabase();
    return this;
}

public void close(){
    myDBHelper.close();
}


public long insertEvent(String eventtitle, String date, String destination, String duration, String alarmtime){
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_EVENTTITLE, eventtitle);
    contentValues.put(COLUMN_Date, date);
    contentValues.put(COLUMN_DESTINATION, destination);
    contentValues.put(COLUMN_DURATION, duration);
    contentValues.put(COLUMN_ALARMTIME, alarmtime);

    //Insert data into database
    return db.insert(TABLE_EVENT, null, contentValues);

}

//Delete a row from the database, by rowid (primary key)
public boolean deleteRow(long rowId){
    String where = COLUMN_ID + "=" + rowId;
    return db.delete(TABLE_EVENT, where, null) != 0;
}

//return all data in database
public Cursor getAllRows(){
    String where = null;
    Cursor c = db.query(true, TABLE_EVENT, ALL_KEYS, where, null, null, null,null, null);
    if(c != null){
        c.moveToFirst();
    }
    return c;
}

//Get a specific row(by rowID)
public Cursor getRow(long rowId){
    String where = COLUMN_ID + "=" + rowId;
    Cursor c = db.query(true, TABLE_EVENT, ALL_KEYS, where, null, null, null, null, null);
    if(c != null){
        c.moveToFirst();
    }
    return c;
}

//Change an existing row to be equal to new data
public boolean updateEvent(long id, String eventtitle, String date, String destination, String duration,String alarmtime ){
    String where = COLUMN_ID + "=" + id;
    ContentValues newValues = new ContentValues();
    newValues.put(COLUMN_EVENTTITLE, eventtitle);
    newValues.put(COLUMN_Date, date);
    newValues.put(COLUMN_DESTINATION, destination);
    newValues.put(COLUMN_DURATION, duration);
    newValues.put(COLUMN_ALARMTIME, alarmtime);

   return db.update(TABLE_EVENT, newValues, where, null) !=0;
}


private static class DatabaseHelper extends SQLiteOpenHelper{
    DatabaseHelper(Context context){
        super(context, TABLE_EVENT, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(DATABASE_CREATE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        Log.w(TAG, "Upgrading application's database from version" + oldVersion +
        "to" + newVersion + ", which will destroy all old data!");

        //Destroy old database:
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_EVENT);

        onCreate(db);
    }
}

}

android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT DISTINCT _id, eventTitle, date, destination, durationTime, alarmTime FROM Event at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1430) at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1277) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1148) at com.howard.fyp.DBHandler.getAllRows(DBHandler.java:92) at com.howard.fyp.TodayActivity.populateListView(TodayActivity.java:40) at com.howard.fyp.TodayActivity.onCreateView(TodayActivity.java:26) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5356) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) at dalvik.system.NativeStart.main(Native Method)

Aucun commentaire:

Enregistrer un commentaire