Results 1 to 2 of 2
  1. #1
    exclusiv3 is offline Member
    Join Date
    Dec 2016
    Posts
    1
    Rep Power
    0

    Default Database fetching isn't working properly - Android

    I've the code below which I've managed to get extras from another activity and put them in class that working perfectly fine (I've checked it couple of times, the problem isn't in Details class).
    Anyway, when I get the result from the activity with extras it should add it to the database or update it if the table already exists, though the application crash every time I sending result back to my main activity.

    Any ideas what causing this?




    MainActivity.java
    Java Code:
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK && requestCode == 1) {
                Details details = new Details(data.getExtras().getDouble("returnData3"), data.getExtras().getDouble("returnData4"),
                        data.getExtras().getString("returnData"), data.getExtras().getString("returnData1"),
                        data.getExtras().getBoolean("returnData5"), data.getExtras().getInt("returnData2")); // Updated details
    
    
                DatabaseHandler db = new DatabaseHandler(this); //DATABASE
                if(db.check()) {
                    db.updateDetails(details);
                }
                else{
                    db.addDetails(details);
                }
                final TextView bmi = (TextView) findViewById(R.id.bmi);
                final TextView bmr = (TextView) findViewById(R.id.bmr);
                final TextView rmr = (TextView) findViewById(R.id.rmr);
                bmi.setText(db.getDetails().toString());
                bmr.setText(details.toString());
                //bmi.setText(String.format("BMI: %.2f", details.getBmi()));
                //bmr.setText(String.format("BMR: %.2f", details.getBmr()));
                //rmr.setText(String.format("RMR: %.2f", details.getRmr()));
    
    
                Toast.makeText(MainActivity.this, "Information saved successfully", Toast.LENGTH_LONG).show();
    
                NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
                navigationView.setCheckedItem(R.id.nav_home);
            }
    DatabaseHandler.java
    Java Code:
    import android.content.ContentValues;
            import android.content.Context;
            import android.database.Cursor;
            import android.database.sqlite.SQLiteDatabase;
            import android.database.sqlite.SQLiteOpenHelper;
    
    public class DatabaseHandler extends SQLiteOpenHelper {
    
        // Database Version
        private static final int DATABASE_VERSION = 1;
    
        // Database Name
        private static final String DATABASE_NAME = "details";
    
        //table name
        private static final String TABLE_DETAILS = "details";
    
        //Table Columns names
        private static final String KEY_HEIGHT = "height";
        private static final String KEY_WEIGHT = "weight";
        private static final String KEY_NAME = "name";
        private static final String KEY_LASTNAME = "lastname";
        private static final String KEY_GENDER = "gender";
        private static final String KEY_AGE = "age";
    
        public DatabaseHandler(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
    
        // Creating Tables
        @Override
        public void onCreate(SQLiteDatabase db) {
            String CREATE_DETAILS_TABLE = "CREATE TABLE " + TABLE_DETAILS + "("
                    + KEY_HEIGHT + " REAL," + KEY_WEIGHT + " REAL,"
                    + KEY_NAME + " TEXT," + KEY_LASTNAME + " TEXT," + KEY_GENDER + " NUMERIC," + KEY_AGE + "INTEGER" + ")";
            db.execSQL(CREATE_DETAILS_TABLE);
        }
    
        // Upgrading database
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // Drop older table if existed
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAILS);
    
            // Create tables again
            onCreate(db);
        }
    
        /**
         * All CRUD(Create, Read, Update, Delete) Operations
         */
    
        // Adding new contact
        void addDetails(Details detail) {
            SQLiteDatabase db = this.getWritableDatabase();
    
            ContentValues values = new ContentValues();
            values.put(KEY_HEIGHT, detail.getHeight());
            values.put(KEY_WEIGHT, detail.getWeight());
            values.put(KEY_NAME, detail.getName());
            values.put(KEY_LASTNAME, detail.getLastName());
            values.put(KEY_GENDER, detail.getGender());
            values.put(KEY_AGE, detail.getAge());
    
            // Inserting Row
            db.insert(TABLE_DETAILS, null, values);
            db.close(); // Closing database connection
        }
    
        public boolean check(){
            SQLiteDatabase db = this.getWritableDatabase();
            String selectQuery = "SELECT  * FROM " + TABLE_DETAILS;
            Cursor cursor = db.rawQuery(selectQuery, null);
            Boolean rowExists;
    
            if (cursor.moveToFirst())
            {
                // DO SOMETHING WITH CURSOR
                rowExists = true;
    
            } else
            {
                // I AM EMPTY
                rowExists = false;
            }
            return rowExists;
        }
        // Getting All Contacts
        public Details getDetails() {
            SQLiteDatabase db = this.getWritableDatabase();
            String selectQuery = "SELECT  * FROM " + TABLE_DETAILS;
            Cursor cursor = db.rawQuery(selectQuery, null);
            Details details = new Details(cursor.getDouble(0), cursor.getDouble(1),
                    cursor.getString(2), cursor.getString(3), cursor.getInt(4) > 0,
                    cursor.getInt(5));
    
            return details;
        }
    
        // Updating single contact
        public int updateDetails(Details detail) {
            SQLiteDatabase db = this.getWritableDatabase();
    
            ContentValues values = new ContentValues();
            values.put(KEY_HEIGHT, detail.getHeight());
            values.put(KEY_WEIGHT, detail.getWeight());
            values.put(KEY_NAME, detail.getName());
            values.put(KEY_LASTNAME, detail.getLastName());
            values.put(KEY_GENDER, detail.getGender());
            values.put(KEY_AGE, detail.getAge());
    
            // updating row
            return db.update(TABLE_DETAILS, values, KEY_NAME, null);
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,239
    Rep Power
    23

    Default Re: Database fetching isn't working properly - Android

    What errors are you seeing?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. iterator not working properly
    By arieltal in forum Advanced Java
    Replies: 3
    Last Post: 09-29-2012, 01:01 PM
  2. Problem fetching data from database using JList
    By LearnJava82 in forum New To Java
    Replies: 3
    Last Post: 05-19-2012, 04:50 PM
  3. If statement is not working properly
    By Alpa in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 09:40 PM
  4. Why this block is not working properly?
    By juhiswt in forum New To Java
    Replies: 2
    Last Post: 03-13-2011, 12:08 PM
  5. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 01:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •