0

I'm novice in Android and I trying to create BD sqlite with API. My class exted SQLiteHelper, the constructor method is OK but the create method not is call. No error in compilation. My doubt not is when onCreate or onUpdate events are calls.

If I do 'wipe data' in VM, BD not is created. If I put new version (from 1 to 2, from 2 to 3,...), BD not is created. In Android Studio->Device Monitor->File Explorer, in app folder the BD and the folder not exist.

MainActivity.java code:

public class MainActivity extends ActionBarActivity {
    private DBTesteHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        dbHelper = new DBTesteHelper(this);
        setContentView(R.layout.activity_main);
    }
}

DBTesteHelper.java code:

public class DBTesteHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "bd_teste.db";
private static final int DATABASE_VERSION = 1;


public DBTesteHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE_STUDENT = "CREATE TABLE tb_estudante ("
            + " id INTEGER PRIMARY KEY AUTOINCREMENT ,"
            + " name TEXT, "
            + " idade INTEGER, "
            + " email TEXT )";

    db.execSQL(CREATE_TABLE_STUDENT);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS tb_estudante");
    onCreate(db);
    }
}
AirtonCarneiro
  • 132
  • 1
  • 8
  • 1
    The onCreate method is called only on first time when you create DB if you want to call it one more time, you have to clear application data – Konrad Krakowiak Mar 08 '15 at 20:33
  • The answer I was look was: "Create a helper object to create, open, and/or manage a database. The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called." The database is created when getWritable ou getReadable is userd. link: http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html – AirtonCarneiro Mar 25 '15 at 12:28

0 Answers0