2012-02-23 168 views
0

我有一個有關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

我認爲你autoincrement關鍵字需要一個下劃線,如:auto_increment

+0

AUTOINCREMENT是OK。 – 2012-02-23 12:22:19

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; 

此方法返回我的上下文中的類別列表。

以這種方式重新排列你的代碼,它將工作。