English 中文(简体)
Android 更名为Kalk数据库
原标题:Android rename SQLite Database

是否可以重新命名已在甲状腺中建立的数据库?

在我更新后,我想重新命名旧数据库,安装新的数据库,然后比较一些价值,最后删除旧的数值。

我正在从资产夹中的 file子档案中制造。 因此,我不能重新命名所有表格并插入新的表格。

查明:

旧的数据库将只包含一个表格,我需要对照新的(更新后)数据库对数值进行比较。

两个数据库都从资产夹中的一个qlite文档中复制。

一旦把旧数据库的数值与新数据库相比较,我将删除旧的数值,并将新数值与我比较的数值相比较。

想做的是,旧的老旧造就了新事物,做了上述一切。

最佳回答

页: 1 闪电是正确的,你应当删除旧的b布,复制新的b。

假设您使用<代码>SQLiteOpenHelper,您可使用<代码>createDatabaseIfRequired();、getReadableDatabase(和getWritableDatabase()。

private boolean checkOldDatabase() {
    Log.d(Constants.LOGTAG, "OperationDbHelper.checkDatabase");
    File f = new File(DB_PATH + OLD_DB_NAME);
    return f.exists();
}

public void createDatabaseIfRequired() throws IOException, SQLiteException {
    if (!checkOldDatabase()) {
      // do db comparison / delete old db / copy new db
    }
}
问题回答

Just rename the File. Make sure the database is closed first!

在你的活动类别中呼吁这样做:

private void renameDatabase()
{
    File databaseFile = getDatabasePath("yourdb.whatever");
    File oldDatabaseFile = new File(databaseFile.getParentFile(), "yourdb_old.whatever");

    databaseFile.renameTo(oldDatabaseFile);
}

对澄清的答复。 重新命名旧(如上所述),从资产夹中复制新资产,开放数据库,进行比较。 然后删除旧档案。

不可能直接改用一张薄片。

但是,你可以复制这一文件,创造新的和删除旧的。

Thanks to Kevin Galligan s answer, I was able to create a function in my Kotlin Android app that I can use whenever it might ever be necessary, to rename the database files.

如果你重新使用 Java,你就不得不把yn改成比,但希望这部法典能够有所解释。

val x: String = "Hello"
//in Kotlin would be
String x = "Hello";
//in Java, for example.

不管怎么说,在我的法典中,我感到可以自由地问问:你有:

private fun checkAndRenameDatabase(oldName: String, newName: String) {
    val oldDatabaseFile: File = getDatabasePath(oldName)
    val oldDatabaseJournal: File = getDatabasePath("${oldName}-journal")

    // Can use this to check files beforehand, using breakpoints 
    //val files = oldDatabaseFile.parentFile.listFiles()

    if(oldDatabaseFile.exists() || oldDatabaseJournal.exists()) {
        db.close()  // Ensure existing database is closed

        val newDatabaseFile: File = getDatabasePath(newName)
        val newDatabaseJournal: File = getDatabasePath("${newName}-journal")

        if(oldDatabaseFile.exists()) {
            if(newDatabaseFile.exists()) {
                newDatabaseFile.delete()
            }
            oldDatabaseFile.renameTo(newDatabaseFile)
        }

        if(oldDatabaseJournal.exists()) {
            if(newDatabaseJournal.exists()) {
                newDatabaseJournal.delete()
            }
            oldDatabaseJournal.renameTo(newDatabaseJournal)
        }

        // Use with breakpoints to ensure files are now in order
        //val newFiles = oldDatabaseFile.parentFile.listFiles()

        // Re-open database with new name
        db = SQLiteDBHelper(applicationContext, newName)
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签