I am trying to create a local database on an android phone using sqlite
.
I have a helper class, shown below, that is used to create the database and pro
oncreate() method is not a constructor and also Called when the database is created for the first time.So you have to call getWritableDatabase() or getReadableDatabase() for open or create to your database.
getReadableDatabase() :- Create and/or open a database.
getWritableDatabase() :- Create and/or open a database that will be used for reading and writing.
You have to call getWritableDatabase()
or getReadableDatabase()
.
The onCreate
is not a constructor for the database class. It is only called if you try to open a database that does not exist.
To open/create the database you need to add a call to one of these methods:
public class SmartApp extends Activity implements OnSharedPreferenceChangeListener {
private SmartDBHelper dBHelper;
public void onCreate(Bundle savedInstanceState) {
//where i am wanting to create the database and tables
dBHelper = new SmartDBHelper(getContext());
// open to read and write
dBHelper.getWritableDatabase();
// or to read only
// dBHelper.getReadableDatabase();
}
}
It is a bit big but here is my DatabaseAdapter you can take a look at: http://saintfeintcity.org/projects/sfc/repository/entry/trunk/src/org/saintfeintcity/database/GameDbAdapter.java