Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes it is,</p> <p>You need to write your <code>onUpgrade()</code> method when you update your table. Currently, I use the following to create a new table with the new column and to copy all my current data over. Hopefully you can adapt this to your code.</p> <pre><code>@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion); db.beginTransaction(); try { db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_UPGRADE); List&lt;String&gt; columns = GetColumns(db, DATABASE_TABLE); db.execSQL("ALTER table " + DATABASE_TABLE + " RENAME TO 'temp_" + DATABASE_TABLE + "'"); db.execSQL("create table " + DATABASE_UPGRADE); columns.retainAll(GetColumns(db, DATABASE_TABLE)); String cols = join(columns, ","); db.execSQL(String.format( "INSERT INTO %s (%s) SELECT %s from temp_%s", DATABASE_TABLE, cols, cols, DATABASE_TABLE)); db.execSQL("DROP table 'temp_" + DATABASE_TABLE + "'"); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } } public static List&lt;String&gt; GetColumns(SQLiteDatabase db, String tableName) { List&lt;String&gt; ar = null; Cursor c = null; try { c = db.rawQuery("select * from " + tableName + " limit 1", null); if (c != null) { ar = new ArrayList&lt;String&gt;(Arrays.asList(c.getColumnNames())); } } catch (Exception e) { Log.v(tableName, e.getMessage(), e); e.printStackTrace(); } finally { if (c != null) c.close(); } return ar; } public static String join(List&lt;String&gt; list, String delim) { StringBuilder buf = new StringBuilder(); int num = list.size(); for (int i = 0; i &lt; num; i++) { if (i != 0) buf.append(delim); buf.append((String) list.get(i)); } return buf.toString(); } </code></pre> <p>This contains <code>onUpgrade()</code> and two helper methods. <code>DATABASE_UPGRADE</code> is a string that contains the upgrade database:</p> <pre><code>private static final String DATABASE_UPGRADE = "notes (_id integer primary key autoincrement, " + "title text not null, " + "body text not null, " + "date text not null, " + "edit text not null, " + "reminder text, " + "img_source text, " + "deletion, " + "priority)"; </code></pre> <p>Quick note about how this works:</p> <ol> <li>Check if current version of the table already exists (it never should) as <code>onUpgrade()</code> shouldn't get called in this case.</li> <li>Since that failed, get the list of columns from the current table (uses helper function <code>GetColumns()</code>).</li> <li>rename the old table to temp_OLDTABLENAME.</li> <li>Create the a new version of the database additional columns (currently empty).</li> <li>get all information from the old version of the database.</li> <li>insert it into new database</li> <li>drop the old table (temp_OLDTABLENAME).</li> </ol> <p>I tried to write this generic enough so all i have to do is update DATABASE_UPGRADE with the additional columns and this handles all the rest. It has worked for me through 3 upgrade so far.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload