无法在android

时间:2017-11-05 15:46:22

标签: android sqlite

所以我试图在android中创建一组SQLite表,这样我就可以存储我的数据,将其拉出来,操纵它并将其推回去。

我遇到的错误是:

android.database.sqlite.SQLiteException: no such table: units (code 1): , while compiling: SELECT * FROM units
         at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
         at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
         at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
         at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
         at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
         at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
         at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
         at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
         at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
         at au.edu.murdoch.ict376.universityquest.helpers.DBHelper.getAllUnits(DBHelper.java:787)
         at au.edu.murdoch.ict376.universityquest.fragments.SettingsFragment.onCreateView(SettingsFragment.java:54)
         at android.app.Fragment.performCreateView(Fragment.java:2353)
         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:995)
         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
         at android.app.BackStackRecord.run(BackStackRecord.java:816)
         at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
         at android.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
         at android.os.Handler.handleCallback(Handler.java:751)
         at android.os.Handler.dispatchMessage(Handler.java:95)
         at android.os.Looper.loop(Looper.java:154)
         at android.app.ActivityThread.main(ActivityThread.java:6119)
         at java.lang.reflect.Method.invoke(Native Method)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我已经尝试了一切来尝试修复它。我无法解决我出错的地方。

这是我的DBHelper onCreate代码:

public class DBHelper extends SQLiteOpenHelper {
    // Array divider
    private static final String ARRAY_DIVIDER = "vyo6vj50fdbv";

    // The database name
    private static final String DATABASE_NAME = "UniQuest.db";

    // The table "notes"
    private static final String NOTES_TABLE_NAME = "notes";
    private static final String NOTES_COLUMN_ID = "id";
    private static final String NOTES_COLUMN_TITLE = "title";
    private static final String NOTES_COLUMN_DATE = "date";
    private static final String NOTES_COLUMN_TEXT = "text";

    // The table "tasks"
    private static final String TASKS_TABLE_NAME = "tasks";
    private static final String TASKS_COLUMN_ID = "id";
    private static final String TASKS_COLUMN_TITLE = "title";
    private static final String TASKS_COLUMN_DATE = "date";
    private static final String TASKS_COLUMN_TEXT = "text";
    private static final String TASKS_COLUMN_COMPLETED = "completed";

    // The table "assignments"
    private static final String ASSIGNMENTS_TABLE_NAME = "assignments";
    private static final String ASSIGNMENTS_COLUMN_ID = "id";
    private static final String ASSIGNMENTS_COLUMN_TITLE = "title";
    private static final String ASSIGNMENTS_COLUMN_DUEDATE = "duedate";
    private static final String ASSIGNMENTS_COLUMN_TASKS = "tasks";
    private static final String ASSIGNMENTS_COLUMN_COMPLETED = "completed";

    // The table "achievements"
    private static final String ACHIEVEMENTS_TABLE_NAME = "notes";
    private static final String ACHIEVEMENTS_COLUMN_ID = "id";
    private static final String ACHIEVEMENTS_COLUMN_TITLE = "title";
    private static final String ACHIEVEMENTS_COLUMN_CRITERIA = "criteria";
    private static final String ACHIEVEMENTS_COLUMN_COMPLETED = "completed";

    // The table "classes"
    private static final String TUTES_TABLE_NAME = "classes";
    private static final String TUTES_COLUMN_ID = "id";
    private static final String TUTES_COLUMN_TITLE = "title";
    private static final String TUTES_COLUMN_DAY = "day";
    private static final String TUTES_COLUMN_TIME = "time";
    private static final String TUTES_COLUMN_TYPE = "type";
    private static final String TUTES_COLUMN_LOCATION = "location";

    // The table "grades"
    private static final String GRADES_TABLE_NAME = "grades";
    private static final String GRADES_COLUMN_ID = "id";
    private static final String GRADES_COLUMN_UNIT = "unit";
    private static final String GRADES_COLUMN_GRADE = "grade";

    // The table "units"
    private static final String UNITS_TABLE_NAME = "units";
    private static final String UNITS_COLUMN_ID = "id";
    private static final String UNITS_COLUMN_UNIT = "unit";
    private static final String UNITS_COLUMN_MARKID = "markid";

    // The table "marks"
    private static final String MARKS_TABLE_NAME = "marks";
    private static final String MARKS_COLUMN_ID = "id";
    private static final String MARKS_COLUMN_TASK = "task";
    private static final String MARKS_COLUMN_TYPE = "type";
    private static final String MARKS_COLUMN_WEIGHT = "weight";
    private static final String MARKS_COLUMN_GRADE = "grade";
    private static final String MARKS_COLUMN_UNITID = "unitid";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        // Creating the tables
        db.execSQL(
                "create table " + NOTES_TABLE_NAME + "(" +
                        NOTES_COLUMN_ID + " integer primary key autoincrement, " +
                        NOTES_COLUMN_TITLE + " text, " +
                        NOTES_COLUMN_DATE + " text, " +
                        NOTES_COLUMN_TEXT + " text);" +

                        "create table " + TASKS_TABLE_NAME + "(" +
                        TASKS_COLUMN_ID + " integer primary key autoincrement, " +
                        TASKS_COLUMN_TITLE + " text, " +
                        TASKS_COLUMN_DATE + " text, " +
                        TASKS_COLUMN_TEXT + " text, " +
                        TASKS_COLUMN_COMPLETED + " text);" +

                        "create table " + ASSIGNMENTS_TABLE_NAME + "(" +
                        ASSIGNMENTS_COLUMN_ID + " integer primary key autoincrement, " +
                        ASSIGNMENTS_COLUMN_TITLE + " text, " +
                        ASSIGNMENTS_COLUMN_DUEDATE + " text, " +
                        ASSIGNMENTS_COLUMN_TASKS + " text, " +
                        ASSIGNMENTS_COLUMN_COMPLETED + " text);" +

                        "create table " + ACHIEVEMENTS_TABLE_NAME + "(" +
                        ACHIEVEMENTS_COLUMN_ID + " integer primary key autoincrement, " +
                        ACHIEVEMENTS_COLUMN_TITLE + " text, " +
                        ACHIEVEMENTS_COLUMN_CRITERIA + " text, " +
                        ACHIEVEMENTS_COLUMN_COMPLETED + " text);" +

                        "create table " + TUTES_TABLE_NAME + "(" +
                        TUTES_COLUMN_ID + " integer primary key autoincrement, " +
                        TUTES_COLUMN_TITLE + " text, " +
                        TUTES_COLUMN_DAY + " text, " +
                        TUTES_COLUMN_TIME + " text, " +
                        TUTES_COLUMN_TYPE + " text, " +
                        TUTES_COLUMN_LOCATION + " text);" +

                        "create table " + GRADES_TABLE_NAME + "(" +
                        GRADES_COLUMN_ID + " integer primary key autoincrement, " +
                        GRADES_COLUMN_UNIT + " text, " +
                        GRADES_COLUMN_GRADE + " text);" +

                        "create table " + UNITS_TABLE_NAME + "(" +
                        UNITS_COLUMN_ID + " integer primary key autoincrement, " +
                        UNITS_COLUMN_UNIT + " text, " +
                        UNITS_COLUMN_MARKID + " text);" +

                        "create table " + MARKS_TABLE_NAME + "(" +
                        MARKS_COLUMN_ID + " integer primary key autoincrement, " +
                        MARKS_COLUMN_TASK + " text, " +
                        MARKS_COLUMN_TYPE + " text, " +
                        MARKS_COLUMN_WEIGHT + " text, " +
                        MARKS_COLUMN_GRADE + " text, " +
                        MARKS_COLUMN_UNITID + " text);"
        );
    }

这是我试图在我的片段中调用的方法:

public List<Unit> getAllUnits() {
        List<Unit> units = new LinkedList<Unit>();

        String query = "SELECT * FROM " + UNITS_TABLE_NAME;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        Unit unit = null;
        if (cursor.moveToFirst()) {
            do {
                unit = new Unit();
                unit.setId(Integer.parseInt(cursor.getString(0)));
                unit.setUnit(cursor.getString(1));
                unit.setMarks(Integer.parseInt(cursor.getString(2)));

                units.add(unit);
            } while (cursor.moveToNext());
        }

        return units;
    }

我无法弄清楚为什么桌子没有创造,我已经倾倒了一个小时。

1 个答案:

答案 0 :(得分:1)

execSQL()只运行一个查询,在输入结束时停止或首先;

;分隔的查询拆分为多个execSQL()来电。您可以卸载应用程序以擦除其数据,以便再次运行onCreate()