我知道这已被问了一百万次。但是,我找不到在onUpgrade()
subclass
的方法
我已经在SQLite DataBase
中内置了SQLite Manager
,它可以正常使用。我希望用户在应用程序中创建一个额外的表。所以,据我所知,我必须在onUpgrade
这是我的代码:
String DB_PATH =null;
private static String DB_NAME = "CompositionFoodTable_LatinAmerica";
private SQLiteDatabase myDataBase;
private final Context myContext;
public FoodDataBaseHelper(Context context) {
super(context, DB_NAME, null, 2); //Constructor with newer version
//However I am not sure about this because when I run a query within the manager
//to check the version, it throws 0
this.myContext = context;
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
// I am not calling getReadableDatabase() or getWriteable(). They destroy my
//current version
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//NOT BEING CALLED
// Drop older plates table if existed
String CREATE_PRODUCT_TABLE = null;
try {
db.beginTransaction();
for (int i = oldVersion + 1; i <= newVersion; i++) {
CREATE_PRODUCT_TABLE = "CREATE TABLE IF NOT EXISTS PRODUCTS ( " +
"ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"NAME TEXT, " +
"DATE TEXT, " +
"SERVING_SIZE_G TEXT, " +
"H20_PERC TEXT, " +
"ENERGY_KCAL TEXT, " +
"PROTEIN_G TEXT, " +
"TOTAL_FAT_G TEXT, " +
"CARBS_G TEXT, " +
"TOT_DIET_FIBER_G TEXT, " +
"ASH_G TEXT, " +
"CALCIUM_MG TEXT, " +
"PHOSPHORUS_MG TEXT, " +
"IRON_MG TEXT, " +
"THIAMINE_MG TEXT, " +
"RIBOFLAVIN_MG TEXT, " +
"NIACIN_MG TEXT, " +
"VIT_C_MG TEXT, " +
"VIT_A_EQUIV_RETINOL_MCG TEXT, " +
"MUFA_G TEXT, " +
"PUFA_G TEXT, " +
"SATURATED_FATTY_ACIDS_G TEXT, " +
"CHOLESTEROL_MG TEXT, " +
"POTASSIUM_MG TEXT, " +
"SODIUM_MG TEXT, " +
"ZINC_MG TEXT, " +
"MAGNESIUM_MG TEXT, " +
"VIT_B6_MG TEXT, " +
"VIT_B12_MCG TEXT, " +
"FOLIC_AC_MCG TEXT, " +
"FOLATE_EQUIV_FD_MCG TEXT, " +
"EDIBLE_FRACTION_PERC TEXT)";
db.execSQL(CREATE_PRODUCT_TABLE);
// create plates table
db.setTransactionSuccessful();
}
} finally{
db.endTransaction();
}
// Future schema changes has to go into this loop
}
我的其他方法:
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are going to be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
boolean checkdb = false;
try{
String myPath = myContext.getFilesDir().getAbsolutePath().replace("files", "databases")+File.separator + DB_NAME;
File dbfile = new File(myPath);
checkdb = dbfile.exists();
}
catch(SQLiteException e){
System.out.println("Database doesn't exist");
}
return checkdb;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transferring bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create plate table
}
答案 0 :(得分:2)
但是,我找不到在我的子类
中调用onUpgrade()的方法
您不要自己致电onUpgrade()
。如果需要,SQLiteOpenHelper
会致电onUpgrade()
。
我已经在SQLite Manager中构建了一个正常运行的SQLite数据库。
如果您想在应用中打包SQLite数据库,请考虑switching to SQLiteAssetHelper
。
我希望用户在应用中创建一个额外的表。所以,据我所知,我必须在onUpgrade中执行此操作
没有。 onUpgrade()
适用于开发人员发布需要新数据库架构的应用程序的新版本。它不适用于表,索引或基于用户输入动态添加的任何其他内容。为此,请在execSQL()
上,在适当的时间点自己致电SQLiteDatabase
。