0

我在考虑 SQLite 插入时遇到问题。我有一个存储 10 个不同行的类。然后当用户单击一个按钮时,这些行应该被插入到数据库中,其中有一个 for 循环执行 10 次,然后应该完成。但是我遇到了一些奇怪的错误。
我使用了与其他项目相同的 SQLite 代码。

使用数据库:

           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();
                }
            }
        }

数据库类中的插入方法:

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);
}

这是错误

(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)
4

1 回答 1

1

set是一个 SQLite 关键字,将您的列名更改为其他名称,您的问题应该会消失。

于 2012-08-05T18:07:54.503 回答