-1
我正在編寫我的第一個應用程序,並且我也試圖合併一個數據庫, 爲什麼sqlitedatabase插入方法返回-1 我很困惑...爲什麼sqlitedatabase.insert返回-1?
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "MyDatabase";
private static final String TABLE_CONTACT = "contact";
private static final String id_col = "id";
private static final String name_col = "name";
private static final String ph_num_col = "phone_number";
public static final String CREATE__TABLE_contact=
"CREATE TABLE " + TABLE_CONTACT + " (" +
id_col + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
name_col + " TEXT, " +
ph_num_col + " TEXT" + ")";
public SqliteDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//sakhte table haye morede niaz....
sqLiteDatabase.execSQL(CREATE__TABLE_contact);
}
.................................... ...........................
public void addContact(Contact contact){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",contact.getName());
values.put("phone_number",contact.getPhone_number());
Long test = db.insert(TABLE_CONTACT, null, values);
if(test== -1) {
Log.i("insert", ""+test);
}
db.close();
}
您能否提供日誌跟蹤? –
-1意味着在數據庫中插入新行時發生錯誤。您需要提供更多詳細信息:如特定的SQL錯誤。看看你的logcat – GVillani82
嘗試使用_id而不是隻是id作爲列名,http://stackoverflow.com/questions/3192064/about-id-field-in-android-sqlite – avinesh