我知道这个问题上有很多帖子,但我尝试了其中几个,但是当我调用getHomework()
方法时,我仍然会收到此错误。
Error:
android.database.sqlite.SQLiteException: no such table: homework (code 1): , while compiling: SELECT * FROM homework
DatabaseHelper
- 我认为这些方法的错误在于:
public void getHomework() {
SQLiteDatabase localSQLiteDatabase = getReadableDatabase();
String[] tblName = {};
Cursor localCursor = localSQLiteDatabase.rawQuery("SELECT * FROM homework",tblName);
localCursor.moveToFirst();
while (localCursor.isAfterLast() == false)
{
HashMap<String, String> homework = new HashMap<String, String>();
homework.put("Subject", localCursor.getString(0));
homework.put("DueDate", localCursor.getString(1));
net.attwoodthomas.mytimetable.app.FourthActivity.mHomeworkDue.add(homework);
localCursor.moveToNext();
}
}
public void getLessons()
{
SQLiteDatabase localSQLiteDatabase = getReadableDatabase();
String[] arrayOfString = new String[2];
arrayOfString[0] = MainActivity.mWeek;
arrayOfString[1] = MainActivity.mDay;
Cursor localCursor = localSQLiteDatabase.rawQuery("SELECT Period1, Period2, Period3, Period4, Period5, Period6 FROM lessons WHERE Week = ? AND day = ?", arrayOfString);
Log.d("DatabaseHelper", "1");
localCursor.moveToNext();
Log.d("DatabaseHelper", "2");
net.attwoodthomas.mytimetable.app.SecondActivity.period1 = localCursor.getString(localCursor.getColumnIndex("Period1"));
Log.d("DatabaseHelper", "3");
net.attwoodthomas.mytimetable.app.SecondActivity.period2 = localCursor.getString(localCursor.getColumnIndex("Period2"));
Log.d("DatabaseHelper", "4");
net.attwoodthomas.mytimetable.app.SecondActivity.period3 = localCursor.getString(localCursor.getColumnIndex("Period3"));
Log.d("DatabaseHelper", "5");
net.attwoodthomas.mytimetable.app.SecondActivity.period4 = localCursor.getString(localCursor.getColumnIndex("Period4"));
Log.d("DatabaseHelper", "6");
net.attwoodthomas.mytimetable.app.SecondActivity.period5 = localCursor.getString(localCursor.getColumnIndex("Period5"));
Log.d("DatabaseHelper", "7");
net.attwoodthomas.mytimetable.app.SecondActivity.period6 = localCursor.getString(localCursor.getColumnIndex("Period6"));
Log.d("DatabaseHelper", "8");
localCursor.close();
}
public void onCreate(SQLiteDatabase paramSQLiteDatabase)
{
paramSQLiteDatabase.execSQL("CREATE TABLE lessons (Week VARCHAR(1), Day VARCHAR(10), Period1 VACHAR(20),Period2 VARCHAR(20),Period3 VARCHAR(20),Period4 VARCHAR(20),Period5 VARCHAR(20),Period6 VARCHAR(20));");
paramSQLiteDatabase.execSQL("CREATE TABLE homework (Subject VARCHAR(50), DateDue VARCHAR(50), Description VARCHAR(100));");
if (paramSQLiteDatabase.rawQuery("SELECT * FROM lessons", null).moveToFirst())
{
Log.d("DatabaseHelper", "not emtpy");
return;
}
Log.d("DatabaseHelper", "emtpy");
paramSQLiteDatabase.execSQL("INSERT INTO lessons VALUES ('A', 'Monday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'), ('A', 'Tuesday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('A', 'Wednesday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('A', 'Thursday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('A', 'Friday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('B', 'Monday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('B', 'Tuesday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('B', 'Wednesday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'), ('B', 'Thursday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('B', 'Friday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music');");
paramSQLiteDatabase.execSQL("INSERT INTO homework VALUES ('Maths', '08.03.04', 'Finish p38');");
}
public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
{
paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS CREATE TABLE lessons (Week VARCHAR(1), Day VARCHAR(10), Period1 VACHAR(20),Period2 VARCHAR(20),Period3 VARCHAR(20),Period4 VARCHAR(20),Period5 VARCHAR(20),Period6 VARCHAR(20));");
paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS CREATE TABLE homework (Subject VARCHAR(50), DateDue VARCHAR(50), Description VARCHAR(100));");
onCreate(paramSQLiteDatabase);
}
整个项目可在github上找到:https://github.com/Ottermad/MyPlanner
如果我提供了太多/很少的信息,我很抱歉这是我的第一篇帖子。感谢
答案 0 :(得分:1)
OnUpgrade中的DROP TABLE IF EXISTS
查询不正确。
语法是:
DROP TABLE IF EXISTS <DATABASE_NAME>.<TABLE_NAME>
答案 1 :(得分:0)
1。更改数据库版本,然后启动应用程序..
OR
2。尝试卸载应用程序,然后安装..
“共享日志总是有助于在代码中找到问题......”