1

I keep hitting the 'no such table' error for this. I use two very similar database helper classes. The first one works fine with the same implementation, the second one always crashes the app saying that there is no such table! Here is the code for the first table being implemented:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = new DatabaseHelper(this);
    workoutsCursor = db
            .getReadableDatabase()
            .rawQuery("SELECT _ID, title " +
                            "FROM workouts ORDER BY title",
                    null
            );
    adapter = new SimpleCursorAdapter(this,
            R.layout.list_view_item, workoutsCursor,
            new String[]{DatabaseHelper.TITLE},
            new int[]{R.id.textViewItem}, 0);

    setListAdapter(adapter);
    registerForContextMenu(getListView());

}

And the overridden onCreate of that db helper:

@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE workouts (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT);");
}

Compare this to the implementation of the second table in the second activity along with its respective dbhelper's onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nIntent = getIntent();
    PID = nIntent.getIntExtra("PID", 0);
    String eyedee = Integer.toString(PID);
    try {
        db = new ExerciseDBHelper(this);
        exerciseCursor = db
                .getReadableDatabase()
                .rawQuery("SELECT _ID, title " +
                                "FROM exercises WHERE pid = " + eyedee,
                        null
                );
    } catch (Exception e){
        Log.d("MAKEDB", "that didnt work bruh");
    }

    adapter = new SimpleCursorAdapter(this,
            R.layout.list_view_item, exerciseCursor,
            new String[]{ExerciseDBHelper.TITLE},
            new int[]{R.id.textViewItem}, 0);

    setListAdapter(adapter);
    registerForContextMenu(getListView());

and

@Override
public void onCreate(SQLiteDatabase db){

    db.execSQL("CREATE TABLE exercises (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, pid INTEGER);");
}

If it helps: This worked as it is before I uninstalled the app and reinstalled it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Zolnoor
  • 699
  • 6
  • 11

1 Answers1

0

By using the same DatabaseHelper I was able to solve the problem. The new code for my databasehelper:

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME="db";
static final String TITLE="title";
static final String TABLE1="CREATE TABLE IF NOT EXISTS workouts (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT);";
static final String TABLE2="CREATE TABLE IF NOT EXISTS exercises (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, pid INTEGER);";
static final String PID="pid";


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

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

@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
    android.util.Log.v("Workouts", "Upgrading database, which will destroy old date");
    db.execSQL("DROP TABLE IF EXISTS workouts");
    db.execSQL("DROP TABLE IF EXISTS exercises");
    onCreate(db);
}

}

Shoutout to @joao2fast4u for the suggestion!

Zolnoor
  • 699
  • 6
  • 11