mardi 16 février 2016

Two tables android sql

I have 2 tables and i want access from one to second. I can to transfer key of one table to second. It`s true? I have 2 handlers - databasehandlerforbalance and create user. I want that, when i create table user with name and password and table balance with balancedol balancerub and id of table balance pass to table user. How can i do?

public class DatabaseHandlerForBalance extends 

    SQLiteOpenHelper {
        SQLiteDatabase db;
        public static final String KEY_ID = "id";
        public static final String KEY_RubBalance = "rubBalance";
        public static final String KEY_DollarBalance = "dolBalance";
        private static final String DATABASE_NAME = "newUser";
        private static final String TABLE_NAME = "Balance";
        private static final int DATABASE_VERSION = 1;
        public DatabaseHandlerForBalance (Context context){
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String CREATE_BALANCE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
                    + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_RubBalance +
                    " INTEGER, " + KEY_DollarBalance + " INTEGER " + ")";
            db.execSQL(CREATE_BALANCE_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
            onCreate(db);
        }
        BalanceTable getBalance(int id) {
            SQLiteDatabase db = this.getReadableDatabase();

            Cursor cursor = db.query(TABLE_NAME, new String[]{KEY_ID,
                            KEY_DollarBalance, KEY_RubBalance}, KEY_ID + "=?",
                    new String[]{String.valueOf(id)}, null, null, null, null);
            if (cursor != null)
                cursor.moveToFirst();

            BalanceTable balanceTable = new BalanceTable(Integer.parseInt(cursor.getString(0)),
                    Integer.parseInt(cursor.getString(1)), Integer.parseInt(cursor.getString(2)));
            // return contact
            return balanceTable;
        }
        public boolean updateDollarBalance(int rowId, int addToDollarBalance) {
           BalanceTable myTableOfUser = getBalance(rowId);
            ContentValues args = new ContentValues();
            args.put(KEY_DollarBalance, myTableOfUser.getDollarBalance()+addToDollarBalance);
            return db.update(DATABASE_NAME, args, KEY_ID + "=" + rowId, null) > 0;
        }
        public void addBalance(BalanceTable balanceTable){
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(KEY_DollarBalance,balanceTable.getDollarBalance());
            values.put(KEY_RubBalance,balanceTable.getRubBalance());
            db.insert(TABLE_NAME, null, values);
            db.close();
        }
    }

and user handler:

public class DatabaseHandler extends SQLiteOpenHelper {
//----------- TABLE COLUMNS -----------//
public static final String KEY_ID = "id"; // eash user has unique id
public static final String KEY_NAME = "name";
public static final String KEY_PASSWORD = "password";
public static final String KEY_BALANCE = "balance";
public static final String KEY_AGE ="age";
public static final String BALANCE_ID = "balanceId";
//----------- TABLE COLUMNS -----------//
private static final String DATABASE_NAME = "newUser";
private static final String TABLE_NAME = "User";
public DatabaseHandler(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final int DATABASE_VERSION = 1;
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_NEWUSER_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
            + KEY_ID + " INTEGER PRIMARY KEY, " +BALANCE_ID + " INTEGER, " + KEY_NAME +
            " TEXT, " + KEY_PASSWORD + " TEXT, " + KEY_AGE + " INTEGER, "
            + KEY_BALANCE + " INTEGER" + ")";
    db.execSQL(CREATE_NEWUSER_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
    onCreate(db);
}

public int getUsersCount() {
    String countQuery = "SELECT  * FROM " + TABLE_NAME;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();
    // return count
    return cursor.getCount();
}
// добавить новую запись
public void addUser(NewUserTable newUserTable){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME,newUserTable.getName());
    values.put(KEY_PASSWORD,newUserTable.getPassword());
    values.put(KEY_BALANCE,newUserTable.getBalance());
    values.put(KEY_AGE,newUserTable.getAge());
    values.put(BALANCE_ID,newUserTable.getBalanceId());
    db.insert(TABLE_NAME, null, values);
    db.close();
}
// считать записи
public NewUserTable getUser(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID,
                    KEY_NAME, KEY_PASSWORD, KEY_BALANCE, KEY_AGE, BALANCE_ID }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    NewUserTable newUserTable = new NewUserTable(Integer.parseInt(cursor.getString(0)),cursor.getString(1),
            cursor.getString(2), Integer.parseInt(cursor.getString(3)),
            Integer.parseInt(cursor.getString(4)),Integer.parseInt(cursor.getString(5)));
    // return contact
    return newUserTable;
}
public List<NewUserTable> getAllUsers() {
    List<NewUserTable> contactList = new ArrayList<NewUserTable>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_NAME;

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            NewUserTable newUserTable = new NewUserTable();
            newUserTable.setID(Integer.parseInt(cursor.getString(0)));
            newUserTable.setName(cursor.getString(1));
            newUserTable.setPassword(cursor.getString(2));
            newUserTable.setBalance(Integer.parseInt(cursor.getString(3)));
            newUserTable.setAge(Integer.parseInt(cursor.getString(4)));
            newUserTable.setBalanceId(Integer.parseInt(cursor.getString(5)));
            contactList.add(newUserTable);
        } while (cursor.moveToNext());
    }
    // return contact list
    return contactList;
}

}

Aucun commentaire:

Enregistrer un commentaire