コード例 #1
0
  public WalletModel insert(WalletModel wallet) {
    SQLiteDatabase db = openHelper.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(COLUMN_USER_ID, wallet.getUserId());
    values.put(COLUMN_WALLET_ADDRESS, wallet.getAddress());
    values.put(COLUMN_BALANCE, wallet.getBalance());

    db.insert(TABLE_WALLETS, null, values);
    Cursor cursor = db.rawQuery("SELECT last_insert_rowid()", null);

    if (cursor.moveToFirst()) {
      wallet.setId(cursor.getInt(0));
    }

    // Insert row
    db.close();
    cursor.close();

    return wallet;
  }
コード例 #2
0
  public WalletModel selectByUserId(int userId) {

    WalletModel wallet = new WalletModel();

    String selectQuery =
        "SELECT * FROM " + TABLE_WALLETS + " WHERE " + COLUMN_USER_ID + " = " + userId;

    SQLiteDatabase db = openHelper.getReadableDatabase();

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

    if (cursor.moveToFirst()) {
      wallet.setId(cursor.getInt(0));
      wallet.setUserId(cursor.getInt(1));
      wallet.setAddress(cursor.getString(2));
      wallet.setBalance(cursor.getString(3));
    }

    cursor.close();
    db.close();

    return wallet;
  }