不知何故,代码不会插入数据。即使我将数据硬编码到内容值中。
public void addEvent(String username, String itemName, String price, String imageId, String eventType) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(EVENT_USERNAME, username);
values.put(EVENT_ITEM_NAME, itemName);
values.put(EVENT_ITEM_PRICE, price);
values.put(EVENT_ITEM_IMAGE, imageId);
values.put(EVENT_EVENT_TYPE, eventType);
long result;
result = db.insert(TABLE_EVENT, null, values);
Toast.makeText(context, "RESULT = " + result, Toast.LENGTH_SHORT).show();
db.close();
}
结果总是返回-1。
它不是来自我的数据源,因为我已经将它分别烘烤了,甚至硬编码只是为了检查它仍然没有。
继承人我的定义
//table name
private static final String TABLE_USERS = "table_users";
private static final String TABLE_EVENT = "table_event"
//table_event columns
private static final String EVENT_ID = "id";
private static final String EVENT_USERNAME = "username";
private static final String EVENT_ITEM_NAME = "item_name";
private static final String EVENT_ITEM_PRICE = "item_price";
private static final String EVENT_ITEM_IMAGE = "item_image_id";
private static final String EVENT_EVENT_TYPE = "event_type";
private static final String CREATE_TABLE_EVENT = "CREATE TABLE " + TABLE_EVENT
+ " (" + EVENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ EVENT_USERNAME + " VARCHAR, " + EVENT_ITEM_NAME + " VARCHAR, " + EVENT_ITEM_PRICE + " VARCHAR, "
+ EVENT_ITEM_IMAGE + " VARCHAR" + EVENT_EVENT_TYPE + " VARCHAR)";
用户表工作正常。 谢谢你的时间
答案 0 :(得分:0)
根据Android API
返回long新插入行的行ID,如果发生错误则返回-1
也许日志告诉你更多的调试。
答案 1 :(得分:-1)
search
create table语句字符串末尾缺少分号。尝试:
/*...*/ EVENT_EVENT_TYPE + " VARCHAR)";