我将dagger2
库用于我的应用程序。然后我使用android sqlite
来存储数据。问题是,当我尝试升级数据库时,数据库版本不会更改。我的逻辑有问题吗?
这是我的AppModule
课:
public class AppModule {
@Provides
@Singleton
Context provideContext(Application application) {
return application;
}
@Provides
@Singleton
ApiHelper provideApiHelper(AppApiHelper appApiHelper) {
return appApiHelper;
}
@Provides
@Singleton
DataManager provideDataManager(AppDataManager appDataManager) {
return appDataManager;
}
@Provides
@DatabaseInfo
String provideDatabaseName() {
return AppConstants.DB_NAME;
}
@Provides
@DatabaseInfo
Integer provideDatabaseVersion(PreferencesHelper preferencesHelper) {
return preferencesHelper.getVersion();
}
@Provides
@Singleton
Gson provideGson() {
return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
}
@Provides
@PreferenceInfo
String providePreferenceName() {
return AppConstants.PREF_NAME;
}
@Provides
@Singleton
PreferencesHelper providePreferencesHelper(AppPreferencesHelper appPreferencesHelper) {
return appPreferencesHelper;
}
@Provides
SchedulerProvider provideSchedulerProvider() {
return new AppSchedulerProvider();
}
}
这是我的Datamanager
课:
public class AppDataManager implements DataManager {
private final ApiHelper mApiHelper;
private final Context mContext;
private final Gson mGson;
private final PreferencesHelper mPreferencesHelper;
private DatabaseSqlite mDatabaseSqlite;
private Insert mInsert;
private Select mSelect;
@Inject
public AppDataManager(Context context, PreferencesHelper preferencesHelper, ApiHelper apiHelper, Gson gson, DatabaseSqlite databaseSqlite,
Insert insert, Select select) {
mContext = context;
mPreferencesHelper = preferencesHelper;
mGson = gson;
mApiHelper = apiHelper;
mDatabaseSqlite = databaseSqlite;
mInsert = insert;
mSelect = select;
}
@Override
public DatabaseSqlite setDatabaseSqlite(Context context, int Version, ArrayList<Lst_App_version_Log> app_version_log) {
mDatabaseSqlite = new DatabaseSqlite(context, Version, app_version_log);
return mDatabaseSqlite;
} // I try to pass data into costructor my Database Sqlite
}
这是我的DatabaseSqlite
课:
public class DatabaseSqlite extends SQLiteOpenHelper {
private static final String TAG = "DatabaseSqlite";
private Context context;
private ArrayList<Log_version> mlogVersion = null;
@Inject
public DatabaseSqlite(Context context,
@DatabaseInfo String dbName,
@DatabaseInfo Integer version) {
super(context, dbName, null, version);
}
public DatabaseSqlite(Context context, int version, ArrayList<Log_version> version_log) {
super(context, DB_NAME, null, version);
this.mlog_version = version_log;
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
if (app_version_log != null) {
for (Log_version dataUpdateModel : Log_version) {
Log.e(TAG, "onCreate: " + dataUpdateModel.getId());
db.execSQL(dataUpdateModel.getQuer());
}
Log.e(TAG, "onCreate: Finish");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < newVersion) {
onCreate(db);
}
}
这就是我尝试更新数据库版本的方式:
getDataManager().setVersion(itemVersionResponse.getVersion());
getDataManager().setDatabaseSqlite(context, itemVersionResponse.getVersion(), dataUpdateModels);
是否存在某些问题,导致我的数据库版本未升级?请我希望有人能告诉我我的错误在哪里。非常感谢你