I am creating a budgeting app as a beginner coding project(Mostly to learn how to manipulate databases). Right now I am trying to figure out how to retrieve data from the database and then be able to use it.
I want to be able to make this work
display.setText(Integer.toString(dbHelper.getBalance()));
I have found tutorials using cursor but all deal with strings and I am having trouble imitating it for an integer. I erased the code setting up the database because I know it works properly because I have nothing else to say and I need to add more text than there is code it into this post. As well in the hidden code I inserted a single row into the database that set up a row with ID = 0 and balance = 500;
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "accountDB";
public static final String TABLE_ACCOUNT = "account";
public static final String COLUMN_ID = "id";
public static final String COLUMN_BALANCE = "balance";
public int getBalance(){
int balance;
SQLiteDatabase db = this.getWritableDatabase();
String[] columns = new String[]{COLUMN_ID, COLUMN_BALANCE};
Cursor c = db.query(TABLE_ACCOUNT, columns, COLUMN_ID + "=" + 0, null, null, null, null);
c.moveToFirst();
balance = c.getInt(2);
return balance;
}}
Aucun commentaire:
Enregistrer un commentaire