在sqliteopenhelper中没有被求职

发布于 2025-01-26 00:41:00 字数 3736 浏览 2 评论 0原文

方法onupgrade即使它获得的数据库版本大于当前版本,也不会被调用。

我使用以下命令验证了DB版本,目前是 6

sqlite> PRAGMA user_version;
6

在构造函数中,我要传递以下(版本7 ),

super(context, DB_NAME, null, 7);

注意:我的类dbhelper扩展了sqliteopenhelper,上面的行中存在于dbhelper的构造函数中。

sqlite版本:3.32.3

当我传递新的(更大的)DB版本时,我希望可以调用Onupgrade。但是从来没有称呼upgrade。我想念其他东西吗?

更新1:

dbhelper.class

class DBHelper extends SQLiteOpenHelper
{
    @Inject
    public DBHelper(final Context context)
    {
        this(context, DBContracts.DATABASE_FILE_NAME);
    }

    DBHelper(final Context context, final String databaseFileName)
    {
        super(context, databaseFileName, null, DBContracts.DATABASE_VERSION);
        Logger.i("APPTAG", "DBHelper constructor");
        Logger.i("APPTAG", "DATABASE_VERSION : " + DBContracts.DATABASE_VERSION);
    }

    @Override
    public void onCreate(final SQLiteDatabase db)
    {
        Logger.i("APPTAG", "onCreate");
        // create scripts
    }

    @Override
    public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion)
    {
        Logger.i("APPTAG", "onUpgrade");
        Logger.i("APPTAG", "oldVersion: " + oldVersion);
        Logger.i("APPTAG", "newVersion: " + newVersion);
        // upgrade scripts
    }

    @Override
    public void onConfigure(final SQLiteDatabase db)
    {
        Logger.i("APPTAG", "onConfigure");
        Logger.i("APPTAG", "DB version: " + db.getVersion());

        try {
            Logger.i("APPTAG", "DB path: " + db.getPath());
            int version = ((Long) DatabaseUtils.longForQuery(db, "PRAGMA user_version;", null)).intValue();
            Logger.i("APPTAG", "onConfigure version: " + version);
        } catch (Exception e) {
            Logger.e("APPTAG", "Error" + e, e);
        }

        super.onConfigure(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Logger.i("APPTAG", "onDowngrade");
        Logger.i("APPTAG", "oldVersion: " + oldVersion);
        Logger.i("APPTAG", "newVersion: " + newVersion);
        super.onDowngrade(db, oldVersion, newVersion);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        Logger.i("APPTAG", "onOpen");
        Logger.i("APPTAG", "DB version: " + db.getVersion());
        super.onOpen(db);
    }
}

,这是我看到的日志,

05-04 07:47:33.021  4295  4295 I APPTAG: DBHelper constructor
05-04 07:47:33.021  4295  4295 I APPTAG: DATABASE_VERSION : 7
05-04 07:47:33.023  4295  4295 I APPTAG: UpdatesDB constructor
05-04 07:47:33.023  4295  4295 I APPTAG: SQLiteDatabaseWrapperFactory create
05-04 07:47:33.029  4295  4295 I APPTAG: onConfigure
05-04 07:47:33.030  4295  4295 I APPTAG: DB version: 7
05-04 07:47:33.030  4295  4295 I APPTAG: DB path: /data/user/0/<application-name>/databases/updates.db
05-04 07:47:33.030  4295  4295 I APPTAG: onConfigure version: 7
05-04 07:47:33.031  4295  4295 I APPTAG: onOpen
05-04 07:47:33.031  4295  4295 I APPTAG: DB version: 7

如果我直接在数据库上执行此命令pragma user_version;,我将其版本为6。但是,当我尝试使用代码(如在configure中)时,它给了我7。因此,它会根据此代码

谁能帮助我理解为什么我通过代码获取DB版本为7?

澄清:

  • 我正在调用getWritabledatabase()获取DB实例。
  • dbontracts.database_version设置为7

The method onUpgrade is not getting called even if it gets the DB version greater than the current.

I verified the DB version using the following command and it is 6 currently.

sqlite> PRAGMA user_version;
6

In the constructor, I'm passing the following (version 7),

super(context, DB_NAME, null, 7);

Note: My class DBHelper extends SQLiteOpenHelper and the above line is present in the constructor of DBHelper.

SQLite version: 3.32.3

As I'm passing the new (greater) DB version, I expect the onUpgrade to be called. But the onUpgrade is never called. Am I missing any other thing?

UPDATE 1:

DBHelper.class

class DBHelper extends SQLiteOpenHelper
{
    @Inject
    public DBHelper(final Context context)
    {
        this(context, DBContracts.DATABASE_FILE_NAME);
    }

    DBHelper(final Context context, final String databaseFileName)
    {
        super(context, databaseFileName, null, DBContracts.DATABASE_VERSION);
        Logger.i("APPTAG", "DBHelper constructor");
        Logger.i("APPTAG", "DATABASE_VERSION : " + DBContracts.DATABASE_VERSION);
    }

    @Override
    public void onCreate(final SQLiteDatabase db)
    {
        Logger.i("APPTAG", "onCreate");
        // create scripts
    }

    @Override
    public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion)
    {
        Logger.i("APPTAG", "onUpgrade");
        Logger.i("APPTAG", "oldVersion: " + oldVersion);
        Logger.i("APPTAG", "newVersion: " + newVersion);
        // upgrade scripts
    }

    @Override
    public void onConfigure(final SQLiteDatabase db)
    {
        Logger.i("APPTAG", "onConfigure");
        Logger.i("APPTAG", "DB version: " + db.getVersion());

        try {
            Logger.i("APPTAG", "DB path: " + db.getPath());
            int version = ((Long) DatabaseUtils.longForQuery(db, "PRAGMA user_version;", null)).intValue();
            Logger.i("APPTAG", "onConfigure version: " + version);
        } catch (Exception e) {
            Logger.e("APPTAG", "Error" + e, e);
        }

        super.onConfigure(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Logger.i("APPTAG", "onDowngrade");
        Logger.i("APPTAG", "oldVersion: " + oldVersion);
        Logger.i("APPTAG", "newVersion: " + newVersion);
        super.onDowngrade(db, oldVersion, newVersion);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        Logger.i("APPTAG", "onOpen");
        Logger.i("APPTAG", "DB version: " + db.getVersion());
        super.onOpen(db);
    }
}

And this is the log I see,

05-04 07:47:33.021  4295  4295 I APPTAG: DBHelper constructor
05-04 07:47:33.021  4295  4295 I APPTAG: DATABASE_VERSION : 7
05-04 07:47:33.023  4295  4295 I APPTAG: UpdatesDB constructor
05-04 07:47:33.023  4295  4295 I APPTAG: SQLiteDatabaseWrapperFactory create
05-04 07:47:33.029  4295  4295 I APPTAG: onConfigure
05-04 07:47:33.030  4295  4295 I APPTAG: DB version: 7
05-04 07:47:33.030  4295  4295 I APPTAG: DB path: /data/user/0/<application-name>/databases/updates.db
05-04 07:47:33.030  4295  4295 I APPTAG: onConfigure version: 7
05-04 07:47:33.031  4295  4295 I APPTAG: onOpen
05-04 07:47:33.031  4295  4295 I APPTAG: DB version: 7

Interestingly, if I execute this command PRAGMA user_version; directly on the database, I get the version as 6. But when I tried through the code (as in onConfigure), it gives me 7. Due to this, it skips the onUpgrade method as per this code.

Could anyone help me understand why the db version is getting reported as 7 when I fetch it through code?

Clarifications:

  • I'm calling getWritableDatabase() to get the db instance.
  • DBContracts.DATABASE_VERSION is set to 7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2025-02-02 00:41:00

我相信您的问题在于您的问题中,或者可能是滥用拳头的构造函数(也许会评论一下,看看您的票价)。

考虑此修改(主要参见代码的末尾,而IE添加getDatabaseversion方法),以在打开数据库和其他次要更改之前,请包括检索版本号,以便我的便利: -

class DBHelper extends SQLiteOpenHelper {
    //@Inject
    /* MAKE SURE THIS CONSTRUCTOR CAN'T BE USED, it could cause issues
    public DBHelper(final Context context) {
        this(context, DBContracts.DATABASE_FILE_NAME);
    }
     */
    
    /* Changed logger to Log.i throughout */
    
    DBHelper(final Context context, final String databaseFileName) {
        super(context, databaseFileName, null, DBContracts.DATABASE_VERSION);
        Log.i("APPTAG","PREOPEN VERSION = " + getDatabaseVersion(context)); //<<<<<<<<<< ADDED 
        Log.i("APPTAG", "DBHelper constructor");
        Log.i("APPTAG", "DATABASE_VERSION : " + DBContracts.DATABASE_VERSION);
    }

    @Override
    public void onCreate(final SQLiteDatabase db) {
        Log.i("APPTAG", "onCreate");
        // create scripts
    }

    @Override
    public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
        Log.i("APPTAG", "onUpgrade");
        Log.i("APPTAG", "oldVersion: " + oldVersion);
        Log.i("APPTAG", "newVersion: " + newVersion);
        // upgrade scripts
    }

    @Override
    public void onConfigure(final SQLiteDatabase db) {
        Log.i("APPTAG", "onConfigure");
        Log.i("APPTAG", "DB version: " + db.getVersion());

        try {
            Log.i("APPTAG", "DB path: " + db.getPath());
            int version = ((Long) DatabaseUtils.longForQuery(db, "PRAGMA user_version;", null)).intValue();
            Log.i("APPTAG", "onConfigure version: " + version);
        } catch (Exception e) {
            Log.e("APPTAG", "Error" + e, e);
        }

        super.onConfigure(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("APPTAG", "onDowngrade");
        Log.i("APPTAG", "oldVersion: " + oldVersion);
        Log.i("APPTAG", "newVersion: " + newVersion);
        super.onDowngrade(db, oldVersion, newVersion);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        Log.i("APPTAG", "onOpen");
        Log.i("APPTAG", "DB version: " + db.getVersion());
        super.onOpen(db);
    }

    /*<<<<<<<<<< ADDED >>>>>>>>>>*/
    private int getDatabaseVersion(Context context) {
        int rv = -666;
        SQLiteDatabase db = null;
        File databaseFilePath = new File(context.getDatabasePath(DBContracts.DATABASE_FILE_NAME).getPath());
        File databaseDirectory = databaseFilePath.getParentFile();
        /* If database file does not exist AND version is 6 then create the databases directory and then create the database */
        if (! databaseFilePath.exists() && (DBContracts.DATABASE_VERSION == 6)) {
            databaseDirectory.mkdirs();
            db = SQLiteDatabase.openOrCreateDatabase(databaseFilePath.getPath(),null);
        }
        else {
            db = SQLiteDatabase.openDatabase(context.getDatabasePath(DBContracts.DATABASE_FILE_NAME).getPath(), null, SQLiteDatabase.OPEN_READWRITE);
        }
        if (db != null) {
            rv = db.getVersion();
            db.close();
        }
        return rv;
    }
}

然后活动中的以下代码: -

public class MainActivity extends AppCompatActivity {

    DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new DBHelper(this,DBContracts.DATABASE_FILE_NAME);
        SQLiteDatabase db = dbHelper.getWritableDatabase(); //<<<<<<<<<< Force an open of the db
    }
}

应用程序两次运行: -

  1. 首先使用dbontracts.database_version为6
    1. 这将创建版本0,然后由SqliteOpenhelper更改为版本6
  2. 秒的版本,而无需使用dbContracts.database_version卸载该应用程序为7的

结果是: -

2022-05-05 21:30:06.639 I/APPTAG: PREOPEN VERSION = 0
2022-05-05 21:30:06.640 I/APPTAG: DBHelper constructor
2022-05-05 21:30:06.640 I/APPTAG: DATABASE_VERSION : 6
2022-05-05 21:30:06.643 I/APPTAG: onConfigure
2022-05-05 21:30:06.644 I/APPTAG: DB version: 0
2022-05-05 21:30:06.644 I/APPTAG: DB path: /data/user/0/a.a.so721000750unupdatenotbeingcalled/databases/testdb
2022-05-05 21:30:06.645 I/APPTAG: onConfigure version: 0
2022-05-05 21:30:06.645 I/APPTAG: onCreate
2022-05-05 21:30:06.655 I/APPTAG: onOpen
2022-05-05 21:30:06.655 I/APPTAG: DB version: 6 
  • 正如预期的(onupgrade noceptrade not unupgrade nowate nowate not upgrade not upgrade note inscreate ness of increate and increate ness将版本设置为6)。

然后: -

2022-05-05 21:33:37.138 I/APPTAG: PREOPEN VERSION = 6
2022-05-05 21:33:37.138 I/APPTAG: DBHelper constructor
2022-05-05 21:33:37.138 I/APPTAG: DATABASE_VERSION : 7
2022-05-05 21:33:37.140 I/APPTAG: onConfigure
2022-05-05 21:33:37.141 I/APPTAG: DB version: 6
2022-05-05 21:33:37.141 I/APPTAG: DB path: /data/user/0/a.a.so721000750unupdatenotbeingcalled/databases/testdb
2022-05-05 21:33:37.141 I/APPTAG: onConfigure version: 6
2022-05-05 21:33:37.141 I/APPTAG: onUpgrade
2022-05-05 21:33:37.142 I/APPTAG: oldVersion: 6
2022-05-05 21:33:37.142 I/APPTAG: newVersion: 7
2022-05-05 21:33:37.152 I/APPTAG: onOpen
2022-05-05 21:33:37.153 I/APPTAG: DB version: 7
  • 完全按预期;那是
    • 尝试打开之前的数据库是版本6和
    • 数据库_Vearsion是7。
    • 在OnConfigure中,版本为6。
    • onupgrade 被要求从6更改为7和
    • 打开数据库时的版本是7。

第三次运行,没有更改任何内容(因此已经在版本7处的DB): -

2022-05-05 21:43:02.167 I/APPTAG: PREOPEN VERSION = 7
2022-05-05 21:43:02.167 I/APPTAG: DBHelper constructor
2022-05-05 21:43:02.167 I/APPTAG: DATABASE_VERSION : 7
2022-05-05 21:43:02.170 I/APPTAG: onConfigure
2022-05-05 21:43:02.170 I/APPTAG: DB version: 7
2022-05-05 21:43:02.170 I/APPTAG: DB path: /data/user/0/a.a.so721000750unupdatenotbeingcalled/databases/testdb
2022-05-05 21:43:02.171 I/APPTAG: onConfigure version: 7
2022-05-05 21:43:02.171 I/APPTAG: onOpen
2022-05-05 21:43:02.171 I/APPTAG: DB version: 7
  • 哪个限制了质疑的(按照注释)2登录行,而其他的预开放登录行基本上与您所显示的内容匹配 。如果您出于某种原因说“有趣的是,如果我执行此命令pragma user_version;直接在数据库上,我将版本为6。”

I believe that your issue lies outside of the code in your question, or perhaps it is a misuse the fist constructor (perhaps comment that out and see how you fare).

Consider this modification (primarily see the end of the code and the i.e. the added getDatabaseVersion method), to include retrieval of the version number prior to to opening the database and other minor changes for my convenience :-

class DBHelper extends SQLiteOpenHelper {
    //@Inject
    /* MAKE SURE THIS CONSTRUCTOR CAN'T BE USED, it could cause issues
    public DBHelper(final Context context) {
        this(context, DBContracts.DATABASE_FILE_NAME);
    }
     */
    
    /* Changed logger to Log.i throughout */
    
    DBHelper(final Context context, final String databaseFileName) {
        super(context, databaseFileName, null, DBContracts.DATABASE_VERSION);
        Log.i("APPTAG","PREOPEN VERSION = " + getDatabaseVersion(context)); //<<<<<<<<<< ADDED 
        Log.i("APPTAG", "DBHelper constructor");
        Log.i("APPTAG", "DATABASE_VERSION : " + DBContracts.DATABASE_VERSION);
    }

    @Override
    public void onCreate(final SQLiteDatabase db) {
        Log.i("APPTAG", "onCreate");
        // create scripts
    }

    @Override
    public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
        Log.i("APPTAG", "onUpgrade");
        Log.i("APPTAG", "oldVersion: " + oldVersion);
        Log.i("APPTAG", "newVersion: " + newVersion);
        // upgrade scripts
    }

    @Override
    public void onConfigure(final SQLiteDatabase db) {
        Log.i("APPTAG", "onConfigure");
        Log.i("APPTAG", "DB version: " + db.getVersion());

        try {
            Log.i("APPTAG", "DB path: " + db.getPath());
            int version = ((Long) DatabaseUtils.longForQuery(db, "PRAGMA user_version;", null)).intValue();
            Log.i("APPTAG", "onConfigure version: " + version);
        } catch (Exception e) {
            Log.e("APPTAG", "Error" + e, e);
        }

        super.onConfigure(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("APPTAG", "onDowngrade");
        Log.i("APPTAG", "oldVersion: " + oldVersion);
        Log.i("APPTAG", "newVersion: " + newVersion);
        super.onDowngrade(db, oldVersion, newVersion);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        Log.i("APPTAG", "onOpen");
        Log.i("APPTAG", "DB version: " + db.getVersion());
        super.onOpen(db);
    }

    /*<<<<<<<<<< ADDED >>>>>>>>>>*/
    private int getDatabaseVersion(Context context) {
        int rv = -666;
        SQLiteDatabase db = null;
        File databaseFilePath = new File(context.getDatabasePath(DBContracts.DATABASE_FILE_NAME).getPath());
        File databaseDirectory = databaseFilePath.getParentFile();
        /* If database file does not exist AND version is 6 then create the databases directory and then create the database */
        if (! databaseFilePath.exists() && (DBContracts.DATABASE_VERSION == 6)) {
            databaseDirectory.mkdirs();
            db = SQLiteDatabase.openOrCreateDatabase(databaseFilePath.getPath(),null);
        }
        else {
            db = SQLiteDatabase.openDatabase(context.getDatabasePath(DBContracts.DATABASE_FILE_NAME).getPath(), null, SQLiteDatabase.OPEN_READWRITE);
        }
        if (db != null) {
            rv = db.getVersion();
            db.close();
        }
        return rv;
    }
}

Then the following code in an activity:-

public class MainActivity extends AppCompatActivity {

    DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new DBHelper(this,DBContracts.DATABASE_FILE_NAME);
        SQLiteDatabase db = dbHelper.getWritableDatabase(); //<<<<<<<<<< Force an open of the db
    }
}

The App was run twice:-

  1. First with DBContracts.DATABASE_VERSION as 6
    1. This would then create version 0, which would then be changed by the SQLiteOpenHelper to be version 6
  2. Second without uninstalling the App with DBContracts.DATABASE_VERSION as 7

The results being :-

2022-05-05 21:30:06.639 I/APPTAG: PREOPEN VERSION = 0
2022-05-05 21:30:06.640 I/APPTAG: DBHelper constructor
2022-05-05 21:30:06.640 I/APPTAG: DATABASE_VERSION : 6
2022-05-05 21:30:06.643 I/APPTAG: onConfigure
2022-05-05 21:30:06.644 I/APPTAG: DB version: 0
2022-05-05 21:30:06.644 I/APPTAG: DB path: /data/user/0/a.a.so721000750unupdatenotbeingcalled/databases/testdb
2022-05-05 21:30:06.645 I/APPTAG: onConfigure version: 0
2022-05-05 21:30:06.645 I/APPTAG: onCreate
2022-05-05 21:30:06.655 I/APPTAG: onOpen
2022-05-05 21:30:06.655 I/APPTAG: DB version: 6 
  • as expected (onUpgrade not called as there is no upgrade rather onCreate is called which sets the version as 6).

Then :-

2022-05-05 21:33:37.138 I/APPTAG: PREOPEN VERSION = 6
2022-05-05 21:33:37.138 I/APPTAG: DBHelper constructor
2022-05-05 21:33:37.138 I/APPTAG: DATABASE_VERSION : 7
2022-05-05 21:33:37.140 I/APPTAG: onConfigure
2022-05-05 21:33:37.141 I/APPTAG: DB version: 6
2022-05-05 21:33:37.141 I/APPTAG: DB path: /data/user/0/a.a.so721000750unupdatenotbeingcalled/databases/testdb
2022-05-05 21:33:37.141 I/APPTAG: onConfigure version: 6
2022-05-05 21:33:37.141 I/APPTAG: onUpgrade
2022-05-05 21:33:37.142 I/APPTAG: oldVersion: 6
2022-05-05 21:33:37.142 I/APPTAG: newVersion: 7
2022-05-05 21:33:37.152 I/APPTAG: onOpen
2022-05-05 21:33:37.153 I/APPTAG: DB version: 7
  • this exactly as expected; that is
    • the database before an attempt to open is version 6 and
    • the DATABASE_VEARSION is 7.
    • At onConfigure the version is 6.
    • onUpgrade is called to change from 6 to 7 and
    • finally the version is 7 when the database is opened.

A third run, without changing anything (so db already at version 7) then :-

2022-05-05 21:43:02.167 I/APPTAG: PREOPEN VERSION = 7
2022-05-05 21:43:02.167 I/APPTAG: DBHelper constructor
2022-05-05 21:43:02.167 I/APPTAG: DATABASE_VERSION : 7
2022-05-05 21:43:02.170 I/APPTAG: onConfigure
2022-05-05 21:43:02.170 I/APPTAG: DB version: 7
2022-05-05 21:43:02.170 I/APPTAG: DB path: /data/user/0/a.a.so721000750unupdatenotbeingcalled/databases/testdb
2022-05-05 21:43:02.171 I/APPTAG: onConfigure version: 7
2022-05-05 21:43:02.171 I/APPTAG: onOpen
2022-05-05 21:43:02.171 I/APPTAG: DB version: 7
  • Which bar the questioned (as per the comments) 2 logged lines and the additional PREOPEN logged line basically matches what you have shown. Which could be your issue if for some reason you are incorrect when you say "Interestingly, if I execute this command PRAGMA user_version; directly on the database, I get the version as 6."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文