无法创建超过2列的android sql数据库

时间:2013-05-23 01:43:53

标签: android database

我尽可能多地搜索,但找不到任何帮助。我创建了一个显然超过2列的数据库。有没有人可以指点我来源帮助我。我也应该使用不同的数据库来提高性能。

我得到的错误信息是:

CursorWindow字段槽0,2的请求不好。 numRows = 3. numColumns = 2

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = String
            .format("CREATE TABLE %s (%s INTEGER PRIMARY KEY, %s INTEGER NOT NULL, %s TEXT NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s REAL NOT NULL, %s REAL NOT NULL, %s REAL NOT NULL, %s REAL NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL, %s INTEGER NOT NULL);",
                    EXERCISES_TABLE, COL_ID, EXERCISE_ID, NAME,
                    TYPE_OF_TRAINING, WEIGHT_BEST, SETS_BEST, REPS_BEST,
                    SETS, REPS, WEIGHT_GOAL, SETS_GOAL, REPS_GOAL,
                    DISTANCE, TIME, BEST_TIME, BEST_DISTANCE, BEST_DATE,
                    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
                    SATURDAY);

    db.execSQL(sql);
}

我试图检索数据的方式:

public List<Exercise> getExerciseList() {
    List<Exercise> retEx = new ArrayList<Exercise>();

    SQLiteDatabase db = getReadableDatabase();

    String sql = String.format("SELECT %s, %s FROM %s ORDER BY %s",
            EXERCISE_ID, SATURDAY, EXERCISES_TABLE, COL_ID);

    Cursor cursor = db.rawQuery(sql, null);

    while (cursor.moveToNext()) {
        int id = cursor.getInt(0);
        String name = cursor.getString(1);
        int typeOfTraining = cursor.getInt(2);
        int weightBest = cursor.getInt(3);
        int setsBest = cursor.getInt(4);
        int repsBest = cursor.getInt(5);
        int sets = cursor.getInt(6);
        int reps = cursor.getInt(7);
        int weightGoal = cursor.getInt(8);
        int setsGoal = cursor.getInt(9);
        int repsGoal = cursor.getInt(10);
        float distance = cursor.getFloat(11);
        float time = cursor.getFloat(12);
        float bestTime = cursor.getFloat(13);
        float bestDistance = cursor.getFloat(14);
        int bestDate = cursor.getInt(15);
        int sunday = cursor.getInt(16);
        int monday = cursor.getInt(17);
        int tuesday = cursor.getInt(18);
        int wednesday = cursor.getInt(19);
        int thursday = cursor.getInt(20);
        int friday = cursor.getInt(21);
        int saturday = cursor.getInt(22);

        retEx.add(new Exercise(name, weightBest, setsBest, repsBest, sets,
                reps, weightGoal, setsGoal, repsGoal, distance, time,
                bestTime, bestDistance, bestDate, typeOfTraining, id,
                sunday, monday, tuesday, wednesday, thursday, friday,
                saturday));
    }

    db.close();

    return retEx;
}

1 个答案:

答案 0 :(得分:2)

您正在成功填充大量数据库。

您选择的方式是个问题。

String sql = String.format("SELECT %s, %s FROM %s ORDER BY %s",
            EXERCISE_ID, SATURDAY, EXERCISES_TABLE, COL_ID);

这会像这样包装查询

SELECT EXERCISE_ID, SATURDAY FROM EXERCISES_TABLE ORDER BY COL_ID

您只选择了2列EXERCISE_ID和SATURDAY。 但是您希望您的结果包含所有列。

String sql = String.format("SELECT * FROM %s ORDER BY %s",
                 EXERCISES_TABLE, COL_ID);

这应该有用;]