0

I have a problem considering SQLite insert. I have a class which stores 10 different rows. These rows should then be inserted to the database when the user clicks a button, where there is a for-loop that goes 10 times and then it should be done. But im getting some weird errors.
I have used the same kind of SQLite code that I have used for other projects.

Using db:

           Log.v(LOG_TAG, "\n\n\nSave Clicked");
           for (int i = 0; i < 10; i++) {

            dataSpinner[i] = spinnerList.get(i).getSelectedItem().toString(); // Area
            dataExercise[i] = exerciseList.get(i).getText().toString(); // Exercise
            dataSet[i] = setList.get(i).getText().toString(); // Set
            dataRep[i] = repList.get(i).getText().toString(); // Rep

            // Info box
            // Might not need anything here?

            Log.v(LOG_SAVE,
                    i + ". area: " + dataSpinner[i].toString() + ", ex: "
                            + dataExercise[i].toString() + ", set:"
                            + dataSet[i].toString() + ", rep:"
                            + dataRep[i].toString());

            // Save the current row to db
            boolean diditWork = true;
            try {
                ScheduleDatabase entry = new ScheduleDatabase(Monday.this);
                entry.open();
                entry.createEntry(table, dataSpinner[i].toString(),
                        dataExercise[i].toString(), dataSet[i].toString(),
                        dataRep[i].toString());
                entry.close();
            } catch (Exception e) {
                diditWork = false;
                String error = e.toString();
                Toast.makeText(this, "Something went wrong, " + error,
                        Toast.LENGTH_SHORT);
            } finally {
                if (diditWork) {
                    Toast.makeText(this, "IT WORKED!!!!!!",
                            Toast.LENGTH_SHORT).show();
                }
            }
        }

Insert method in database class:

public long createEntry(String table, String area, String exercise,
        String set, String rep) throws SQLException {
    System.out.println("Values: " + table + ", " + area + ", " + exercise
            + ", " + set + ", " + rep);
    ContentValues cv = new ContentValues();
    cv.put(KEY_AREA, area);
    cv.put(KEY_EXERCISE, exercise);
    cv.put(KEY_SET, set);
    cv.put(KEY_REP, rep);
    System.out.println("Done putting");
    return ourDatabase.insert(table, null, cv);
}

And here is the error

(1) near "set": syntax error<br>
Error inserting exercise=Exercise: 19 area=Abdominals set=5 rep=10
android.database.sqlite.SQLiteException: near "set": syntax error (code 1): , while compiling: INSERT INTO monday(exercise,area,set,rep) VALUES (?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.trainingschedule.days.ScheduleDatabase.createEntry(ScheduleDatabase.java:124)
at com.trainingschedule.days.Monday.onClick(Monday.java:262)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

1 Answer 1

1

set is an SQLite keyword, change your column name to something else and your issue should go away.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot :D This really fixed my problem, have been sitting with it for 2 hours now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.