예제 #1
0
  public List<Participant> getAllParticipants() {
    List<Participant> participants = new LinkedList<Participant>();

    // 1. build the query
    String query = "SELECT * FROM " + TABLE_PARTICIPANT;

    // 2. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    // 3. go over each row, build participant and add it to list Participant participant = null;
    if (cursor.moveToFirst()) {
      do {
        Participant participant = new Participant();
        participant.setId(Integer.parseInt(cursor.getString(0)));
        participant.setNom(cursor.getString(1));
        participant.setPrenom(cursor.getString(2));

        // Add participant to participants
        participants.add(participant);
      } while (cursor.moveToNext());
    }
    // return participants
    return participants;
  }
예제 #2
0
  public Participant getParticipant(int id) {
    // 1. get reference to readable DB
    SQLiteDatabase db = this.getReadableDatabase();

    // 2. build query
    Cursor cursor =
        db.query(
            TABLE_PARTICIPANT,
            COLUMNS,
            PARTICIPANT_ID + " = ?",
            new String[] {String.valueOf(id)},
            null,
            null,
            null,
            null);

    // 3. if we got results get the first one if (cursor != null)
    cursor.moveToFirst();

    // 4. build Participant object
    Participant p = new Participant();
    p.setId(Integer.parseInt(cursor.getString(0)));
    p.setNom(cursor.getString(1));
    p.setPrenom(cursor.getString(2));

    // 5. return Participant
    return p;
  }