【问题标题】:SQLiteException no such column (code 1)?SQLiteException 没有这样的列(代码 1)?
【发布时间】:2015-07-13 16:00:51
【问题描述】:

我不断收到错误:

SQLiteException: no such column: name (code 1): , while compile: 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));
}

}

【问题讨论】:

  • db.execSQL("CREATE TABLE items_table (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "date TEXT, amount TEXT, item TEXT);"); 在这条指令中,名为 name 的列在哪里?我看不到。
  • “这是我的 Logcat:”:它在哪里?您还没有发布任何内容。

标签: java android sql sqlite


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多