SQLiteException没有这样的列(代码1)?

时间:2015-07-13 16:00:51

标签: java android sql sqlite

我一直收到错误:

SQLiteException:没有这样的列:name(代码1):,同时编译:SELECT _id,date,amount,item FROM items_table ORDER BY name

每当我尝试进入某项活动时,它都会崩溃,而且我不确定如何修复它。我已尝试过其他用户在此论坛中提出的许多选项,但无济于事。非常感谢任何帮助。

这是我的 Logcat

它似乎指向另一项活动,也称为“T_Entertainment"

这是 T_Entertainment.class

prefs = PreferenceManager.getDefaultSharedPreferences(this);
    initList();
    prefs.registerOnSharedPreferenceChangeListener(prefListener);

    //newEntry = (Button) findViewById(R.id.add_entry);
    //newEntry.setOnClickListener(new View.OnClickListener() {

    //@Override
    //public void onClick(View v) {
    //startActivity(new Intent(T_Entertainment.this, NewEntry.class));
    //}
}
//}

private void initList(){
    if (model != null){
        stopManagingCursor(model);
        model.close();
    }
    model = helper.getAll(prefs.getString("sort_order", "name"));
    startManagingCursor(model);
    adapter = new ItemAdapter(model);
    setListAdapter(adapter);
}

这是我的 Helper.class

class Helper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mymanager.db";
private static final int SCHEMA_VERSION = 2;

public Helper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // Will be called once when the database is not created
    db.execSQL("CREATE TABLE items_table (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
            + "date TEXT, amount TEXT, item TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    //Will not be called until 2nd scheme i.e. when SCHEME_VERSION
    //increases

}

/*Read all records from items_table */
public Cursor getAll(String orderBy) {
    return (getReadableDatabase().rawQuery (
            "SELECT _id, date, amount, item FROM items_table ORDER BY " + orderBy, null));
}

/* Read a record from items_table by ID provided */
public Cursor getById(String id) {
    String[] args = { id };

    return (getReadableDatabase().rawQuery("SELECT _id, date, amount, item FROM items_table WHERE _ID=?", args));
}

/* Write a record into items_table */
public void insert(String date, String amount, String item) {
    ContentValues cv = new ContentValues();

    cv.put("date", date);
    cv.put("amount", amount);
    cv.put("item", item);

    getWritableDatabase().insert("items_table", "name", cv);
}

public void update(String id, String date, String amount, String item) {
    ContentValues cv = new ContentValues();
    String[] args = { id };
    cv.put("date", date);
    cv.put("amount", amount);
    cv.put("item", item);

    getWritableDatabase().update("items_table", cv, "_ID=?", args);
}

public String getID(Cursor c) {
    return (c.getString(0));
}

public String getDate(Cursor c) {
    return (c.getString(1));
}

public String getAmount(Cursor c) {
    return (c.getString(2));
}

public String getItem(Cursor c) {
    return (c.getString(3));
}

}

1 个答案:

答案 0 :(得分:0)

似乎您正在将数据库从先前版本更新为新添加了一列的新数据库。 尝试从设备中删除旧应用程序并再次安装,它不应再出现问题。