2015-06-13 16 views
-1

我重新命名了一個名爲_id的列名,但是當我運行並使用getAllRows()方法在ListView中查看數據庫時,它顯示一個錯誤,說(no such column: _id (code 1): , while compiling: SELECT DISTINCT _id, eventTitle, date, destination, durationTime, alarmTime FROM Event.)
下面是我的代碼和我的錯誤消息。
請幫我教一下我的代碼。在SQLite數據庫中沒有找到列

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.<init>(SQLiteProgram.java:58) 
      at android.database.sqlite.SQLiteQuery.<init>(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) 

回答

0

你在第二次加_id列?
然後,您必須增加您的DATABASE_VERSION固定值,以使onUpgrade()方法成爲可能。

但是你onUpgrade()方法失敗摧毀舊錶:

db.execSQL("DROP TABLE IF EXISTS" + TABLE_EVENT); 

你需要一個空間表名前:

db.execSQL("DROP TABLE IF EXISTS " + TABLE_EVENT); 

或者,由於舊錶仍然存在,它將不會創建新的。

+1

非常感謝..它的工作原理... –

+0

忘記空格是一個**常見的**錯誤。你無法想象我看到了多少次! –

+1

我真的認識到.. .. jux一個正常的間距缺失......非常感謝 –