【发布时间】: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:”:它在哪里?您还没有发布任何内容。