在SQLite数据库中找不到列

时间:2015-06-13 15:26:17

标签: android database sqlite

我创建了一个名为_id的列名,但是当我使用getAllRows()方法运行并在ListView中查看数据库时,它显示错误(no such column: _id (code 1): , while compiling: SELECT DISTINCT _id, eventTitle, date, destination, durationTime, alarmTime FROM Event.)。 以下是我的代码和我的错误消息 请帮助我并教导我的代码会发生什么。

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)

1 个答案:

答案 0 :(得分:0)

您是否第二次添加了_id列? 然后,您必须增加 DATABASE_VERSION 常量值才能使onUpgrade()方法触发。

但是你的onUpgrade()方法无法破坏旧表:

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

在表名前需要空格

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

或者,由于旧表仍然存在,因此不会创建新表。