【问题标题】:How to resolve this SQLiteException: no such column ?如何解决这个 SQLiteException: no such column ?
【发布时间】:2012-07-30 11:51:59
【问题描述】:

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

D/Error(2808): android.database.sqlite.SQLiteException: no such column: names: , while compile: SELECT id, names, items, quantity, price FROM orderDetails

我该如何解决这个问题?

公共类 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;

    }
}

}

【问题讨论】:

    标签: android sqlite


    【解决方案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);
    

    【讨论】:

    • 嘿,朋友必须遵循我关于DELETING OLDER创建的数据库的第一个解决方案。
    • 否则 OLDER DATABASE 将没有“名称”列。因此,要创建具有“名称”列的新表,您必须 DELETE 或 ALTER 旧数据库
    • 是的,它成功了,非常感谢 :) 是的,我不得不删除旧表。
    【解决方案2】:

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

    @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);
    
    }
    

    【讨论】:

    • 没问题,伙计。我们都有这样的时候。 :)
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      相关资源
      最近更新 更多