Android sqlite - 表中存在“没有这样的表”

时间:2013-11-17 19:07:49

标签: java android sqlite android-sqlite

我写了一个SQL查询来返回要显示给用户的项目 然而,它似乎有一个奇怪的问题。

这是错误:

11-17 13:44:08.644: E/AndroidRuntime(3229): Caused by: 
android.database.sqlite.SQLiteException: no such table: events (code 1): , while compiling: 

SELECT eventsmeta.*, events.* FROM eventsmeta JOIN 
(SELECT events.*, classes.* FROM events JOIN classes ON events.idfortype=classes._id)
 ON eventsmeta.eventid=events._id WHERE (eventsmeta.repeat_start = ?) OR 
(((? - eventsmeta.repeat_start) % (eventsmeta.repeat_day * 86400000)) = 0) OR 
(((? - eventsmeta.repeat_start) % (eventsmeta.repeat_week * 7 * 86400000)) = 0) OR 
((((12 - eventsmeta.repeat_start_month + ?) % (eventsmeta.repeat_month)) = 0) AND (eventsmeta.repeat_start_year <= ?)) 
ORDER BY events.startat ASC;

events确实存在(我用数据库查看器验证了它)。

如果是这样,我猜有一些语法问题......但我找不到它。

有关此问题的其他问题建议代码引用不同的数据库或不是最新的数据库。我不认为这是问题,因为我试图从头开始删除和重新创建数据库。我之前也使用过相同的代码模式,没有任何问题...

如果是这样,还有什么可能导致此问题?

CREATE TABLE代码

public static final String SQL_CREATE_EVENTS = 
    "CREATE TABLE IF NOT EXISTS " + Events.TABLE_NAME + " (" +
    Events._ID + " INTEGER PRIMARY KEY," +
    Events.COLUMN_NAME_EVENT_PRIORITY + " INTEGER," + //
    Events.COLUMN_NAME_EVENT_TITLE + " Text," +
    Events.COLUMN_NAME_START_AT + " INTEGER," +
    Events.COLUMN_NAME_END_AT + " INTEGER," +
    Events.COLUMN_NAME_EVENT_NOTES + " Text," +
    Events.COLUMN_NAME_EVENT_TYPE + " Text," +
    Events.COLUMN_NAME_ID_FOR_TYPE + " INTEGER);";

额外代码

SQLiteDatabase db = SchooLauncherDbHelper.getInstance(getActivity()).getReadableDatabase();

String sql = SQL QUERY HERE;

Cursor c = db.rawQuery(sql, new String[] {.............});

修改

显然,SELECT * FROM events完美无缺......

编辑2

SQLiteOpenHelper

public class SchooLauncherDbHelper extends SQLiteOpenHelper {

    static SchooLauncherDbHelper mInstance = null;
    static final int DATABASE_VERSION = 1;
    static final String DATABASE_NAME = "SchooLauncher.db";
    String query;
    String[] iconFileNames = {};

    public static SchooLauncherDbHelper getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new SchooLauncherDbHelper(
                    context.getApplicationContext());
        }

        return mInstance;
    }

    private SchooLauncherDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SchooLauncherContract.SQL_CREATE_SEMESTERS);
        db.execSQL(SchooLauncherContract.SQL_CREATE_STUDENTS);
        db.execSQL(SchooLauncherContract.SQL_CREATE_SUBJECTS);
        db.execSQL(SchooLauncherContract.SQL_CREATE_STUDENTSTUBJECTJOIN);
        db.execSQL(SchooLauncherContract.SQL_CREATE_TEACHERS);
        db.execSQL(SchooLauncherContract.SQL_CREATE_TEACHERSUBJECTJOIN);
        db.execSQL(SchooLauncherContract.SQL_CREATE_CLASSES);
        db.execSQL(SchooLauncherContract.SQL_CREATE_TEACHERCLASSJOIN);
        db.execSQL(SchooLauncherContract.SQL_CREATE_ASSIGNMENTS);
        db.execSQL(SchooLauncherContract.SQL_CREATE_EXAMS);
        db.execSQL(SchooLauncherContract.SQL_CREATE_EVENTS);
        db.execSQL(SchooLauncherContract.SQL_CREATE_EVENTS_META);
        db.execSQL(SchooLauncherContract.SQL_CREATE_SETTINGS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("onUpgrade()", "Upgrading from " + oldVersion + " to "
                + newVersion);

    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
}

3 个答案:

答案 0 :(得分:1)

发现了问题!

我所要做的就是为派生表创建一个别名,并使用它而不是events

SELECT eventsmeta.*, E1.* FROM eventsmeta JOIN 
(SELECT events.*, classes.* FROM events JOIN classes ON events.idfortype=classes._id) AS E1
 ON eventsmeta.eventid=E1._id WHERE (eventsmeta.repeat_start = ?) OR 
(((? - eventsmeta.repeat_start) % (eventsmeta.repeat_day * 86400000)) = 0) OR 
(((? - eventsmeta.repeat_start) % (eventsmeta.repeat_week * 7 * 86400000)) = 0) OR 
((((12 - eventsmeta.repeat_start_month + ?) % (eventsmeta.repeat_month)) = 0) AND (eventsmeta.repeat_start_year <= ?)) 
ORDER BY E1.startat ASC;

我认为问题是我无法从events表和新派生表中选择表eventsmeta,因为它不存在 THERE 。所以我不得不创建一个临时的新“表” - E1,并改为使用它。

答案 1 :(得分:1)

解决方案是先卸载应用程序,然后再次运行。

原因-您可能已经更改了SQLite类的@Override onCreate()方法。

我们无法通过更改onCreate代码来更改SQLite的表。那就是我的事。

**提示-**我们必须删除已创建的SQL数据库以直接进行更改,或者您可以使用sql代码。

答案 2 :(得分:0)

此问题有时会发生,解决方法是从设备中卸载应用程序,然后重新安装。