哪些方法可用于管理现有数据库的不同版本?

2024-03-12

Note这旨在成为提出并回答你自己的问题 as per 以问答方式分享您的知识 https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/

通常,为使用 SQLite 的 Android 应用程序提供预先存在的数据库。数据库通常放在assets文件夹中,如果数据库不存在,App会复制该数据库。

对于较新版本的应用程序,更改资产文件夹中的数据库是可以的,因为更改的数据库将被复制。然而,当从以前的版本升级应用程序时,数据库将存在,因此不会被复制,也不能轻松地无缝删除它以启用从资产文件夹的复制。

如果需要保留用户数据,可能会变得更加复杂。

例如,我最初部署一个数据库,其中包含一个包含 3 列的表,其中创建的列是:-

CREATE TABLE "user" (
  "_id" INTEGER NOT NULL,
  "user" TEXT,
  "password" TEXT,
  PRIMARY KEY ("_id")
)

其中包含以下数据:-

该应用程序的下一个版本希望利用更改后的架构,其中包含一个额外的列和一个唯一约束user列如:-

CREATE TABLE "user" (
  "_id" INTEGER NOT NULL,
  "user" TEXT,
  "password" TEXT,
  "email" TEXT,
  PRIMARY KEY ("_id"),
  CONSTRAINT "user" UNIQUE ("user")
);

另外还添加了额外的数据,例如就是现在 :-

用户可以添加自己的数据,这些数据需要保留。因此,即使可能,删除数据库文件也会删除用户的数据。

可以使用哪些方法来管理现有数据库的不同版本?


简而言之,一种方法是将新资产文件复制到适当的位置,同时保留原始数据库文件的副本,然后您可以应用更新,以便有效地将用户数据保留到新复制的数据库中。BUT仅当应用程序针对该特定更改进行升级并且数据库存在时。

如果数据库不存在,则应使用资产文件的标准副本。

以下是如何完成此操作的示例。

此示例在很大程度上依赖于允许管理和对资产文件夹中的文件和/或数据库文件本身进行一些询问的例程。

一个类即DBAssetHandler.java满足上述要求(以及使用 SQLiteOpenHelper 时提取 user_version 又名数据库版本的能力)。

  • 请注意,该类也已在 Android Pie 上进行了测试,因此适用于 Android Pie、预写式日志记录(WAL 是 Pie 中的默认设置)以及日志模式(之前的默认设置)。

  • 另请注意,如果使用 WAL,那么您应该确保数据库是完全检查点的请参阅 - 预写式日志记录 https://www.sqlite.org/wal.html

it is :-

public class DBAssetHandler {

    static final String[] tempfiles = new String[]{"-journal","-wal","-shm"}; // temporary files to rename
    public static final String backup = "-backup"; //value to be appended to file name when renaming (psuedo delete)
    public static final  int OUCH = -666666666;

    /**
     * Check if the database already exists. NOTE will create the databases folder is it doesn't exist
     * @return true if it exists, false if it doesn't
     */
    public static boolean checkDataBase(Context context, String dbname) {

        File db = new File(context.getDatabasePath(dbname).getPath()); //Get the file name of the database
        Log.d("DBPATH","DB Path is " + db.getPath()); //TODO remove if publish App
        if (db.exists()) return true; // If it exists then return doing nothing

        // Get the parent (directory in which the database file would be)
        File dbdir = db.getParentFile();
        // If the directory does not exits then make the directory (and higher level directories)
        if (!dbdir.exists()) {
            db.getParentFile().mkdirs();
            dbdir.mkdirs();
        }
        return false;
    }


    /**
     * Copy database file from the assets folder
     * (long version caters for asset file name being different to the database name)
     * @param context           Context is needed to get the applicable package
     * @param dbname            name of the database file
     * @param assetfilename     name of the asset file
     * @param deleteExistingDB  true if an existing database file should be deleted
     *                              note will delete journal and wal files
     *                              note doen't actually delete the files rater it renames
     *                              the files by appended -backup to the file name
     *                              SEE/USE clearForceBackups below to delete the renamed files
     */
    public static void copyDataBase(Context context, String dbname, String assetfilename, boolean deleteExistingDB) {

        final String TAG = "COPYDATABASE";
        int stage = 0, buffer_size = 4096, blocks_copied = 0, bytes_copied = 0;
        File f = new File(context.getDatabasePath(dbname).toString());
        InputStream is;
        OutputStream os;

        /**
         * If forcing then effectively delete (rename) current database files
         */
        if (deleteExistingDB) {
            //String[] tempfiles = new String[]{"-journal","-wal","-shm"};
            //String backup = "-backup";
            f.renameTo(context.getDatabasePath(dbname + "-backup"));
            for (String s: tempfiles) {
                File tmpf = new File(context.getDatabasePath(dbname + s).toString());
                if (tmpf.exists()) {
                    tmpf.renameTo(context.getDatabasePath(dbname + s + backup));
                }
            }
        }


        //Open your local db as the input stream
        Log.d(TAG,"Initiated Copy of the database file " + assetfilename + " from the assets folder."); //TODO remove if publishing
        try {
            is = context.getAssets().open(assetfilename); // Open the Asset file
            stage++;
            Log.d(TAG, "Asset file " + assetfilename + " found so attmepting to copy to " + f.getPath()); //TODO remove if publishing

            os = new FileOutputStream(f);
            stage++;
            //transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[buffer_size];
            int length;
            while ((length = is.read(buffer)) > 0) {
                blocks_copied++;
                Log.d(TAG, "Attempting copy of block " + String.valueOf(blocks_copied) + " which has " + String.valueOf(length) + " bytes."); //TODO remove if publishing
                os.write(buffer, 0, length);
                bytes_copied += length;
            }
            stage++;
            Log.d(TAG,
                    "Finished copying Database " + dbname +
                            " from the assets folder, to  " + f.getPath() +
                            String.valueOf(bytes_copied) + "were copied, in " +
                            String.valueOf(blocks_copied) + " blocks of size " +
                            String.valueOf(buffer_size) + "."
            ); //TODO remove if publishing
            //Close the streams
            os.flush();
            stage++;
            os.close();
            stage++;
            is.close();
            Log.d(TAG, "All Streams have been flushed and closed.");
        } catch (IOException e) {
            String exception_message = "";
            e.printStackTrace();
            switch (stage) {
                case 0:
                    exception_message = "Error trying to open the asset " + dbname;
                    break;
                case 1:
                    exception_message = "Error opening Database file for output, path is " + f.getPath();
                    break;
                case 2:
                    exception_message = "Error flushing written database file " + f.getPath();
                    break;
                case 3:
                    exception_message = "Error closing written database file " + f.getPath();
                    break;
                case 4:
                    exception_message = "Error closing asset file " + f.getPath();

            }
            throw new RuntimeException("Unable to copy the database from the asset folder." + exception_message + " see starck-trace above.");
        }
    }

    /**
     * Copy the databsse from the assets folder where asset name and dbname are the same
     * @param context
     * @param dbname
     * @param deleteExistingDB
     */
    public static void copyDataBase(Context context, String dbname, boolean deleteExistingDB) {
        copyDataBase(context, dbname,dbname,deleteExistingDB);
    }

    /**
     * Get the SQLite_user_vesrion from the DB in the asset folder
     *
     * @param context           needed to get the appropriate package assets
     * @param assetfilename     the name of the asset file (assumes/requires name matches database)
     * @return                  the version number as stored in the asset DB
     */
    public static int getVersionFromDBInAssetFolder(Context context, String assetfilename) {
        InputStream is;
        try {
            is = context.getAssets().open(assetfilename);
        } catch (IOException e) {
            return OUCH;
        }
        return getDBVersionFromInputStream(is);
    }

    /**
     * Get the version from the database itself without opening the database as an SQliteDatabase
     * @param context   Needed to ascertain package
     * @param dbname    the name of the dataabase
     * @return          the version number extracted
     */
    public static int getVersionFromDBFile(Context context, String dbname) {
        InputStream is;
        try {
            is = new FileInputStream(new File(context.getDatabasePath(dbname).toString()));
        } catch (IOException e) {
            return OUCH;
        }
        return getDBVersionFromInputStream(is);
    }

    /**
     * Get the Database Version (user_version) from an inputstream
     *  Note the inputstream is closed
     * @param is    The Inputstream
     * @return      The extracted version number
     */
    private static int getDBVersionFromInputStream(InputStream is) {
        int rv = -1, dbversion_offset = 60, dbversion_length = 4 ;
        byte[] dbfileheader = new byte[64];
        byte[] dbversion = new byte[4];
        try {
            is.read(dbfileheader);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
            return rv;
        }

        for (int i = 0; i < dbversion_length; i++ ) {
            dbversion[i] = dbfileheader[dbversion_offset + i];
        }
        return ByteBuffer.wrap(dbversion).getInt();
    }

    /**
     * Check to see if the asset file exists
     *
     * @param context           needed to get the appropriate package
     * @param assetfilename     the name of the asset file to check
     * @return                  true if the asset file exists, else false
     */
    public static boolean ifAssetFileExists(Context context, String assetfilename) {
        try {
            context.getAssets().open(assetfilename);
        } catch (IOException e) {
            return false;
        }
        return true;
    }


    /**
     * Delete the backup
     * @param context
     * @param dbname
     */
    public static void clearForceBackups(Context context, String dbname) {
        String[] fulllist = new String[tempfiles.length + 1];

        for (int i = 0;i < tempfiles.length; i++) {
            fulllist[i] = tempfiles[i];
        }
        fulllist[tempfiles.length] = ""; // Add "" so database file backup is also deleted
        for (String s: fulllist) {
            File tmpf = new File(context.getDatabasePath(dbname + s + backup).toString());
            if (tmpf.exists()) {
                tmpf.delete();
            }
        }
    }
}
  • 希望方法名称和注释能够解释上面的代码。

资产文件有两个:-

  • pev1.db- 如上所述的原始预先存在的数据库
  • pev1mod.db- 修改的(额外的列、UNIQUE 约束和额外的行)。

数据库助手(SQLOpenHelper 的子类)即PEV2DBHelper.java,需要注意的是数据库版本(数据库版本) 用于控制,因此与 APK 版本不同(可能比 DB 更频繁地更改)

  • 尝试使用时发现问题升级时方法,因此另一种方法是获取数据库的用户版本从文件而不是通过 SQLiteDatabase。

Here is PEV2DBHelper.java :-

/**
 * MORE COMPLEX EXAMPLE RETAINING USER DATA
 */
public class PEV2DBHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "pev1.db";
    public static final String ASSETTOCOPY_DBV2 = "pev1mod.db"; //<<<<<<<<<< changed DB
    public static final int DBVERSION = 2; //<<<<<<<<<< increase and db file from assets will copied keeping existing data
    Context mContext;

    public PEV2DBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);


        int dbversion = DBAssetHandler.getVersionFromDBFile(context,DBNAME);
        Log.d("DBFILEVERSION","Database File Version = " + String.valueOf(dbversion));
        int af1version = DBAssetHandler.getVersionFromDBInAssetFolder(context,DBNAME);
        Log.d("DBFILEVERSION","Asset Database File Version = " + String.valueOf(af1version));
        int af2version = DBAssetHandler.getVersionFromDBInAssetFolder(context,ASSETTOCOPY_DBV2);
        Log.d("DBFILEVERSION","Asset Database File Version = " + String.valueOf(af2version));

        // cater for different DBVERSIONS (for testing )
        if (!DBAssetHandler.checkDataBase(context,DBNAME)) {

            //If new installation of the APP then copy the appropriate asset file for the DB
            switch (DBVERSION) {
                case 1:
                    DBAssetHandler.copyDataBase(context,DBNAME,DBNAME,false);
                    break;
                case 2:
                    DBAssetHandler.copyDataBase(context,DBNAME,ASSETTOCOPY_DBV2,false);
                    break;
            }
        }

        // If DBVERSION upgraded to 2 with modified DB but wanting to preserve used data
        if (DBAssetHandler.checkDataBase(context,DBNAME) & (DBVERSION > DBAssetHandler.getVersionFromDBFile(context, DBNAME)) & (DBVERSION == 2) ) {

            String[] oldcolumns = new String[]{"user","password"};

            // Copy in the new DB noting that delete option renames old (truue flag important)
            DBAssetHandler.copyDataBase(context,DBNAME,ASSETTOCOPY_DBV2,true);

            //Get the newly copied database
            SQLiteDatabase newdb = SQLiteDatabase.openDatabase(context.getDatabasePath(DBNAME).toString(),null,SQLiteDatabase.OPEN_READWRITE);
            //Get the old database (backup copy)
            SQLiteDatabase olddb =  SQLiteDatabase.openDatabase(context.getDatabasePath(DBNAME + DBAssetHandler.backup).toString(),null,SQLiteDatabase.OPEN_READWRITE);

            //Prepare to insert old rows (note user column is UNIQUE so pretty simple scenario just try inserting all and duplicates will be rejected)
            ContentValues cv = new ContentValues();
            Cursor oldcsr = olddb.query("user",null,null,null,null,null,null);
            newdb.beginTransaction();
            while (oldcsr.moveToNext()) {
                cv.clear();
                for (String columnname: oldcolumns) {
                    cv.put(columnname,oldcsr.getString(oldcsr.getColumnIndex(columnname)));
                }
                newdb.insert("user",null,cv);
            }
            newdb.setTransactionSuccessful();
            newdb.endTransaction();
            newdb.close();
            olddb.close();

            // Finally delete the renamed old database
            DBAssetHandler.clearForceBackups(context,DBNAME);
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
  • 请注意,添加、删除提取行的方法几乎没有臃肿之处。然而,它有点过于复杂,因为它处理版本之间的切换以方便演示。

最后是一个调用 PEV2DBHelper 的示例活动,将架构和表中的行写入日志。

使用的活动是MainActivity.java并且是 :-

public class MainActivity extends AppCompatActivity {

    PEV2DBHelper mDBHlpr2; //DBHelper for example that retains user data

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        doPEV2(); // Test simple more complex scenario
    }


    private void doPEV2() {

        mDBHlpr2 = new PEV2DBHelper(this);
        SQLiteDatabase db = mDBHlpr2.getWritableDatabase();
        Cursor csr = db.query("sqlite_master",null,null,null,null,null,null);
        DatabaseUtils.dumpCursor(csr);
        csr = db.query("user",null,null,null,null,null,null);
        DatabaseUtils.dumpCursor(csr);
        if (PEV2DBHelper.DBVERSION == 1) {
            addUserData(db);
            csr = db.query("user",null,null,null,null,null,null);
            DatabaseUtils.dumpCursor(csr);
        }
        csr.close();
        db.close();
    }

    /**
     * Add some user data for testing presevation of that data
     * @param db    the SQLitedatabase
     */
    private void addUserData(SQLiteDatabase db) {
        ContentValues cv = new ContentValues();
        cv.put("user","mr new user");
        cv.put("password","a password");
        db.insert("user",null,cv);
    }
}

Results

1.首次运行时DBVERSION为1(全新App)

在本例中是资产文件pev1.db从资产文件夹复制输出是:-

2019-02-22 19:07:54.676 28670-28670/? D/DBFILEVERSION: Database File Version = -666666666
2019-02-22 19:07:54.677 28670-28670/? D/DBFILEVERSION: Asset Database File Version = 0
2019-02-22 19:07:54.677 28670-28670/? D/DBFILEVERSION: Asset Database File Version = 0
2019-02-22 19:07:54.677 28670-28670/? D/DBPATH: DB Path is /data/user/0/mjt.so54807516/databases/pev1.db
2019-02-22 19:07:54.677 28670-28670/? D/COPYDATABASE: Initiated Copy of the database file pev1.db from the assets folder.
2019-02-22 19:07:54.677 28670-28670/? D/COPYDATABASE: Asset file pev1.db found so attmepting to copy to /data/user/0/mjt.so54807516/databases/pev1.db
2019-02-22 19:07:54.677 28670-28670/? D/COPYDATABASE: Attempting copy of block 1 which has 4096 bytes.
2019-02-22 19:07:54.677 28670-28670/? D/COPYDATABASE: Attempting copy of block 2 which has 4096 bytes.
2019-02-22 19:07:54.677 28670-28670/? D/COPYDATABASE: Finished copying Database pev1.db from the assets folder, to  /data/user/0/mjt.so54807516/databases/pev1.db8192were copied, in 2 blocks of size 4096.
2019-02-22 19:07:54.678 28670-28670/? D/COPYDATABASE: All Streams have been flushed and closed.
2019-02-22 19:07:54.678 28670-28670/? D/DBPATH: DB Path is /data/user/0/mjt.so54807516/databases/pev1.db
2019-02-22 19:07:54.701 28670-28670/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@71528f1
2019-02-22 19:07:54.701 28670-28670/? I/System.out: 0 {
2019-02-22 19:07:54.701 28670-28670/? I/System.out:    type=table
2019-02-22 19:07:54.701 28670-28670/? I/System.out:    name=user
2019-02-22 19:07:54.701 28670-28670/? I/System.out:    tbl_name=user
2019-02-22 19:07:54.702 28670-28670/? I/System.out:    rootpage=2
2019-02-22 19:07:54.702 28670-28670/? I/System.out:    sql=CREATE TABLE "user" (
2019-02-22 19:07:54.702 28670-28670/? I/System.out:   "_id" INTEGER NOT NULL,
2019-02-22 19:07:54.702 28670-28670/? I/System.out:   "user" TEXT,
2019-02-22 19:07:54.702 28670-28670/? I/System.out:   "password" TEXT,
2019-02-22 19:07:54.702 28670-28670/? I/System.out:   PRIMARY KEY ("_id")
2019-02-22 19:07:54.702 28670-28670/? I/System.out: )
2019-02-22 19:07:54.702 28670-28670/? I/System.out: }
2019-02-22 19:07:54.702 28670-28670/? I/System.out: 1 {
2019-02-22 19:07:54.702 28670-28670/? I/System.out:    type=table
2019-02-22 19:07:54.702 28670-28670/? I/System.out:    name=android_metadata
2019-02-22 19:07:54.702 28670-28670/? I/System.out:    tbl_name=android_metadata
2019-02-22 19:07:54.702 28670-28670/? I/System.out:    rootpage=3
2019-02-22 19:07:54.702 28670-28670/? I/System.out:    sql=CREATE TABLE android_metadata (locale TEXT)
2019-02-22 19:07:54.702 28670-28670/? I/System.out: }
2019-02-22 19:07:54.702 28670-28670/? I/System.out: <<<<<
2019-02-22 19:07:54.703 28670-28670/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@1e20cd6
2019-02-22 19:07:54.703 28670-28670/? I/System.out: 0 {
2019-02-22 19:07:54.703 28670-28670/? I/System.out:    _id=1
2019-02-22 19:07:54.703 28670-28670/? I/System.out:    user=Fred
2019-02-22 19:07:54.703 28670-28670/? I/System.out:    password=fredpassword
2019-02-22 19:07:54.703 28670-28670/? I/System.out: }
2019-02-22 19:07:54.703 28670-28670/? I/System.out: 1 {
2019-02-22 19:07:54.703 28670-28670/? I/System.out:    _id=2
2019-02-22 19:07:54.703 28670-28670/? I/System.out:    user=Mary
2019-02-22 19:07:54.704 28670-28670/? I/System.out:    password=marypassword
2019-02-22 19:07:54.704 28670-28670/? I/System.out: }
2019-02-22 19:07:54.704 28670-28670/? I/System.out: <<<<<
2019-02-22 19:07:54.705 28670-28670/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@acdb57
2019-02-22 19:07:54.705 28670-28670/? I/System.out: 0 {
2019-02-22 19:07:54.705 28670-28670/? I/System.out:    _id=1
2019-02-22 19:07:54.705 28670-28670/? I/System.out:    user=Fred
2019-02-22 19:07:54.705 28670-28670/? I/System.out:    password=fredpassword
2019-02-22 19:07:54.706 28670-28670/? I/System.out: }
2019-02-22 19:07:54.706 28670-28670/? I/System.out: 1 {
2019-02-22 19:07:54.706 28670-28670/? I/System.out:    _id=2
2019-02-22 19:07:54.706 28670-28670/? I/System.out:    user=Mary
2019-02-22 19:07:54.706 28670-28670/? I/System.out:    password=marypassword
2019-02-22 19:07:54.706 28670-28670/? I/System.out: }
2019-02-22 19:07:54.706 28670-28670/? I/System.out: 2 {
2019-02-22 19:07:54.706 28670-28670/? I/System.out:    _id=3
2019-02-22 19:07:54.706 28670-28670/? I/System.out:    user=mr new user
2019-02-22 19:07:54.706 28670-28670/? I/System.out:    password=a password
2019-02-22 19:07:54.706 28670-28670/? I/System.out: }
2019-02-22 19:07:54.706 28670-28670/? I/System.out: <<<<<
  • -666666666 是版本,因为不存在文件,因此尝试从文件获取版本返回默认值,表明无法获取版本。

2. 第二次运行一切相同,除了版本号为 1。

2019-02-22 19:09:43.724 28730-28730/mjt.so54807516 D/DBFILEVERSION: Database File Version = 1
2019-02-22 19:09:43.724 28730-28730/mjt.so54807516 D/DBFILEVERSION: Asset Database File Version = 0
2019-02-22 19:09:43.724 28730-28730/mjt.so54807516 D/DBFILEVERSION: Asset Database File Version = 0
2019-02-22 19:09:43.725 28730-28730/mjt.so54807516 D/DBPATH: DB Path is /data/user/0/mjt.so54807516/databases/pev1.db
2019-02-22 19:09:43.725 28730-28730/mjt.so54807516 D/DBPATH: DB Path is /data/user/0/mjt.so54807516/databases/pev1.db
2019-02-22 19:09:43.729 28730-28730/mjt.so54807516 I/System.out: >>>>> 
..... etc

3.将DBVERSION更改为2后下次运行

2019-02-22 19:13:49.157 28866-28866/mjt.so54807516 D/DBFILEVERSION: Database File Version = 1
2019-02-22 19:13:49.158 28866-28866/mjt.so54807516 D/DBFILEVERSION: Asset Database File Version = 0
2019-02-22 19:13:49.158 28866-28866/mjt.so54807516 D/DBFILEVERSION: Asset Database File Version = 0
2019-02-22 19:13:49.158 28866-28866/mjt.so54807516 D/DBPATH: DB Path is /data/user/0/mjt.so54807516/databases/pev1.db
2019-02-22 19:13:49.158 28866-28866/mjt.so54807516 D/DBPATH: DB Path is /data/user/0/mjt.so54807516/databases/pev1.db
2019-02-22 19:13:49.158 28866-28866/mjt.so54807516 D/COPYDATABASE: Initiated Copy of the database file pev1mod.db from the assets folder.
2019-02-22 19:13:49.159 28866-28866/mjt.so54807516 D/COPYDATABASE: Asset file pev1mod.db found so attmepting to copy to /data/user/0/mjt.so54807516/databases/pev1.db
2019-02-22 19:13:49.159 28866-28866/mjt.so54807516 D/COPYDATABASE: Attempting copy of block 1 which has 4096 bytes.
2019-02-22 19:13:49.159 28866-28866/mjt.so54807516 D/COPYDATABASE: Attempting copy of block 2 which has 4096 bytes.
2019-02-22 19:13:49.159 28866-28866/mjt.so54807516 D/COPYDATABASE: Attempting copy of block 3 which has 4096 bytes.
2019-02-22 19:13:49.159 28866-28866/mjt.so54807516 D/COPYDATABASE: Attempting copy of block 4 which has 4096 bytes.
2019-02-22 19:13:49.159 28866-28866/mjt.so54807516 D/COPYDATABASE: Finished copying Database pev1.db from the assets folder, to  /data/user/0/mjt.so54807516/databases/pev1.db16384were copied, in 4 blocks of size 4096.
2019-02-22 19:13:49.159 28866-28866/mjt.so54807516 D/COPYDATABASE: All Streams have been flushed and closed.
2019-02-22 19:13:49.186 28866-28866/mjt.so54807516 E/SQLiteDatabase: Error inserting password=fredpassword user=Fred
    android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: user.user (code 2067 SQLITE_CONSTRAINT_UNIQUE)
        at 
    .........
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-02-22 19:13:49.191 28866-28866/mjt.so54807516 E/SQLiteDatabase: Error inserting password=a password user=mr new user
    android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: user.user (code 2067 SQLITE_CONSTRAINT_UNIQUE)
        at 
   .............
2019-02-22 19:13:49.209 28866-28866/mjt.so54807516 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@34252b0
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out: 0 {
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out:    type=table
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out:    name=user
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out:    tbl_name=user
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out:    rootpage=2
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out:    sql=CREATE TABLE "user" (
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out:   "_id" INTEGER NOT NULL,
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out:   "user" TEXT,
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out:   "password" TEXT,
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out:   "email" TEXT,
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out:   PRIMARY KEY ("_id"),
2019-02-22 19:13:49.210 28866-28866/mjt.so54807516 I/System.out:   CONSTRAINT "user" UNIQUE ("user")
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out: )
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out: }
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out: 1 {
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out:    type=index
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out:    name=sqlite_autoindex_user_1
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out:    tbl_name=user
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out:    rootpage=4
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out:    sql=null
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out: }
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out: 2 {
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out:    type=table
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out:    name=android_metadata
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out:    tbl_name=android_metadata
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out:    rootpage=3
2019-02-22 19:13:49.211 28866-28866/mjt.so54807516 I/System.out:    sql=CREATE TABLE android_metadata (locale TEXT)
2019-02-22 19:13:49.212 28866-28866/mjt.so54807516 I/System.out: }
2019-02-22 19:13:49.212 28866-28866/mjt.so54807516 I/System.out: <<<<<
2019-02-22 19:13:49.212 28866-28866/mjt.so54807516 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@c8f0529
2019-02-22 19:13:49.213 28866-28866/mjt.so54807516 I/System.out: 0 {
2019-02-22 19:13:49.213 28866-28866/mjt.so54807516 I/System.out:    _id=1
2019-02-22 19:13:49.213 28866-28866/mjt.so54807516 I/System.out:    user=Fred
2019-02-22 19:13:49.213 28866-28866/mjt.so54807516 I/System.out:    password=fredpassword
2019-02-22 19:13:49.213 28866-28866/mjt.so54807516 I/System.out:    [email protected] /cdn-cgi/l/email-protection
2019-02-22 19:13:49.213 28866-28866/mjt.so54807516 I/System.out: }
2019-02-22 19:13:49.213 28866-28866/mjt.so54807516 I/System.out: 1 {
2019-02-22 19:13:49.213 28866-28866/mjt.so54807516 I/System.out:    _id=2
2019-02-22 19:13:49.213 28866-28866/mjt.so54807516 I/System.out:    
...... etc
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

哪些方法可用于管理现有数据库的不同版本? 的相关文章

随机推荐

  • IIS Express Web 服务器端口正在使用

    我通常可以毫无问题地调试我的 Web 服务 但现在我不断收到错误 无法启动 IIS Express Web 服务器 端口 3268 正在使用中 我已经看过之前的问题 但我被困住了 我使用哪个端口并不重要 重新启动 关闭网络浏览器 删除网络历
  • 如何查看URL的状态?

    我创建了一个宏 可以在其中从任何网页获取每个 URL 现在 我在列中包含了每个 URL 如何检查 URL 是否有效 如果这些 URL 中的任何一个不起作用 那么它应该在下一列中的 URL 旁边显示错误 不起作用 下面是我写的代码 Sub C
  • 让 NSRunLoop 等待设置标志的最佳方法?

    在苹果文档中NS运行循环 http developer apple com documentation Cocoa Reference Foundation Classes NSRunLoop Class Reference Referen
  • DirectoryInfo.GetFiles,如何在 C# 中获取不同类型的文件

    如何使用以下命令找到 gif 和 jpg 这两种文件类型DirectoryInfo GetFilesC 中的函数 当我尝试这段代码时 string pattern gif jpg FileInfo files dir GetFiles pa
  • 如何使用 Webpack 和 React create-react-app 在 Monaco 中加载 npm 模块类型定义

    我想在 React 应用程序中加载 Monaco 中的一些模块类型定义以用于教程目的 事实上 我在经历了很大的痛苦之后设法让它工作 但以一种非常hacky的方式 所以我不是问如何做 而是问如何正确地做 我希望能用 Webpack 解决的部分
  • cakephp:查找带有“包含”的语句

    以下用户模型函数来自 MilesJones 论坛插件 有人可以告诉我 find stmt txt 中的 contain 有什么用吗 我在 cakephp 食谱中找不到任何包含的示例 任何帮助表示赞赏 public function getP
  • 防止浏览器缓存角度模板

    我一直在研究这个问题 这很简单 现代浏览器 chrome FF 会缓存一些东西 比如 html 页面等等 当您发布新版本时 Angular 会获取这些模板 但是 由于浏览器提供这些页面的缓存版本 而不是新的更新版本 我读过大约 2000 篇
  • Python 中的滤波器设计和频率提取

    我正在开展一个项目 用 Python 查找多分量音频信号的瞬时频率 我目前正在使用巴特沃斯带通滤波器结合scipy signal lfilter提取我想要的频率区域 然后我使用分析信号 来自scipy signal hilbert 以获得瞬
  • Jquery 每 x 秒运行一次单击函数

    我有以下 jQuery
  • 最喜欢的 (G)Vim 插件/脚本? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 加载多个小型 JavaScript 文件和加载一个大型 JavaScript 文件哪个更好?

    我注意到在 Chrome 中 如果我将图像加载为 Base64 字符串 然后滚动浏览页面的该部分 速度会变慢 我还注意到 当我导航出包含 Javascript 的选项卡 然后移回该选项卡时 速度会慢几秒钟 就好像 V8 正在重新编译 js
  • Google 自定义搜索 API 自动完成?

    我们使用 Google 自定义搜索 API 付费服务器端 API 来支持我们的搜索结果 我想在搜索中添加自动完成功能 但是 有谁知道是否支持此功能 通过服务器端 API 或通过某种客户端 JSONP 我曾尝试使用 Google 自定义搜索的
  • Node.js 服务器没有响应

    我正在 Node js 主页上尝试这个简单的演示 var http require http http createServer function req res res writeHead 200 Content Type text pl
  • 如何防止应用程序在 Windows 7 中被固定?

    我试图阻止用户将我的 NET 应用程序固定到任务栏 我在上面找到了一些代码老新事物 http blogs msdn com b oldnewthing archive 2011 06 01 10170113 aspx就是这么做的 然而 它是
  • 如何在 C# 中使用箭头禁用 WinForm 上的导航?

    我需要禁用使用表单上的箭头改变焦点 有一个简单的方法可以做到吗 谢谢 大致如下 private void Form1 Load object sender EventArgs e foreach Control control in thi
  • 流星 0.5.7:如何处理/使用 Meteor.Collection.ObjectID?

    我昨天更新了流星并尝试使用新的Meteor Collection ObjectID 但此后一直没有成功 首先 我以这种方式更新了我的收藏 myCollection new Meteor Collection mycollection idG
  • 选择每一列,但重命名其中一列

    假设我有一个名为 Alphabet 的表 现在假设该表具有以下列 a b c d e z 现在我想从提到的表中选择所有内容 但选择 f 作为 aaa 但我不想这样做 select a b c d e f as aaa g z from Al
  • silverlight 画布装订

    我正在尝试创建一个画布 其中的项目位于画布上的指定位置 因为我无法将源和模板直接绑定到画布 我是否使用了 ItemsControl 但有一个问题 所有的项目都位于 0 0 我已经测试了绑定 它们不返回 0 0 我怎样才能完成这项工作 使物品
  • System.Speech.Synthesis 在 2012 R2 上因 CPU 高而挂起

    我有一个 asp net MVC 应用程序 它有一个控制器操作 该操作将字符串作为输入并发送合成语音的响应 wav 文件 这是一个简化的示例 public async Task
  • 哪些方法可用于管理现有数据库的不同版本?

    Note这旨在成为提出并回答你自己的问题 as per 以问答方式分享您的知识 https stackoverflow blog 2011 07 01 its ok to ask and answer your own questions