0

条目已成功提交,但当按下“查看”​​按钮时,应用程序崩溃并且 Logcat 输出为:

D/错误(2808):android.database.sqlite.SQLiteException:没有这样的列:名称:,编译时:从 orderDetails 中选择 id、名称、项目、数量、价格

我该如何解决这个问题?

公共类 DatabaseHandler 扩展 SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "orders";
private static final String TABLE_ORDERS = "orderDetails";

// Now, column names

private static final String KEY_ID = "id";
private static final String KEY_NAME = "names";
private static final String KEY_ITEM = "items";
private static final String KEY_QUANTITY = "quantity";
private static final String KEY_PRICE = "price";

private DatabaseHandler handler;
private final Context myContext;
private SQLiteDatabase myDatabase;

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    myContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_TABLE = "CREATE TABLE " + TABLE_ORDERS + "(" + KEY_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ITEM + " BLOB, "
            + KEY_QUANTITY + " REAL, " + KEY_PRICE + " REAL);";
    db.execSQL(CREATE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORDERS);
    onCreate(db);
}

public void addEntry(String name, String item, float quantity, float price) {
    try {
        handler = new DatabaseHandler(myContext);
        myDatabase = handler.getWritableDatabase();

        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_ITEM, item);
        cv.put(KEY_QUANTITY, quantity);
        cv.put(KEY_PRICE, price);

        myDatabase.insert(TABLE_ORDERS, null, cv);
        handler.close();

        // delete this toast
        Toast.makeText(myContext, "It worked", Toast.LENGTH_SHORT).show();

    } catch (Exception e) {
        // TODO Auto-generated catch block

        Toast.makeText(myContext, "It failed", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
        Log.d("Error", e.toString());
    }

}

public String getEntries() {

    try {
        handler = new DatabaseHandler(myContext);
        myDatabase = handler.getReadableDatabase();
        String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_ITEM,
                KEY_QUANTITY, KEY_PRICE };

        Cursor c = myDatabase.query(TABLE_ORDERS, columns, null, null, null,
                null, null);
        String result = "";

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result = result + c.getString(0) + " |" + c.getString(1) + " |"
                    + c.getString(2) + " |" + c.getString(3) + " |"
                    + c.getString(4) + "\n";
        }
        handler.close();
        return result;
    } catch (Exception e) {
        Log.d("Error", e.toString());
        e.printStackTrace();
        return null;

    }
}

}

4

3 回答 3

1

解决方案

[1] 首先从您的 AVD 或 Real Device 中删除创建的数据库,因为已经在那里创建的表没有列 KEY_NAME("names")

[2] 那么下次你何时运行应用程序时,该表将在表中使用以下代码创建::

String CREATE_TABLE = "CREATE TABLE " + TABLE_ORDERS + "(" + KEY_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ITEM + " BLOB, "
            + KEY_NAME + " TEXT, "  //  ADD THIS  COLUMN
            + KEY_QUANTITY + " REAL, " + KEY_PRICE + " REAL);";
    db.execSQL(CREATE_TABLE);

忘记将 KEY_NAME放入表的创建查询中,请参阅以下代码:

String CREATE_TABLE = "CREATE TABLE " + TABLE_ORDERS + "(" + KEY_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ITEM + " BLOB, "
            + KEY_QUANTITY + " REAL, " + KEY_PRICE + " REAL);";
    db.execSQL(CREATE_TABLE);
于 2012-07-30T11:54:09.743 回答
0

如您所见,您的表定义中没有列名:

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_TABLE = "CREATE TABLE " + TABLE_ORDERS + "(" + KEY_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ITEM + " BLOB, "
            + KEY_QUANTITY + " REAL, " + KEY_PRICE + " REAL);";
    db.execSQL(CREATE_TABLE);

}
于 2012-07-30T11:55:52.277 回答
-1

“名称”是保留字。尝试使用带方括号的 [names]。

于 2012-07-30T11:54:39.640 回答