我有一個有關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。
如果您需要任何信息,告訴我!
在此先感謝
AUTOINCREMENT是OK。 – 2012-02-23 12:22:19