我有一个关于SQLite和整数主键自动增量字段的问题。 我知道我可以通过调用ROWID列来获取id,但我需要一个'_id'。
这是我创建表格的查询:
private static final String TABLE_FACEBOOK_IMPORTED_CONTACTS = "facebook_imported_contacts";
private static final String COL_ID = "_id";
private static final String COL_FACEBOOK_ID = "facebookId";
private static final String COL_PHONE_CONTACT_ID = "phoneContactId";
private static final String COL_FIRST_NAME = "firstName";
private static final String COL_LAST_NAME= "lastName";
private static final String COL_BIRTH_DATE = "birthDate";
private static final String COL_GROUP = "contactGroup";
private static final String COL_MAIL_ADDRESS = "mailAddress";
private static final String COL_PHONE_NUMBER = "phoneNumber";
public static final String CREATE_FACEBOOK_IMPORT_BDD = "" +
"CREATE TABLE "+TABLE_FACEBOOK_IMPORTED_CONTACTS+" ("+
COL_ID + " integer primary key autoincrement," +
COL_FACEBOOK_ID +" INT," +
COL_PHONE_CONTACT_ID +" INT, " +
COL_FIRST_NAME +" TEXT NOT NULL," +
COL_LAST_NAME +" TEXT NOT NULL, " +
COL_BIRTH_DATE +" TEXT," +
COL_GROUP +" TEXT," +
COL_MAIL_ADDRESS +" TEXT," +
COL_PHONE_NUMBER +" TEXT);";
所以我没有错误消息。一切正常,除了...... COL_ID字段(_id)不会自动递增。 以下是代码的其余部分:
public class FacebookContactDAO {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "automatic_sms.db";
private static final String TABLE_FACEBOOK_IMPORTED_CONTACTS = "facebook_imported_contacts";
private static final String COL_ID = "_id";
private static final int NUM_COL_ID = 0;
private static final String COL_FACEBOOK_ID = "facebookId";
private static final int NUM_COL_FACEBOOK_ID = 1;
private static final String COL_PHONE_CONTACT_ID = "phoneContactId";
private static final int NUM_COL_PHONE_CONTACT_ID = 2;
public static final String COL_FIRST_NAME = "firstName";
private static final int NUM_COL_FIRST_NAME = 3;
public static final String COL_LAST_NAME= "lastName";
private static final int NUM_COL_LAST_NAME = 4;
private static final String COL_BIRTH_DATE = "birthDate";
private static final int NUM_COL_BIRTH_DATE = 5;
private static final String COL_GROUP = "contactGroup";
private static final int NUM_COL_GROUP = 6;
private static final String COL_MAIL_ADDRESS = "mailAddress";
private static final int NUM_COL_MAIL_ADDRESS = 7;
private static final String COL_PHONE_NUMBER = "phoneNumber";
private static final int NUM_COL_PHONE_NUMBER = 8;
private SQLiteDatabase database;
private SQLiteDB mySQLiteDB;
public FacebookContactDAO(Context context){
//On créer la BDD et sa table
mySQLiteDB = new SQLiteDB(context, DB_NAME, null, DB_VERSION);
}
public void open(){
database = mySQLiteDB.getWritableDatabase();
//database.execSQL("DROP TABLE " + TABLE_FACEBOOK_IMPORTED_CONTACTS + ";");
//database.execSQL(SQLiteDB.CREATE_FACEBOOK_IMPORT_BDD);
}
public void close(){
database.close();
}
public SQLiteDatabase getDB(){
return database;
}
public long insert(FacebookContact contact){
ContentValues contactValues = new ContentValues();
contactValues.put(COL_FACEBOOK_ID, contact.getFacebookId());
contactValues.put(COL_PHONE_CONTACT_ID, contact.getPhoneContactId());
contactValues.put(COL_FIRST_NAME, contact.getFirstName());
contactValues.put(COL_LAST_NAME, contact.getLastName());
contactValues.put(COL_BIRTH_DATE, contact.getBirthDate());
contactValues.put(COL_GROUP, contact.getGroup());
contactValues.put(COL_MAIL_ADDRESS, contact.getMailAddress());
contactValues.put(COL_PHONE_NUMBER, contact.getPhoneNumber());
return database.insert(TABLE_FACEBOOK_IMPORTED_CONTACTS, null, contactValues);
}
public int update(int id, FacebookContact contact){
ContentValues contactValues = new ContentValues();
//contactValues.put(COL_ID, contact.getId()); // (shouldn't be able to change the id)
contactValues.put(COL_FACEBOOK_ID, contact.getFacebookId());
contactValues.put(COL_PHONE_CONTACT_ID, contact.getPhoneContactId());
contactValues.put(COL_FIRST_NAME, contact.getFirstName());
contactValues.put(COL_LAST_NAME, contact.getLastName());
contactValues.put(COL_BIRTH_DATE, contact.getBirthDate());
contactValues.put(COL_GROUP, contact.getGroup());
contactValues.put(COL_MAIL_ADDRESS, contact.getMailAddress());
contactValues.put(COL_PHONE_NUMBER, contact.getPhoneNumber());
return database.update(TABLE_FACEBOOK_IMPORTED_CONTACTS, contactValues, COL_ID + " = " +id, null);
}
public int remove(long facebookId){
//Suppression d'un livre de la BDD grâce à l'ID
return database.delete(TABLE_FACEBOOK_IMPORTED_CONTACTS, COL_FACEBOOK_ID + " = " +facebookId, null);
}
public int removeEverything(){
//Suppression d'un livre de la BDD grâce à l'ID
return database.delete(TABLE_FACEBOOK_IMPORTED_CONTACTS, "1 = 1", null);
}
public FacebookContact getByFacebookId(long facebookId) {
Cursor queryResult = database.query( TABLE_FACEBOOK_IMPORTED_CONTACTS,
new String[] {COL_ID,COL_FACEBOOK_ID, COL_PHONE_CONTACT_ID, COL_FIRST_NAME,COL_LAST_NAME,COL_BIRTH_DATE,COL_GROUP,COL_MAIL_ADDRESS,COL_PHONE_NUMBER},
COL_FACEBOOK_ID + " = " + facebookId + "",
null, null, null, null);
FacebookContact contact = cursorToFacebookContact(queryResult);
queryResult.close();
return contact;
}
public Cursor getAll() {
Cursor queryResult = database.query( TABLE_FACEBOOK_IMPORTED_CONTACTS,
new String[] {COL_ID,COL_FACEBOOK_ID, COL_PHONE_CONTACT_ID, COL_FIRST_NAME,COL_LAST_NAME,COL_BIRTH_DATE,COL_GROUP,COL_MAIL_ADDRESS,COL_PHONE_NUMBER},
"1",
null, null, null, COL_LAST_NAME+","+COL_FIRST_NAME);
//FacebookContact contact = cursorToFacebookontact(queryResult);
//queryResult.close();
return queryResult;
}
private FacebookContact cursorToFacebookContact(Cursor c){
if (c.getCount() == 0)
return null;
c.moveToFirst();
FacebookContact contact = new FacebookContact();
contact.setId(c.getInt(NUM_COL_ID));
contact.setFacebookId(c.getInt(NUM_COL_FACEBOOK_ID));
contact.setPhoneContactId(c.getInt(NUM_COL_PHONE_CONTACT_ID));
contact.setFirstName(c.getString(NUM_COL_FIRST_NAME));
contact.setLastName(c.getString(NUM_COL_LAST_NAME));
contact.setBirthDate(c.getString(NUM_COL_BIRTH_DATE));
contact.setGroup(c.getString(NUM_COL_GROUP));
contact.setMailAddress(c.getString(NUM_COL_MAIL_ADDRESS));
contact.setPhoneNumber(c.getString(NUM_COL_PHONE_NUMBER));
c.close();
return contact;
}
}
可能问题来自私人FacebookContact cursorToFacebookContact。
如果您需要任何信息,请告诉我!
提前致谢
答案 0 :(得分:1)
删除自动增量
确保您的模型类具有变量以及get和set方法
- 在模态类---
public int getId(){
return this._id;
}
public void setId(int id){
this._id= id;
}
这两种方法在您的模型类中是强制性的
查看您的表是否像以下SQL代码一样创建:
private static final String CREATE_CATEGORY_TABLE=
"CREATE TABLE "+CATEGORY_TABLE+"("
+CAT_ID+" INTEGER PRIMARY KEY , "
+CATEGORY+" TEXT, "
+ DESCRIPTION+" TEXT, "
+CAT_TYPE+" TEXT)";
我有CAT_ID= _id declared;
您无需在DatabaseManager添加方法中添加该字段。
但是您需要在从数据库管理器返回值列表时设置id 等;
public ArrayList<Category> getCategory(){
ArrayList<Category> listofCat= new ArrayList<Category>();
String selquery="SELECT * FROM "+CATEGORY_TABLE;
SQLiteDatabase db= this.getReadableDatabase();
Cursor cursor= db.rawQuery(selquery,null);
if(cursor.moveToFirst()){
do{
Category category= new Category();
category.setId(cursor.getInt(cursor.getColumnIndex(CAT_ID)));
category.setCatName(cursor.getString(cursor.getColumnIndex(CATEGORY)));
category.setCatDesc(cursor.getString(cursor.getColumnIndex(DESCRIPTION)));
category.setCatType(cursor.getString(cursor.getColumnIndex(CAT_TYPE)));
listofCat.add(category);
}while (cursor.moveToNext());
}
cursor.close();
return listofCat;
此方法返回上下文中的类别列表。
以这种方式重新排列代码。
答案 1 :(得分:0)
我认为您的autoincrement
关键字需要一个下划线,如:auto_increment