0

I know there are many posts on this question but I have tried several of them but I still get this error when I call the getHomework() method.

Error:
android.database.sqlite.SQLiteException: no such table: homework (code 1): , while compiling: SELECT * FROM homework

DatabaseHelper - These are the methods were I think the error lies:

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

The entire project is available on github: https://github.com/Ottermad/MyPlanner

I am sorry if I have provided too much/little infomation this is one of my first posts. Thanks

4
  • Uninstall your app to make your helper onCreate() run again. stackoverflow.com/questions/21881992/… Commented Aug 5, 2014 at 10:45
  • Change your database name, it should work. Commented Aug 5, 2014 at 11:49
  • Please show full logcat Commented Aug 5, 2014 at 11:57
  • Fixed it by making new version of project thanks Commented Aug 5, 2014 at 14:51

2 Answers 2

1

Your DROP TABLE IF EXISTS query in OnUpgrade is incorrect.

Syntax is :

DROP TABLE IF EXISTS <DATABASE_NAME>.<TABLE_NAME>
Sign up to request clarification or add additional context in comments.

Comments

0

1 . Change database version, and then start app..

            OR

2. Try uninstalling app and then instal..

"Sharing logs always helps to find problem in your code..."

1 Comment

Tried changing db version but I have got this error: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 1: Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.