简体   繁体   中英

Unable to save data to SQLite database

I'm developing a fitness tracker android app and have set up a SQLite database with 2 tables successfully. One table is for saving data about cardio exercises and one table is for saving data about weight lifting exercises. I'm able to save information to the cardio table (saved_workout_cardio) however, I can't seem to save anything to the weight lifting table (saved_workout_weights). I started off by coding the functionality for saving to 'saved_workout_cardio' table and once I had successfully got that working, I just copy and pasted the code and edited it to fit what was needed for saving to 'saved_workout_weights' table, however unfortunately it's not working and I'm puzzled as to why.

Below is the code from my databasehelper java class where I create the database as well as 2 methods, 1 for saving data to each table:

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "FitnessTracker.db";
public static final String TABLE_WEIGHTS = "saved_workout_weights";
public static final String COL_1 = "ID";
public static final String COL_2 = "DATE";
public static final String COL_3 = "EXERCISE";
public static final String COL_4 = "WEIGHT";
public static final String COL_5 = "REPS";
public static final String COL_6 = "SETS";

public static final String TABLE_CARDIO = "saved_workout_cardio";
public static final String COL_1a = "ID";
public static final String COL_2a = "DATE";
public static final String COL_3a = "TIME";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_WEIGHTS + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,DATE TEXT,EXERCISE TEXT,WEIGHT INTEGER,REPS INTEGER)");
    db.execSQL("create table " + TABLE_CARDIO + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,DATE TEXT,TIME INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_WEIGHTS);
    db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_CARDIO);

    onCreate(db);
}

public boolean saveData (String date, String time) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2a, date);
    contentValues.put(COL_3a, time);
    long result = db.insert(TABLE_CARDIO, null, contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

public boolean saveData2 (String date, String exercise, String weight, String reps, String sets) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues2 = new ContentValues();
    contentValues2.put(COL_2, date);
    contentValues2.put(COL_3, exercise);
    contentValues2.put(COL_4, weight);
    contentValues2.put(COL_5, reps);
    contentValues2.put(COL_6, sets);
    long result = db.insert(TABLE_WEIGHTS, null, contentValues2);
    if(result == -1)
        return false;
    else
        return true;
}

}

Now here's the relevant code from my 'Cardio' java class where I'm calling the method for saving data to the cardio table (this method works and I'm able to save data to the cardio table successfully:

public void saveCardioData () {
    btnSaveCardio.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isInserted = myDb.saveData(txtDate.getText().toString(),
                            txtTimer.getText().toString() );
                    if(isInserted = true)
                        Toast.makeText(Cardio.this, "Workout saved", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(Cardio.this, "Error saving workout", Toast.LENGTH_LONG).show();
                }
            }
    );
}

Now here's the 'weights' java class where I call the method for saving data to the weights table. As you can see I have set up a toast message to appear if data is successfully inserted. Whenever I click to save the workout in my app the toast message tells me that the workout has been successfully saved however, when I check the weights table in my database no values have been saved.

public void saveWeightsData () {
    btnSaveWeights.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isInserted = myDb.saveData2(exerciseVal.getText().toString(),
                            weightVal.getText().toString(),
                            repVal.getText().toString(),
                            setVal.getText().toString(),
                            dateVal.getText().toString() );
                    if(isInserted = true)
                        Toast.makeText(WeightsMain.this, "Workout saved", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(WeightsMain.this, "Error saving workout", Toast.LENGTH_LONG).show();
                }
            }
    );
}

Really confused as to why I'm having this problem, so if anyone can tell me why it would be greatly appreciated! :)

EDIT: I'm using Android Device Monitor within Android studio to pull the database from the android emulator's storage to my pc. I then use SQLite Manager firefox addon to check the 2 tables.

There's no SETS column in your table but you're trying to insert data to it. That won't work.

After adding the SETS column to the CREATE TABLE , you can uninstall your app to force onCreate() to execute again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM