コード例 #1
0
  public void addUser(User user) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_USER, null, null);

    ContentValues values = new ContentValues();
    values.put(KEY_ID, user.getId());
    values.put(KEY_FIRSTNAME, user.getFirstname());
    values.put(KEY_LASTNAME, user.getLastname());
    values.put(KEY_USERNAME, user.getUsername());
    values.put(KEY_TOKEN, user.getToken());

    db.insert(TABLE_USER, null, values);

    db.close(); // Closing database connection
  }
コード例 #2
0
  public User getUser() {
    // Select All Query
    User user = new User();

    String selectQuery = "SELECT * FROM " + TABLE_USER;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {

      user.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)));
      user.setFirstname(cursor.getString(cursor.getColumnIndex(KEY_FIRSTNAME)));
      user.setLastname(cursor.getString(cursor.getColumnIndex(KEY_LASTNAME)));
      user.setUsername(cursor.getString(cursor.getColumnIndex(KEY_USERNAME)));
      user.setToken(cursor.getString(cursor.getColumnIndex(KEY_TOKEN)));
    }
    cursor.close();
    db.close();
    // return contact list
    return user;
  }