引起:android.database.sqlite.SQLiteException:没有这样的列:date(代码1):,同时编译:SELECT id,name,description,date FROM tasks

时间:2017-08-02 09:26:55

标签: android sqlite

我没有得到它,即使我检查代码是正确的,它仍然会崩溃。它只是一直告诉我没有这样的专栏日期。我将在这里发布我的代码用于DBHelper类。

private static final String DATABASE_NAME = "tasks.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_TASK = "tasks";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_DESCRIPTION = "description";
private static final String COLUMN_DATE = "date"; 

 @Override
public void onCreate(SQLiteDatabase db) {
    String createTaskTableSql = "CREATE TABLE " + TABLE_TASK + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_NAME + " TEXT, "
            + COLUMN_DESCRIPTION + " TEXT, "
            + COLUMN_DATE + " DATETIME DEFAULT CURRENT_TIMESTAMP )";
    db.execSQL(createTaskTableSql);
}

 public ArrayList<Lists> getAllLists() {
    ArrayList<Lists> lists = new ArrayList<Lists>();

    String selectQuery = "SELECT " + COLUMN_ID + ", "
            + COLUMN_NAME + ", "  + COLUMN_DESCRIPTION + ", " + COLUMN_DATE  + " FROM " + TABLE_TASK;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            int id = cursor.getInt(0);
            String listsName = cursor.getString(1);
            String desc = cursor.getString(2);
            String date = cursor.getString(3);
            Lists allLists = new Lists(id, listsName, desc, date);
            lists.add(allLists);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return lists;
}

我无法确定问题所在。有人请帮帮我。

1 个答案:

答案 0 :(得分:1)

  

没有这样的列日期

你有两个选择。

  1. 你应该卸载app&amp;再次运行
  2. 或者您可以纠正 onUpgrade 方法。
  3. <强> DO

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
        // If you want to add a column
        if (newVersion > oldVersion) {
            db.execSQL("ALTER TABLE ADD_NAME ADD COLUMN NEW_COLOUM INTEGER DEFAULT 0"); // You can add TEXT Field
        }
    }