在预填充数据库Android中升级

在预填充数据库Android中升级,android,database,Android,Database,我在应用程序中使用预填充数据库,但当我在预填充数据库中添加新行并增加DB版本时,它不会复制新内容。要复制新内容,我必须重新安装应用程序 请帮助我解决这个问题,我想在数据库升级时复制新行 这是我的DBHelper.java package com.suvariyaraj.algorithms.Database; /** * Created by GOODBOY-PC on 04/06/16. */ import java.io.FileOutputStream; import java.io

我在应用程序中使用预填充数据库,但当我在预填充数据库中添加新行并增加DB版本时,它不会复制新内容。要复制新内容,我必须重新安装应用程序

请帮助我解决这个问题,我想在数据库升级时复制新行

这是我的DBHelper.java

package com.suvariyaraj.algorithms.Database;

/**
 * Created by GOODBOY-PC on 04/06/16.
 */
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class ExternalDbOpenHelper extends SQLiteOpenHelper {

    //Path to the device folder with databases
    public static String DB_PATH;

    //Database file name
    public static String DB_NAME;
    public SQLiteDatabase database;
    public final Context context;
    public static final int DB_VERSION = 1;

    public SQLiteDatabase getDb() {
        return database;
    }

    public ExternalDbOpenHelper(Context context, String databaseName) {
        super(context, databaseName, null, DB_VERSION);
        this.context = context;
        //Write a full path to the databases of your application
        String packageName = context.getPackageName();
        DB_PATH = String.format("//data//data//%s//databases//", packageName);
        DB_NAME = databaseName;
        openDataBase();
        Log.d("mycheck", "constructor "+database.getVersion());
    }

    //This piece of code will create a database if it’s not yet created
    public void createDataBase() {
        boolean dbExist = checkDataBase();
        if (!dbExist) {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                Log.e(this.getClass().toString(), "Copying error");
                throw new Error("Error copying database!");
            }
        } else {
            Log.i(this.getClass().toString(), "Database already exists");
        }
    }

    //Performing a database existence check
    private boolean checkDataBase() {
        SQLiteDatabase checkDb = null;
        try {
            String path = DB_PATH + DB_NAME;
            checkDb = SQLiteDatabase.openDatabase(path, null,
                    SQLiteDatabase.OPEN_READONLY);
        } catch (SQLException e) {
            Log.e(this.getClass().toString(), "Error while ch1ecking db");
        }
        //Android doesn’t like resource leaks, everything should
        // be closed
        if (checkDb != null) {
            checkDb.close();
        }
        return checkDb != null;
    }

    //Method for copying the database
    private void copyDataBase() throws IOException {
        //Open a stream for reading from our ready-made database
        //The stream source is located in the assets
        InputStream externalDbStream = context.getAssets().open(DB_NAME);

        //Path to the created empty database on your Android device
        String outFileName = DB_PATH + DB_NAME;

        //Now create a stream for writing the database byte by byte
        OutputStream localDbStream = new FileOutputStream(outFileName);

        //Copying the database
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = externalDbStream.read(buffer)) > 0) {
            localDbStream.write(buffer, 0, bytesRead);
        }
        //Don’t forget to close the streams
        localDbStream.close();
        externalDbStream.close();
    }

    public SQLiteDatabase openDataBase() throws SQLException {
        String path = DB_PATH + DB_NAME;
        if (database == null) {
            createDataBase();
            database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
        }
        return database;
    }

    @Override
    public synchronized void close() {
        if (database != null) {
            database.close();
        }
        super.close();
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("mycheck", "on create");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Toast.makeText(context,"Upgrade",Toast.LENGTH_SHORT).show();
        Log.d("mycheck", "Upgrade");
        context.deleteDatabase(DB_NAME);
        openDataBase();
    }
}

这是因为您没有在onUpgrade方法中添加更改。该方法用于修改/更新现有数据库。如果您在这里什么都不做,那么反映更改的唯一方式就是用户重新安装应用程序

onUpgrade:

当数据库需要升级时调用。实现应使用此方法删除表、添加表或执行升级到新架构版本所需的任何其他操作

例如:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < YOUR_NEW_DB_VERSION) {
     db.execSQL(YOUR_QUERY_TO_ADD_NEW_DATA_TO_EXISTING_TABLE);
}
}
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
如果(旧版本<您的新版本\u DB\u版本){
execSQL(您的查询添加到现有表);
}
}

如果您在使用onUpgrade方面需要更多帮助,也可以查看此文档。希望这有帮助

这是因为您没有在onUpgrade方法中添加更改。该方法用于修改/更新现有数据库。如果您在这里什么都不做,那么反映更改的唯一方式就是用户重新安装应用程序

onUpgrade:

当数据库需要升级时调用。实现应使用此方法删除表、添加表或执行升级到新架构版本所需的任何其他操作

例如:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < YOUR_NEW_DB_VERSION) {
     db.execSQL(YOUR_QUERY_TO_ADD_NEW_DATA_TO_EXISTING_TABLE);
}
}
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
如果(旧版本<您的新版本\u DB\u版本){
execSQL(您的查询添加到现有表);
}
}

如果您在使用onUpgrade方面需要更多帮助,也可以查看此文档。希望这有帮助

很抱歉,我忘记添加这段代码,但我尝试了它,但它不起作用。您能给我onUpgrade的确切代码吗?我不知道您试图将哪些数据/行添加到数据库中的哪个表中。如果您在编写查询以将新行插入现有数据库时遇到问题,只需遵循本文中的格式,一旦您有了查询,只需在我在文章中提供的示例中替换它。抱歉,我忘记添加这段代码,但我尝试了它,但它不起作用。您能给我onUpgrade的确切代码吗?我不知道您试图将哪些数据/行添加到数据库中的哪个表中。如果您在编写查询以将新行插入现有数据库时遇到问题,只需遵循本文中的格式,一旦您有了查询,只需在我的文章中提供的示例中替换它即可