条目已成功提交但按“视图”按钮时,应用程序崩溃且Logcat输出为:
D /错误(2808):android.database.sqlite.SQLiteException:没有这样的列:名称:,同时编译: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;
}
}
}
答案 0 :(得分:1)
<强>解强>
[1]首先从您的AVD或Real设备中删除创建的数据库,因为那里已经创建的表没有列KEY_NAME(“名称”)
[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);
答案 1 :(得分: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);
}
答案 2 :(得分:-1)
“名字”是保留字吗?尝试使用方括号[name]。[/ p>