{"id":940,"date":"2011-01-28T01:00:00","date_gmt":"2011-01-27T16:00:00","guid":{"rendered":"https:\/\/ecoop.net\/memo\/2011-01-28-1.html"},"modified":"2013-09-15T00:19:28","modified_gmt":"2013-09-14T15:19:28","slug":"2011-01-28-1","status":"publish","type":"post","link":"https:\/\/www.ecoop.net\/memo\/archives\/2011-01-28-1.html","title":{"rendered":"SQLiteOpenHelper \u3067\u30c6\u30fc\u30d6\u30eb\u30ab\u30e9\u30e0\u5909\u66f4\u6642\u306b\u30c7\u30fc\u30bf\u3092\u5f15\u304d\u7d99\u3050"},"content":{"rendered":"<p>\nSQLiteOpenHelper \u306e\u5b9f\u88c5\u30b5\u30f3\u30d7\u30eb\u3067\u3059\u3002<br \/>\nonUpgrade() \u306e\u5b9f\u88c5\u306f DROP TABLE \u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3059\u308b\u306e\u304c\u7c21\u5358\u306a\u306e\u3067\u3059\u304c\u3001<br \/>\n\u305d\u308c\u3060\u3068DB\u30d0\u30fc\u30b8\u30e7\u30f3\u30a2\u30c3\u30d7\u3054\u3068\u306b\u30c7\u30fc\u30bf\u304c\u30ea\u30bb\u30c3\u30c8\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002<br \/>\n&nbsp;<br \/>\n\u30d0\u30fc\u30b8\u30e7\u30f3\u30a2\u30c3\u30d7\u3067\u30ab\u30e9\u30e0\u8ffd\u52a0\u3001\u524a\u9664\u3092\u884c\u3063\u305f\u6642\u3082\u5171\u901a\u30ab\u30e9\u30e0\u306b\u3064\u3044\u3066\u306f\u5f15\u304d\u7d99\u3052\u308b\u3088\u3046\u306b\u3001<a href=\"http:\/\/stackoverflow.com\/questions\/3505900\/sqliteopenhelper-onupgrade-confusion-android\">\u3053\u3061\u3089<\/a>\u306e\u56de\u7b54\u3092\u53c2\u8003\u306b\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<div class=\"readmore\">\n<\/p>\n<blockquote><p>import java.util.ArrayList;<br \/>\nimport java.util.Arrays;<br \/>\nimport java.util.List;<br \/>\n&nbsp;<br \/>\nimport android.content.Context;<br \/>\nimport android.database.Cursor;<br \/>\nimport android.database.sqlite.SQLiteDatabase;<br \/>\nimport android.database.sqlite.SQLiteDatabase.CursorFactory;<br \/>\nimport android.database.sqlite.SQLiteOpenHelper;<br \/>\n&nbsp;<br \/>\nclass DatabaseHelper extends SQLiteOpenHelper {<br \/>\n\/**<br \/>\n&nbsp;* DB\u30d5\u30a1\u30a4\u30eb\u540d<br \/>\n&nbsp;*\/<br \/>\nprivate static final String DATABASE_NAME = &#8220;sample.db&#8221;;<br \/>\n\/**<br \/>\n&nbsp;* DB\u30d0\u30fc\u30b8\u30e7\u30f3<br \/>\n&nbsp;* (\u65e2\u5b58\u306eDB\u3068\u30d0\u30fc\u30b8\u30e7\u30f3\u5024\u304c\u7570\u306a\u308b\u6642\u3060\u3051 onUpgrade()\u304c\u547c\u3070\u308c\u308b)<br \/>\n&nbsp;*\/<br \/>\nprivate static final int DATABASE_VERSION = 1;<br \/>\n\/**<br \/>\n&nbsp;* \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf<br \/>\n&nbsp;*\/<br \/>\npublic DatabaseHelper(Context context) {<br \/>\n\tsuper(context, DATABASE_NAME, null, DATABASE_VERSION);<br \/>\n}<br \/>\n&nbsp;<br \/>\n@Override<br \/>\npublic void onCreate(SQLiteDatabase db) {<br \/>\n\tdb.execSQL(&#8220;CREATE TABLE table1 (&#8221; <br \/>\n\t\t\t+ &#8220;name text NOT NULL, &#8221; <br \/>\n\t\t\t+ &#8220;birthdate INTEGER NOT NULL&#8221; <br \/>\n\t\t\t+ &#8220;)&#8221;);<br \/>\n}<br \/>\n&nbsp;<br \/>\n@Override<br \/>\npublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {<br \/>\n\t\/\/ \u6307\u5b9a\u3057\u305f\u30c6\u30fc\u30d6\u30eb\u306e\u30ab\u30e9\u30e0\u69cb\u6210\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3001<br \/>\n\t\/\/ \u540c\u540d\u306e\u30ab\u30e9\u30e0\u306b\u3064\u3044\u3066\u306f\u30a2\u30c3\u30d7\u30b0\u30ec\u30fc\u30c9\u5f8c\u3082\u30c7\u30fc\u30bf\u3092\u5f15\u304d\u7d99\u304e\u307e\u3059\u3002<br \/>\n\t\/\/ \u540c\u540d\u306e\u30ab\u30e9\u30e0\u3067\u578b\u306b\u4e92\u63db\u6027\u304c\u306a\u3044\u5834\u5408\u306f\u30a8\u30e9\u30fc\u306b\u306a\u308b\u306e\u3067\u6ce8\u610f\u3002<\/p>\n<p>\t\/\/ \u66f4\u65b0\u5bfe\u8c61\u306e\u30c6\u30fc\u30d6\u30eb<br \/>\n\tfinal String targetTable = &#8220;table1&#8221;;<br \/>\n\tdb.beginTransaction();<br \/>\n\ttry {<br \/>\n\t\t\/\/ \u5143\u30ab\u30e9\u30e0\u4e00\u89a7<br \/>\n\t\tfinal List&lt;String> columns = getColumns(db, targetTable);<br \/>\n\t\t\/\/ \u521d\u671f\u5316<br \/>\n\t\tdb.execSQL(&#8220;ALTER TABLE &#8221; + targetTable + &#8221; RENAME TO temp_&#8221;<br \/>\n\t\t\t\t+ targetTable);<br \/>\n\t\tonCreate(db);<br \/>\n\t\t\/\/ \u65b0\u30ab\u30e9\u30e0\u4e00\u89a7<br \/>\n\t\tfinal List&lt;String> newColumns = getColumns(db, targetTable);<br \/>\n&nbsp;<br \/>\n\t\t\/\/ \u5909\u5316\u3057\u306a\u3044\u30ab\u30e9\u30e0\u306e\u307f\u62bd\u51fa<br \/>\n\t\tcolumns.retainAll(newColumns);<br \/>\n&nbsp;<br \/>\n\t\t\/\/ \u5171\u901a\u30c7\u30fc\u30bf\u3092\u79fb\u3059\u3002(OLD\u306b\u3057\u304b\u5b58\u5728\u3057\u306a\u3044\u3082\u306e\u306f\u6368\u3066\u3089\u308c, NEW\u306b\u3057\u304b\u5b58\u5728\u3057\u306a\u3044\u3082\u306e\u306fNULL\u306b\u306a\u308b)<br \/>\n\t\tfinal String cols = join(columns, &#8220;,&#8221;);<br \/>\n\t\tdb.execSQL(String.format(<br \/>\n\t\t\t\t&#8220;INSERT INTO %s (%s) SELECT %s from temp_%s&#8221;, targetTable,<br \/>\n\t\t\t\tcols, cols, targetTable));<br \/>\n\t\t\/\/ \u7d42\u4e86\u51e6\u7406<br \/>\n\t\tdb.execSQL(&#8220;DROP TABLE temp_&#8221; + targetTable);<br \/>\n\t\tdb.setTransactionSuccessful();<br \/>\n\t} finally {<br \/>\n\t\tdb.endTransaction();<br \/>\n\t}<br \/>\n}<br \/>\n&nbsp;<br \/>\n\/**<br \/>\n&nbsp;* \u6307\u5b9a\u3057\u305f\u30c6\u30fc\u30d6\u30eb\u306e\u30ab\u30e9\u30e0\u540d\u30ea\u30b9\u30c8\u3092\u53d6\u5f97\u3059\u308b\u3002<br \/>\n&nbsp;* <br \/>\n&nbsp;* @param db<br \/>\n&nbsp;* @param tableName<br \/>\n&nbsp;* @return \u30ab\u30e9\u30e0\u540d\u306e\u30ea\u30b9\u30c8<br \/>\n&nbsp;*\/<br \/>\nprivate static List&lt;String> getColumns(SQLiteDatabase db, String tableName) {<br \/>\n\tList&lt;String> ar = null;<br \/>\n\tCursor c = null;<br \/>\n\ttry {<br \/>\n\t\tc = db.rawQuery(&#8220;SELECT * FROM &#8221; + tableName + &#8221; LIMIT 1&#8243;, null);<br \/>\n\t\tif (c != null) {<br \/>\n\t\t\tar = new ArrayList&lt;String>(Arrays.asList(c.getColumnNames()));<br \/>\n\t\t}<br \/>\n\t} finally {<br \/>\n\t\tif (c != null)<br \/>\n\t\t\tc.close();<br \/>\n\t}<br \/>\n\treturn ar;<br \/>\n}<br \/>\n&nbsp;<br \/>\n\/**<br \/>\n&nbsp;* \u6587\u5b57\u5217\u3092\u4efb\u610f\u306e\u533a\u5207\u308a\u6587\u5b57\u3067\u9023\u7d50\u3059\u308b\u3002<br \/>\n&nbsp;* <br \/>\n&nbsp;* @param list<br \/>\n&nbsp;*            \u6587\u5b57\u5217\u306e\u30ea\u30b9\u30c8<br \/>\n&nbsp;* @param delim<br \/>\n&nbsp;*            \u533a\u5207\u308a\u6587\u5b57<br \/>\n&nbsp;* @return \u9023\u7d50\u5f8c\u306e\u6587\u5b57\u5217<br \/>\n&nbsp;*\/<br \/>\nprivate static String join(List&lt;String> list, String delim) {<br \/>\n\tfinal StringBuilder buf = new StringBuilder();<br \/>\n\tfinal int num = list.size();<br \/>\n\tfor (int i = 0; i < num; i++) {<br \/>\n\t\tif (i != 0)<br \/>\n\t\t\tbuf.append(delim);<br \/>\n\t\tbuf.append((String) list.get(i));<br \/>\n\t}<br \/>\n\treturn buf.toString();<br \/>\n}<br \/>\n&nbsp;<br \/>\n}\n<\/p><\/blockquote>\n<p><a href=\"http:\/\/developer.android.com\/reference\/android\/database\/sqlite\/SQLiteOpenHelper.html\">SQLiteOpenHelper<\/a>(Android Developers)<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/3505900\/sqliteopenhelper-onupgrade-confusion-android\">SQLiteOpenHelper onUpgrade() Confusion Android<\/a>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>SQLiteOpenHelper \u306e\u5b9f\u88c5\u30b5\u30f3\u30d7\u30eb\u3067\u3059\u3002 onUpgrade() \u306e\u5b9f\u88c5\u306f DROP TABLE \u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3059\u308b\u306e\u304c\u7c21\u5358\u306a\u306e\u3067\u3059\u304c\u3001 \u305d\u308c\u3060\u3068DB\u30d0\u30fc\u30b8\u30e7\u30f3\u30a2\u30c3\u30d7\u3054\u3068\u306b\u30c7\u30fc\u30bf\u304c\u30ea\u30bb\u30c3\u30c8\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002  &hellip; <a href=\"https:\/\/www.ecoop.net\/memo\/archives\/2011-01-28-1.html\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">SQLiteOpenHelper \u3067\u30c6\u30fc\u30d6\u30eb\u30ab\u30e9\u30e0\u5909\u66f4\u6642\u306b\u30c7\u30fc\u30bf\u3092\u5f15\u304d\u7d99\u3050<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85],"tags":[65,41],"class_list":["post-940","post","type-post","status-publish","format-standard","hentry","category-android-dev","tag-android","tag-sqlite"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/comments?post=940"}],"version-history":[{"count":0,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/940\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/media?parent=940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/categories?post=940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/tags?post=940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}