0

First to say i already did this with other object and other Table which works perfectly, after that i made this Helper class (which is almost same as previous) and for some reason when i try to "SELECT * FROM" table there is error that says the table does not exist. I am looking at code for 20 min trying to find error and i can't so i decided to ask you.

Here is Constants:

public static String DB_NAME = "database";
public static String DB_TODOS_TAB = "todos";
public static String DB_TODOS_CREATE_TAB = "CREATE TABLE IF NOT EXISTS " + DB_TODOS_TAB +
        "(id INTEGER PRIMARY KEY, name TEXT, importance INT DEFAULT 1, level INTEGER DEFAULT 1," +
        " reminder LONG DEFAULT 0, done INTEGER(1) DEFAULT 0)";
public static String DB_TODO_DROP = "DROP TABLE IF EXIST " + DB_TODOS_TAB;

Helper Class:

public class ToDoHelper extends SQLiteOpenHelper {

    public ToDoHelper(Context context) {
        super(context, DB_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_TODOS_CREATE_TAB);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL(DB_TODO_DROP);
        onCreate(db);
    }

    public ArrayList<ToDo> getNotDone() {
        ArrayList<ToDo> todos = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + DB_TODOS_TAB + " WHERE done = 0", null);

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        todos.add(new ToDo(cursor.getInt(0), cursor.getString(1), cursor.getInt(2), cursor.getInt(3),
                cursor.getInt(4), false));
    }
    cursor.close();
    return todos;
}...

When i create ToDoHelper in Activity onCreate method should make table but for soem reason when i try to get ArrayList there is error that says

no such table: todos (code 1): , while compiling: SELECT * FROM todos WHERE done = 0

Here is code in Activity:

ToDoHelper toDoHelper = new ToDoHelper(getBaseContext());
ArrayList<ToDo> toDos = toDoHelper.getNotDone();

So please help me if someone can notice where is error because i can not. I even compare code with working table and everything seams identical (ofc names of tables are different).

Yagami Light
  • 1,756
  • 4
  • 19
  • 39
Jovan0042
  • 21
  • 4

0 Answers0