2015-07-10 97 views
1

後,我有一個表叫foods與主鍵food_id這是integer primary key autoincrement. Now i know that db.insert()因爲我在列表保存食品,以及在應用插入後returns id (-1 if insertion failed) but the question is if this is the same id as food_id ?? i want food_id`範圍。獲得該行的主鍵插入

+0

所以,你需要獲取該food_id價值? –

+0

是的確切:-) –

+0

請看看我的答案。 –

回答

1

請嘗試這個 -

 //local database variables 
      private DataSourceUserRecords dataResource ; 
      private SQLiteDatabase _database = null; 
      private Cursor _cursor = null; 
      private MySqliteHelper DBHelper; 

中的onCreate添加該代碼或要打電話。

DBHelper = new MySqliteHelper(UploadImgService.this); 
       _database = DBHelper.getWritableDatabase(); 
       dataResource = new DataSourceUserRecords(UploadImgService.this); 
       dataResource.open(); 

       _cursor = _database.rawQuery("select * from "+MySqliteHelper.TABLE_NAME, null); 

      _cursor.moveToFirst(); 

      while(_cursor.isAfterLast() == false){ 


       String _food_id = _cursor.getInt(_cursor.getColumnIndex(MySqliteHelper.YOUR_COLUMN_ID)); 

       System.out.println("Food ID from db : " + _food_id); 


       _cursor.moveToNext(); 
      } 
      _cursor.close();