0

This is an extraction of my MainActivity in which i want to create a database table

public class GameDrive extends Activity {


DatabaseHelper databaseHelper;
SQLiteDatabase database;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_drive);
    getActionBar().setDisplayHomeAsUpEnabled(true);


    databaseHelper = new DatabaseHelper(this);
    database  = databaseHelper.getWritableDatabase();

. . .

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABSE_NAME = "database";
private static final String TABLE_NAME = "tripdata";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TYP = "typ";
private static final String COLUMN_VALUE = "value";

private static final int DATABASE_VERSION = 1;




public DatabaseHelper(Context context){
    super(context, DATABSE_NAME, null, DATABASE_VERSION);
    Log.d("Database","Constructor called");

}


//database is created for first time. creation of table and initial data inside done here
@Override
public void onCreate(SQLiteDatabase db) {
    Log.d("Database","onCreate called");

    db.execSQL("CREATE TABLE "+TABLE_NAME+"("+COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+COLUMN_TYP+" VARCHAR(255),"+COLUMN_VALUE+" VARCHAR(255));" );

    Log.d("Database","onCreate called succesfuly");

}

//drop tables, add tables etc.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    try {
        db.execSQL("DROP TABLE IF EXISTS TABLE_NAME");
        onCreate(db);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

Why is the Log.d("Database","onCreate called") never reached? After starting the app just the Log.d("Database","Constructor called") statement is reached.

Thank you!

d4rty
  • 3,970
  • 5
  • 34
  • 73

1 Answers1

0

I'm guessing the table is already there. So onDreate will not get executed again. Try installing SQLite Viewer to better visualize your DB. I also cannot see method getWritableDatabase() in your DatabaseHelper. Please also check on this.

Another thing you could try is modify your create table command into create table if not exists.

stuckedunderflow
  • 3,551
  • 8
  • 46
  • 63