2015-07-10 53 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();