在我的应用程序中,我使用了一个 SQLiteDBhelper
类(扩展扩展SqliteOpenHelper
),它覆盖了我的 onCreate、OnUpgrade 方法。我还需要创建一个DBManager
-class 以确保从其他类对我的数据库进行受控访问....
现在我想为数据库设置我的数据。此数据必须在我的应用程序第一次启动时创建一次。我如何确保数据只创建一次DBManager
?我想从DBManager
-class 调用我的设置 DB-Data 的方法来进行干净的软件设计....
这是来自的代码示例DBManager
:
public class DBManager {
private static final String TAG = DBManager.class.getSimpleName();
private DatabaseHelper helper;
private static Context myContext;
private static DBManager instance;
private DBManager() {
myContext = MyApplication.getAppContext();
if (myContext == null)
throw new RuntimeException("MyApplication returned null-Context");
helper = new DatabaseHelper(myContext);
}
public static void init() {
if (null == instance) {
instance = new DBManager();
}
}
public static DBManager getInstance() {
if (instance == null) {
instance = new DBManager();
}
return instance;
}
// Some Methods to add Data and Get Data form SQL-Database
// a Method, which creats my Objects for first App-Start; have to be called once
}