Reputation: 36237
I am currently developing an application on android.
I would like a class which creates the database and tables in a separated from the Activity screen.
public void createDatabase()
{
try
{
SQLiteDatabase myDB;
myDB = this.openOrCreateDatabase("PasswordManager", Context.MODE_PRIVATE, null);
myDB.execSQL("CREATE TABLE IF NOT EXISTS `users` (`use_userID` int(11) NOT NULL, `use_username` varchar(25) DEFAULT NULL," +
"`use_usePassword` char(1) DEFAULT NULL, `use_password` varchar(150) DEFAULT NULL, PRIMARY KEY (`use_userID`))");
myDB.execSQL("CREATE TABLE IF NOT EXISTS `password` (`pas_loginID` int(11) NOT NULL, `pas_userID` int(11) DEFAULT NULL," +
"`pas_company` varchar(50) NOT NULL, `pas_companyURL` varchar(250) DEFAULT NULL, `pas_username` " +
"varchar(150) NOT NULL, `pas_password` varchar(150) NOT NULL, `pas_type` varchar(50) NOT NULL, " +
"PRIMARY KEY (`pas_loginID`))");
}
catch (SQLiteException sqlEx)
{
Log.d("Database Error", sqlEx.toString());
}
catch (Exception ex)
{
Log.d("General Error", ex.toString());
}
}
The class isn't activity i.e. it is not showing a screen, it is just going to do certain methods which will need to happen regularly so I haven't got to repeat code. How can I do this. It keeps on coming up with a Null Pointer Exception
Upvotes: 0
Views: 121
Reputation: 2898
Presume you have a class (which is not visible from the code snippet). If you have a class, just create an instance in the Activity class and call the method.
snippet as follows.
public class MyDBHelper extends SQLiteOpenHelper {
public void createDatabase() { ......}
.....
}
public class MyActivity extends Activity {
private MyDBHelper mDBHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
........
mDBHelper.createDatabase();
}
............
}
Upvotes: 2