有必要进行更新,但是这样用户才能保存数据.表,列的名称保持不变.仅表中的条目数增加了.我增加了DATABASE_VERSION.将数据库放入存档.如说明中所创建的文件-brodsky.db_upgrade_1-2.sql ALTER TABLE "poems_table" RENAME TO 'poems_table_TMP'; CREATE TABLE "poems_table" ( "id" long NOT NULL, "title" text, "poem" text, "subject" text, "years" text, "favorite" text, PRIMARY KEY ("id") ); INSERT INTO "poems_table" ("id", "title", "poem", "subject", "years", "favorite") SELECT "id", "title", "poem", "subject", "years", "favorite" FROM "poems_table_TMP"; DROP TABLE "poems_table_TMP"; DbHelper public class PoemsDbHelper extends SQLiteAssetHelper { private static String DB_NAME = "brodsky.db"; private static final int DB_VERSION = 2; public PoemsDbHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); }}什么都没有改变.显示旧数据解决方案从您的代码中,并假定代码在onUpgrade方法中,并且版本从1更改为2,则该代码将不起作用.就是这样:-重命名旧数据库的poems_table创建一个新的poems_table将重命名表的内容复制到新创建的表中删除重命名的poems_table.在任何地方都无法访问较新版本的brodsky.db(我相信)您需要做的是打开较新的brodsky.db并从该数据库复制数据.工作示例以下是此类工作示例的代码.这将在数据库版本增加时(为简便起见,进行任何升级):-将新的更新数据库复制为单独的/附加的数据库,然后尝试将行从更新的数据库复制到现有数据库仅当它们不存在时(基于id列以外的所有列,因为最终用户可能添加了诗歌并因此使用了id).核心代码位于 PoemsDbHelper.java 中,并且位于:-public class PoemsDbHelper extends SQLiteAssetHelper { public static final String DBNAME = "brodsky.db"; public static final int DBVERSION = 1; public static final String TBLNAME = "poems_table"; public static final String COL_ID = "id"; public static final String COL_TITLE = "title"; public static final String COl_POEM = "poem"; public static final String COL_SUBJECT = "subject"; public static final String COL_YEARS = "years"; public static final String COL_FAVOURITE = "favorite"; Context mContext; public PoemsDbHelper(Context context) { super(context, DBNAME, null, DBVERSION); mContext = context; } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { getNewPoems(mContext, db); //<<<<<<<<<< get the new poems when upgraded } private void getNewPoems(Context context, SQLiteDatabase db) { Log.d("GETNEWPOEMS","Initiating getting new poems due to Database version increased."); // Prepare to copy the updated database from the assets folder InputStream is; OutputStream os; final String tempnewdbname = "tempbrodsky.db"; int buffersize = 4096; byte[] buffer = new byte[buffersize]; String newDBPath = mContext.getDatabasePath(tempnewdbname).getPath(); // If a copied version of the updated database exists then delete it // This should not be required but better safe than sorry File newDBFile = new File(newDBPath); if (newDBFile.exists()) { newDBFile.delete(); } // Just in case create the databases directory (it should exist) File newDBFileDirectory = newDBFile.getParentFile(); if (!newDBFileDirectory.exists()) { newDBFileDirectory.mkdirs(); } // Preapre to copy update database from the assets folder try { is = context.getAssets().open("databases/" + DBNAME); os = new FileOutputStream(newDBFile); int bytes_read; while ((bytes_read = is.read(buffer,0,buffersize)) > 0) { os.write(buffer); } os.flush(); os.close(); is.close(); }catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Ouch updated database not copied - processing stopped - see stack-trace above."); } long id = maxid(db) + 1; // Get the next available id SQLiteDatabase newdb = SQLiteDatabase.openDatabase(newDBFile.getPath(),null,SQLiteDatabase.OPEN_READONLY); Cursor csr = newdb.query(TBLNAME,null,null,null,null,null,null); long insert_result; db.beginTransaction(); while (csr.moveToNext()) { insert_result = insertCorePoem( db, id, csr.getString(csr.getColumnIndex(COL_TITLE)), csr.getString(csr.getColumnIndex(COl_POEM)), csr.getString(csr.getColumnIndex(COL_SUBJECT)), csr.getString(csr.getColumnIndex(COL_YEARS)), csr.getString(csr.getColumnIndex(COL_FAVOURITE)) ); // If the row was inserted then increment the if ready for the next insert // If not inserted (result = -2) then leave id as it is as it was unused if (insert_result > 0) { id++; } } db.setTransactionSuccessful(); db.endTransaction(); csr.close(); newDBFile.delete(); // Delete the copied database as no longer required } public long insertCorePoem(SQLiteDatabase db, long id, String title, String poem, String subject, String years, String favourite) { String whereclause = COL_TITLE + "=? AND " + COl_POEM + "=? AND " + COL_SUBJECT + "=? AND " + COL_YEARS + "=?"; String[] whereargs = new String[]{ title, poem, subject, years }; Cursor csr = db.query(TBLNAME,null,whereclause,whereargs,null,null,null); boolean rowexists = (csr.getCount() > 0); csr.close(); if (rowexists) { Log.d("INSERTCOREPOEM","Skipping insert of row"); return -2; // Don't insert if the poem already exists } ContentValues cv = new ContentValues(); cv.put(COL_ID,id); cv.put(COL_TITLE,title); cv.put(COl_POEM,poem); cv.put(COL_SUBJECT,subject); cv.put(COL_YEARS,years); cv.put(COL_FAVOURITE,favourite); Log.d("INSERTCOREPOEM","Inserting new column with id " + String.valueOf(id)); return db.insert(TBLNAME, null, cv); } private long maxid(SQLiteDatabase db) { long rv = 0; String extractcolumn = "maxid"; String[] col = new String[]{"max(" + COL_ID + ") AS " + extractcolumn}; Cursor csr = db.query(TBLNAME,col,null,null,null,null,null); if (csr.moveToFirst()) { rv = csr.getLong(csr.getColumnIndex(extractcolumn)); } csr.close(); return rv; } public Cursor getAllPoems() { SQLiteDatabase db = this.getWritableDatabase(); return db.query(TBLNAME,null,null,null,null,null,null); }} getNewPoems 是执行上述操作的主要方法(请注意如何从onUpgrade方法中调用它).这将从资产文件夹中复制更新的数据库,然后提取所有诗歌(应用程序()附带的核心诗歌).准备插入列的方法使用 maxid 方法从现有数据库中获取当前的最高ID,并添加1,以便新的第一个ID是唯一的.如果要插入的行已经存在,则尝试插入每行,但是将跳过该行.这是通过 insertCorePoem 方法确定的.方法 getAllPoems 返回一个游标(由调用活动使用). 注意,您可能还应该包含其他现有方法.测试使用三首诗和一个外部工具创建了一个名为brodsky.db的数据库.这已被复制到资产文件夹的数据库文件夹中.在调用活动中使用了以下内容:-public class MainActivity extends AppCompatActivity { PoemsDbHelper mDBHlpr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDBHlpr = new PoemsDbHelper(this); Log.d("DBVERSION", "Database version = " + String.valueOf(PoemsDbHelper.DBVERSION)); Cursor csr = mDBHlpr.getAllPoems(); DatabaseUtils.dumpCursor(csr); csr.close(); }}第1阶段-验证/检查现有数据库首次运行时,将从资产文件夹中复制数据库(包含3首诗),并且包含日志(与在此阶段运行该应用程序一样):-12-18 06:19:58.505 3574-3574/? D/DBVERSION: Database version = 112-18 06:19:58.505 3574-3574/? W/SQLiteAssetHelper: copying database from assets...12-18 06:19:58.505 3574-3574/? W/SQLiteAssetHelper: database copy complete12-18 06:19:58.521 3574-3574/? I/SQLiteAssetHelper: successfully opened database brodsky.db12-18 06:19:58.521 3574-3574/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534758c812-18 06:19:58.521 3574-3574/? I/System.out: 0 {12-18 06:19:58.521 3574-3574/? I/System.out: id=112-18 06:19:58.521 3574-3574/? I/System.out: title=A Poem12-18 06:19:58.521 3574-3574/? I/System.out: poem=This is a poem12-18 06:19:58.521 3574-3574/? I/System.out: subject=poem12-18 06:19:58.521 3574-3574/? I/System.out: years=201812-18 06:19:58.521 3574-3574/? I/System.out: favorite=NO12-18 06:19:58.521 3574-3574/? I/System.out: }12-18 06:19:58.521 3574-3574/? I/System.out: 1 {12-18 06:19:58.521 3574-3574/? I/System.out: id=212-18 06:19:58.521 3574-3574/? I/System.out: title=Another Poem12-18 06:19:58.521 3574-3574/? I/System.out: poem=This is another poem12-18 06:19:58.521 3574-3574/? I/System.out: subject=another poem12-18 06:19:58.521 3574-3574/? I/System.out: years=201712-18 06:19:58.521 3574-3574/? I/System.out: favorite=NO12-18 06:19:58.521 3574-3574/? I/System.out: }12-18 06:19:58.521 3574-3574/? I/System.out: 2 {12-18 06:19:58.521 3574-3574/? I/System.out: id=312-18 06:19:58.521 3574-3574/? I/System.out: title=the Third Poem12-18 06:19:58.521 3574-3574/? I/System.out: poem=This is the third poem12-18 06:19:58.521 3574-3574/? I/System.out: subject=third poem12-18 06:19:58.521 3574-3574/? I/System.out: years=201812-18 06:19:58.521 3574-3574/? I/System.out: favorite=NO12-18 06:19:58.521 3574-3574/? I/System.out: }12-18 06:19:58.521 3574-3574/? I/System.out: <<<<<第2阶段-使用更新的数据库(但版本号未更改).使用SQlite工具将另外3行添加到数据库中资产文件夹中的现有数据库已重命名(易于还原以进行测试调试).已更新的数据库已复制到资产文件夹中的databases文件夹中.导致:-然后重新运行了该应用程序,但没有更改版本号作为中间检查.结果高于预期,即版本号未更改,onUpgrade方法未运行.阶段3-更改版本号.版本号从1增加到2,并且App运行.导致按以下方式添加了3行:-12-18 06:24:46.973 3689-3689/? D/DBVERSION: Database version = 212-18 06:24:46.981 3689-3689/? I/SQLiteAssetHelper: successfully opened database brodsky.db12-18 06:24:46.981 3689-3689/? D/GETNEWPOEMS: Initiating getting new poems due to Database version increased.12-18 06:24:46.981 3689-3689/? D/INSERTCOREPOEM: Skipping insert of row12-18 06:24:46.985 3689-3689/? D/INSERTCOREPOEM: Skipping insert of row12-18 06:24:46.985 3689-3689/? D/INSERTCOREPOEM: Skipping insert of row12-18 06:24:46.985 3689-3689/? D/INSERTCOREPOEM: Inserting new column with id 412-18 06:24:46.985 3689-3689/? D/INSERTCOREPOEM: Inserting new column with id 512-18 06:24:46.985 3689-3689/? D/INSERTCOREPOEM: Inserting new column with id 612-18 06:24:46.993 3689-3689/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5346206012-18 06:24:46.993 3689-3689/? I/System.out: 0 {12-18 06:24:46.993 3689-3689/? I/System.out: id=112-18 06:24:46.993 3689-3689/? I/System.out: title=A Poem12-18 06:24:46.993 3689-3689/? I/System.out: poem=This is a poem12-18 06:24:46.993 3689-3689/? I/System.out: subject=poem12-18 06:24:46.993 3689-3689/? I/System.out: years=201812-18 06:24:46.993 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.993 3689-3689/? I/System.out: }12-18 06:24:46.993 3689-3689/? I/System.out: 1 {12-18 06:24:46.993 3689-3689/? I/System.out: id=212-18 06:24:46.993 3689-3689/? I/System.out: title=Another Poem12-18 06:24:46.993 3689-3689/? I/System.out: poem=This is another poem12-18 06:24:46.993 3689-3689/? I/System.out: subject=another poem12-18 06:24:46.993 3689-3689/? I/System.out: years=201712-18 06:24:46.993 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.993 3689-3689/? I/System.out: }12-18 06:24:46.993 3689-3689/? I/System.out: 2 {12-18 06:24:46.993 3689-3689/? I/System.out: id=312-18 06:24:46.993 3689-3689/? I/System.out: title=the Third Poem12-18 06:24:46.993 3689-3689/? I/System.out: poem=This is the third poem12-18 06:24:46.993 3689-3689/? I/System.out: subject=third poem12-18 06:24:46.993 3689-3689/? I/System.out: years=201812-18 06:24:46.993 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.993 3689-3689/? I/System.out: }12-18 06:24:46.993 3689-3689/? I/System.out: 3 {12-18 06:24:46.993 3689-3689/? I/System.out: id=412-18 06:24:46.993 3689-3689/? I/System.out: title=The Update Poem12-18 06:24:46.993 3689-3689/? I/System.out: poem=This is a new poem12-18 06:24:46.993 3689-3689/? I/System.out: subject=4th Core Poem12-18 06:24:46.993 3689-3689/? I/System.out: years=201912-18 06:24:46.993 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.993 3689-3689/? I/System.out: }12-18 06:24:46.993 3689-3689/? I/System.out: 4 {12-18 06:24:46.993 3689-3689/? I/System.out: id=512-18 06:24:46.993 3689-3689/? I/System.out: title=Another Updated Poem12-18 06:24:46.993 3689-3689/? I/System.out: poem=This is another updated poem12-18 06:24:46.997 3689-3689/? I/System.out: subject=5th Core Poem12-18 06:24:46.997 3689-3689/? I/System.out: years=201912-18 06:24:46.997 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.997 3689-3689/? I/System.out: }12-18 06:24:46.997 3689-3689/? I/System.out: 5 {12-18 06:24:46.997 3689-3689/? I/System.out: id=612-18 06:24:46.997 3689-3689/? I/System.out: title=The 3rd Updated Poem12-18 06:24:46.997 3689-3689/? I/System.out: poem=This is the 3rd updated poem12-18 06:24:46.997 3689-3689/? I/System.out: subject=6th Core Poem12-18 06:24:46.997 3689-3689/? I/System.out: years=201912-18 06:24:46.997 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.997 3689-3689/? I/System.out: }12-18 06:24:46.997 3689-3689/? I/System.out: <<<<<再次运行该应用程序,没有任何更改.将版本增加到3将导致相同的6行.但是,运行onUpgrade并尝试添加行,但是按照:-12-18 06:27:08.933 3789-3789/so53801149.so53801149poemupdatefromassets D/DBVERSION: Database version = 312-18 06:27:08.937 3789-3789/so53801149.so53801149poemupdatefromassets I/SQLiteAssetHelper: successfully opened database brodsky.db12-18 06:27:08.937 3789-3789/so53801149.so53801149poemupdatefromassets D/GETNEWPOEMS: Initiating getting new poems due to Database version increased.12-18 06:27:08.937 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.945 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.945 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.945 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.945 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.945 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.949 3789-3789/so53801149.so53801149poemupdatefromassets I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5347529c..... results same as above (6 rows) 注意,您可能需要在 insertCorePoem 方法中调整查询以适合您的需求.I use the library android-sqlite-asset-helperIt is necessary to update, but so that the user has saved data. The names of the tables, columns remained the same. Only the number of entries in the tables has increased.I increased DATABASE_VERSION. Put the database in the archive. As stated in the instructions created file - brodsky.db_upgrade_1-2.sql ALTER TABLE "poems_table" RENAME TO 'poems_table_TMP'; CREATE TABLE "poems_table" ( "id" long NOT NULL, "title" text, "poem" text, "subject" text, "years" text, "favorite" text, PRIMARY KEY ("id") ); INSERT INTO "poems_table" ("id", "title", "poem", "subject", "years", "favorite") SELECT "id", "title", "poem", "subject", "years", "favorite" FROM "poems_table_TMP"; DROP TABLE "poems_table_TMP";DbHelperpublic class PoemsDbHelper extends SQLiteAssetHelper { private static String DB_NAME = "brodsky.db"; private static final int DB_VERSION = 2; public PoemsDbHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); }}nothing changed. old data is displayed 解决方案 From your code and assuming that the code is in the onUpgrade method and that the version was changed from 1 to 2 then the code does nothing of use. That is it :-renames the poems_table of the older databasecreates a new poems_tablecopies the contents of the renamed table to the newly created tabledeletes the renamed poems_table.Nowhere does it access the newer version of the brodsky.dbWhat you need (I believe) to do is open that newer brodsky.db and copy the data from that database.Working ExampleThe following is code for such a working example.This will, when the Database version is increased (for simplicity any upgrade):-copy the new updated database as a separate/additional database andattempt to copy the rows from the updated database into the existing databaseonly if they are not existing rows (based upon all columns except the id column as the end user may have added poems and thus used id's).the core code is within PoemsDbHelper.java and is :-public class PoemsDbHelper extends SQLiteAssetHelper { public static final String DBNAME = "brodsky.db"; public static final int DBVERSION = 1; public static final String TBLNAME = "poems_table"; public static final String COL_ID = "id"; public static final String COL_TITLE = "title"; public static final String COl_POEM = "poem"; public static final String COL_SUBJECT = "subject"; public static final String COL_YEARS = "years"; public static final String COL_FAVOURITE = "favorite"; Context mContext; public PoemsDbHelper(Context context) { super(context, DBNAME, null, DBVERSION); mContext = context; } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { getNewPoems(mContext, db); //<<<<<<<<<< get the new poems when upgraded } private void getNewPoems(Context context, SQLiteDatabase db) { Log.d("GETNEWPOEMS","Initiating getting new poems due to Database version increased."); // Prepare to copy the updated database from the assets folder InputStream is; OutputStream os; final String tempnewdbname = "tempbrodsky.db"; int buffersize = 4096; byte[] buffer = new byte[buffersize]; String newDBPath = mContext.getDatabasePath(tempnewdbname).getPath(); // If a copied version of the updated database exists then delete it // This should not be required but better safe than sorry File newDBFile = new File(newDBPath); if (newDBFile.exists()) { newDBFile.delete(); } // Just in case create the databases directory (it should exist) File newDBFileDirectory = newDBFile.getParentFile(); if (!newDBFileDirectory.exists()) { newDBFileDirectory.mkdirs(); } // Preapre to copy update database from the assets folder try { is = context.getAssets().open("databases/" + DBNAME); os = new FileOutputStream(newDBFile); int bytes_read; while ((bytes_read = is.read(buffer,0,buffersize)) > 0) { os.write(buffer); } os.flush(); os.close(); is.close(); }catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Ouch updated database not copied - processing stopped - see stack-trace above."); } long id = maxid(db) + 1; // Get the next available id SQLiteDatabase newdb = SQLiteDatabase.openDatabase(newDBFile.getPath(),null,SQLiteDatabase.OPEN_READONLY); Cursor csr = newdb.query(TBLNAME,null,null,null,null,null,null); long insert_result; db.beginTransaction(); while (csr.moveToNext()) { insert_result = insertCorePoem( db, id, csr.getString(csr.getColumnIndex(COL_TITLE)), csr.getString(csr.getColumnIndex(COl_POEM)), csr.getString(csr.getColumnIndex(COL_SUBJECT)), csr.getString(csr.getColumnIndex(COL_YEARS)), csr.getString(csr.getColumnIndex(COL_FAVOURITE)) ); // If the row was inserted then increment the if ready for the next insert // If not inserted (result = -2) then leave id as it is as it was unused if (insert_result > 0) { id++; } } db.setTransactionSuccessful(); db.endTransaction(); csr.close(); newDBFile.delete(); // Delete the copied database as no longer required } public long insertCorePoem(SQLiteDatabase db, long id, String title, String poem, String subject, String years, String favourite) { String whereclause = COL_TITLE + "=? AND " + COl_POEM + "=? AND " + COL_SUBJECT + "=? AND " + COL_YEARS + "=?"; String[] whereargs = new String[]{ title, poem, subject, years }; Cursor csr = db.query(TBLNAME,null,whereclause,whereargs,null,null,null); boolean rowexists = (csr.getCount() > 0); csr.close(); if (rowexists) { Log.d("INSERTCOREPOEM","Skipping insert of row"); return -2; // Don't insert if the poem already exists } ContentValues cv = new ContentValues(); cv.put(COL_ID,id); cv.put(COL_TITLE,title); cv.put(COl_POEM,poem); cv.put(COL_SUBJECT,subject); cv.put(COL_YEARS,years); cv.put(COL_FAVOURITE,favourite); Log.d("INSERTCOREPOEM","Inserting new column with id " + String.valueOf(id)); return db.insert(TBLNAME, null, cv); } private long maxid(SQLiteDatabase db) { long rv = 0; String extractcolumn = "maxid"; String[] col = new String[]{"max(" + COL_ID + ") AS " + extractcolumn}; Cursor csr = db.query(TBLNAME,col,null,null,null,null,null); if (csr.moveToFirst()) { rv = csr.getLong(csr.getColumnIndex(extractcolumn)); } csr.close(); return rv; } public Cursor getAllPoems() { SQLiteDatabase db = this.getWritableDatabase(); return db.query(TBLNAME,null,null,null,null,null,null); }}getNewPoems is the main method that performs the above (note how it is called from within the onUpgrade method). This copies the updated database from the assets folder, and then extracts all of the poems (the core poems supplied with the app ()).Prepares to insert columns gets the current highest id from the existing database using the maxid method and adds 1 so that the new first id is unique.Attempts to insert every row BUT if a row to be inserted already exists then it will be skipped. This being determined in the insertCorePoem method.the method getAllPoems returns a cursor (used by the invoking activity).Note you may have other existing methods that should be included.TestingA database named brodsky.db was created using an external tool with 3 poems. This was copied into the database folder of the assets folder.The following was used in an invoking activity :-public class MainActivity extends AppCompatActivity { PoemsDbHelper mDBHlpr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDBHlpr = new PoemsDbHelper(this); Log.d("DBVERSION", "Database version = " + String.valueOf(PoemsDbHelper.DBVERSION)); Cursor csr = mDBHlpr.getAllPoems(); DatabaseUtils.dumpCursor(csr); csr.close(); }}Stage 1 - verifying/inspecting the existing databaseWhen first run the database (with 3 poems) is copied from the assets folder and the log contains (as it does whenever the App is run at this stage) :-12-18 06:19:58.505 3574-3574/? D/DBVERSION: Database version = 112-18 06:19:58.505 3574-3574/? W/SQLiteAssetHelper: copying database from assets...12-18 06:19:58.505 3574-3574/? W/SQLiteAssetHelper: database copy complete12-18 06:19:58.521 3574-3574/? I/SQLiteAssetHelper: successfully opened database brodsky.db12-18 06:19:58.521 3574-3574/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@534758c812-18 06:19:58.521 3574-3574/? I/System.out: 0 {12-18 06:19:58.521 3574-3574/? I/System.out: id=112-18 06:19:58.521 3574-3574/? I/System.out: title=A Poem12-18 06:19:58.521 3574-3574/? I/System.out: poem=This is a poem12-18 06:19:58.521 3574-3574/? I/System.out: subject=poem12-18 06:19:58.521 3574-3574/? I/System.out: years=201812-18 06:19:58.521 3574-3574/? I/System.out: favorite=NO12-18 06:19:58.521 3574-3574/? I/System.out: }12-18 06:19:58.521 3574-3574/? I/System.out: 1 {12-18 06:19:58.521 3574-3574/? I/System.out: id=212-18 06:19:58.521 3574-3574/? I/System.out: title=Another Poem12-18 06:19:58.521 3574-3574/? I/System.out: poem=This is another poem12-18 06:19:58.521 3574-3574/? I/System.out: subject=another poem12-18 06:19:58.521 3574-3574/? I/System.out: years=201712-18 06:19:58.521 3574-3574/? I/System.out: favorite=NO12-18 06:19:58.521 3574-3574/? I/System.out: }12-18 06:19:58.521 3574-3574/? I/System.out: 2 {12-18 06:19:58.521 3574-3574/? I/System.out: id=312-18 06:19:58.521 3574-3574/? I/System.out: title=the Third Poem12-18 06:19:58.521 3574-3574/? I/System.out: poem=This is the third poem12-18 06:19:58.521 3574-3574/? I/System.out: subject=third poem12-18 06:19:58.521 3574-3574/? I/System.out: years=201812-18 06:19:58.521 3574-3574/? I/System.out: favorite=NO12-18 06:19:58.521 3574-3574/? I/System.out: }12-18 06:19:58.521 3574-3574/? I/System.out: <<<<<Stage 2 - Using an updated database (but version number not changed).3 additional rows were added to the database using the SQlite toolThe existing database in the assets folder was renamed (easy to revert back for testing debugging).The updated database was copied into the databases folder in the assets folder.resulting in :-The App was then rerun, but the version number hadn't been changed as an intermediate check. The results were above as expected i.e. as the version number wasn't changed the onUpgrade method wasn't run.Stage 3 - Changing the version number.The version number was increased from 1 to 2 and the App run.resulting in the 3 rows being added as per :-12-18 06:24:46.973 3689-3689/? D/DBVERSION: Database version = 212-18 06:24:46.981 3689-3689/? I/SQLiteAssetHelper: successfully opened database brodsky.db12-18 06:24:46.981 3689-3689/? D/GETNEWPOEMS: Initiating getting new poems due to Database version increased.12-18 06:24:46.981 3689-3689/? D/INSERTCOREPOEM: Skipping insert of row12-18 06:24:46.985 3689-3689/? D/INSERTCOREPOEM: Skipping insert of row12-18 06:24:46.985 3689-3689/? D/INSERTCOREPOEM: Skipping insert of row12-18 06:24:46.985 3689-3689/? D/INSERTCOREPOEM: Inserting new column with id 412-18 06:24:46.985 3689-3689/? D/INSERTCOREPOEM: Inserting new column with id 512-18 06:24:46.985 3689-3689/? D/INSERTCOREPOEM: Inserting new column with id 612-18 06:24:46.993 3689-3689/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5346206012-18 06:24:46.993 3689-3689/? I/System.out: 0 {12-18 06:24:46.993 3689-3689/? I/System.out: id=112-18 06:24:46.993 3689-3689/? I/System.out: title=A Poem12-18 06:24:46.993 3689-3689/? I/System.out: poem=This is a poem12-18 06:24:46.993 3689-3689/? I/System.out: subject=poem12-18 06:24:46.993 3689-3689/? I/System.out: years=201812-18 06:24:46.993 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.993 3689-3689/? I/System.out: }12-18 06:24:46.993 3689-3689/? I/System.out: 1 {12-18 06:24:46.993 3689-3689/? I/System.out: id=212-18 06:24:46.993 3689-3689/? I/System.out: title=Another Poem12-18 06:24:46.993 3689-3689/? I/System.out: poem=This is another poem12-18 06:24:46.993 3689-3689/? I/System.out: subject=another poem12-18 06:24:46.993 3689-3689/? I/System.out: years=201712-18 06:24:46.993 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.993 3689-3689/? I/System.out: }12-18 06:24:46.993 3689-3689/? I/System.out: 2 {12-18 06:24:46.993 3689-3689/? I/System.out: id=312-18 06:24:46.993 3689-3689/? I/System.out: title=the Third Poem12-18 06:24:46.993 3689-3689/? I/System.out: poem=This is the third poem12-18 06:24:46.993 3689-3689/? I/System.out: subject=third poem12-18 06:24:46.993 3689-3689/? I/System.out: years=201812-18 06:24:46.993 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.993 3689-3689/? I/System.out: }12-18 06:24:46.993 3689-3689/? I/System.out: 3 {12-18 06:24:46.993 3689-3689/? I/System.out: id=412-18 06:24:46.993 3689-3689/? I/System.out: title=The Update Poem12-18 06:24:46.993 3689-3689/? I/System.out: poem=This is a new poem12-18 06:24:46.993 3689-3689/? I/System.out: subject=4th Core Poem12-18 06:24:46.993 3689-3689/? I/System.out: years=201912-18 06:24:46.993 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.993 3689-3689/? I/System.out: }12-18 06:24:46.993 3689-3689/? I/System.out: 4 {12-18 06:24:46.993 3689-3689/? I/System.out: id=512-18 06:24:46.993 3689-3689/? I/System.out: title=Another Updated Poem12-18 06:24:46.993 3689-3689/? I/System.out: poem=This is another updated poem12-18 06:24:46.997 3689-3689/? I/System.out: subject=5th Core Poem12-18 06:24:46.997 3689-3689/? I/System.out: years=201912-18 06:24:46.997 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.997 3689-3689/? I/System.out: }12-18 06:24:46.997 3689-3689/? I/System.out: 5 {12-18 06:24:46.997 3689-3689/? I/System.out: id=612-18 06:24:46.997 3689-3689/? I/System.out: title=The 3rd Updated Poem12-18 06:24:46.997 3689-3689/? I/System.out: poem=This is the 3rd updated poem12-18 06:24:46.997 3689-3689/? I/System.out: subject=6th Core Poem12-18 06:24:46.997 3689-3689/? I/System.out: years=201912-18 06:24:46.997 3689-3689/? I/System.out: favorite=NO12-18 06:24:46.997 3689-3689/? I/System.out: }12-18 06:24:46.997 3689-3689/? I/System.out: <<<<<Running the App again and nothing is changed.Increasing the version to 3 results in the same 6 rows. However, onUpgrade runs and an attempt is made to add the rows but they are all skipped, as per :-12-18 06:27:08.933 3789-3789/so53801149.so53801149poemupdatefromassets D/DBVERSION: Database version = 312-18 06:27:08.937 3789-3789/so53801149.so53801149poemupdatefromassets I/SQLiteAssetHelper: successfully opened database brodsky.db12-18 06:27:08.937 3789-3789/so53801149.so53801149poemupdatefromassets D/GETNEWPOEMS: Initiating getting new poems due to Database version increased.12-18 06:27:08.937 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.945 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.945 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.945 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.945 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.945 3789-3789/so53801149.so53801149poemupdatefromassets D/INSERTCOREPOEM: Skipping insert of row12-18 06:27:08.949 3789-3789/so53801149.so53801149poemupdatefromassets I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5347529c..... results same as above (6 rows)Note you may need to tailor the query in the insertCorePoem method to suit your needs. 这篇关于更新数据库. Sqlite-asset-helper库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 18:50