{"id":5079,"date":"2016-11-22T10:00:18","date_gmt":"2016-11-22T01:00:18","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=5079"},"modified":"2016-11-21T19:01:38","modified_gmt":"2016-11-21T10:01:38","slug":"%e6%96%b0%e4%ba%ba%e6%88%90%e9%95%b7%e8%a8%98%e9%8c%b215th","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/5079","title":{"rendered":"\u65b0\u4eba\u6210\u9577\u8a18\u933215th"},"content":{"rendered":"<p>\u4eca\u56de\u306f\u3001DB(DataBase)\u306b\u3064\u3044\u3066\u66f8\u3044\u3066\u3044\u304d\u307e\u3059<\/p>\n<p>DB\u306f\u4e00\u5b9a\u306e\u5f62\u5f0f\u3067\u4f5c\u6210\u3055\u308c\u305f\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u4fdd\u6301\u3059\u308b\u6642\u306b\u4f7f\u7528\u3057\u307e\u3059<br \/>\nAndroid\u306eDB\u306b\u306fSQLite\u304c\u4f7f\u308f\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u30af\u30a8\u30ea\u3092\u66f8\u304f\u969b\u306b\u306fSQLite\u306b\u5247\u3063\u305f\u8a18\u8ff0\u3092\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059<\/p>\n<p><!--more--><\/p>\n<h3>DB\u3068Table\u306e\u4f5c\u6210<\/h3>\n<p>\u521d\u3081\u306b\u3001DB\u3092\u4f5c\u6210\u3059\u308b\u3068\u3053\u308d\u304b\u3089\u59cb\u307e\u308a\u307e\u3059<br \/>\n\u4f5c\u6210\u3059\u308b\u305f\u3081\u306b\u306f\u3001SQLiteOpenHelper\u3092\u62e1\u5f35\u3057\u305f\u72ec\u81ea\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u307e\u3059<br \/>\n\u4ee5\u4e0b\u304c\u4f5c\u6210\u3057\u305f\u3001\u72ec\u81ea\u30af\u30e9\u30b9\u306b\u306a\u308a\u307e\u3059<\/p>\n<pre style=\"border: dashed #000000 2px\"><code>public class Helper extends SQLiteOpenHelper {\r\n\r\n\tprivate static final int version = 1;\r\n\r\n\t\/\/\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\r\n\tpublic Helper(Context context) {\r\n\t\tsuper(context, \"TEST.db\", null, version);\r\n\t}\r\n\r\n\t\/\/\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\r\n\t@Override\r\n\tpublic void onCreate(SQLiteDatabase db) {\r\n\t\tdb.execSQL(\"CREATE TABLE test_t \" +\r\n\t\t\t\t\"(id INTEGER PRIMARY KEY, title VARCHAR(128));\");\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\r\n\t\t\/\/\u30d0\u30fc\u30b8\u30e7\u30f3\u5909\u66f4\u6642\u547c\u3073\u51fa\u3055\u308c\u308b\r\n\t\t\/\/ NOP\r\n\t}\r\n}\r\n<\/code><\/pre>\n<p>\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u89aa\u30af\u30e9\u30b9\u306b\u6e21\u3059\u5f15\u6570\u306f4\u3064\u3042\u308a\u307e\u3059<br \/>\n\u7b2c1\u5f15\u6570\uff1aContext\uff08\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u751f\u6210\u6642\u306b\u5f15\u6570\u306b\u3057\u305fcontext\u3092\u305d\u306e\u307e\u307e\u6d41\u7528\uff09<br \/>\n\u7b2c2\u5f15\u6570\uff1aString\uff08DB\u306e\u540d\u524d\uff09<br \/>\n\u7b2c3\u5f15\u6570\uff1aCursorFactory\uff08select\u306a\u3069\u3067Cursor\u304c\u751f\u6210\u3055\u308c\u308b\u6642\u306b\u72ec\u81ea\u30af\u30e9\u30b9\u306e\u4f7f\u7528\u304c\u3067\u304d\u308b\u3001\u57fa\u672c\u7684\u306b\u306fnull\uff09<br \/>\n\u7b2c4\u5f15\u6570\uff1aint\uff08DB\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u6307\u5b9a\uff09<\/p>\n<p>SQLiteOpenHelper\u3092\u62e1\u5f35\u3057\u305f\u3053\u3068\u3067\u5b9f\u88c5\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u30e9\u30a4\u30d5\u30b5\u30a4\u30af\u30eb\u306f2\u3064\u3042\u308a\u307e\u3059<br \/>\nonCreate\uff1a\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u751f\u6210\u6642\u306bDB\u304c\u5b58\u5728\u3057\u306a\u3051\u308c\u3070\u547c\u3070\u308c\u307e\u3059<br \/>\nonUpgrade\uff1aDB\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u5909\u66f4\u3055\u308c\u308b\u3068\u547c\u3070\u308c\u307e\u3059<\/p>\n<h3>DB\u306e\u4f7f\u7528<\/h3>\n<p>\u4f5c\u6210\u3057\u305fDB\u3092\u4f7f\u7528\u3059\u308b\u6642\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u8a18\u8ff0\u306b\u306a\u308a\u307e\u3059<\/p>\n<pre style=\"border: dashed #000000 2px\"><code>        Helper helper = new Helper(this);\r\n        \/\/\u8aad\u307f\u66f8\u304d\r\n        SQLiteDatabase wdb = helper.getWritableDatabase();\r\n        \/\/\u8aad\u307f\u8fbc\u307f\r\n        SQLiteDatabase rdb = helper.getReadableDatabase();\r\n<\/code><\/pre>\n<p>\u8aad\u307f\u66f8\u304d\u3092\u3059\u308b\u6642(DELETE\/INSERT\/UPDATE\/SELECT)\u306f\u3001getWriteableDatabase()\u30e1\u30bd\u30c3\u30c9\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059<br \/>\n\u8aad\u307f\u8fbc\u307f\u306e\u307f\u306e\u6642(SELECT)\u306f\u3001getReadableDatabase()\u30e1\u30bd\u30c3\u30c9\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059<\/p>\n<h3>Table\u306e\u64cd\u4f5c<\/h3>\n<p>\u5148\u307b\u3069\u53d6\u5f97\u3057\u305fSQLiteDatabase\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f7f\u7528\u3057\u3066\u3001Table\u304b\u3089\u60c5\u5831\u3092\u53d6\u5f97\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059<br \/>\nTable\u306b\u306f\u4f55\u3082\u5165\u3063\u3066\u3044\u306a\u3044\u306e\u3067\u3001\u307e\u305a\u306finsert\u3057\u307e\u3057\u3087\u3046<\/p>\n<pre style=\"border: dashed #000000 2px\"><code>        ContentValues values = new ContentValues();\r\n        values.put(\"id\", 1);\r\n        values.put(\"title\",\"test1\");\r\n        wdb.insert(\"test_t\", null,values);\r\n<\/code><\/pre>\n<p>insert\u306e\u5f15\u6570\u306f3\u3064\u3067\u3059<br \/>\n\u7b2c1\u5f15\u6570\uff1aString\uff08\u30c6\u30fc\u30d6\u30eb\u540d\uff09<br \/>\n\u7b2c2\u5f15\u6570\uff1aString\uff08null\u5024\u304c\u8a31\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u306b\u3001\u4ee3\u308f\u308a\u306b\u633f\u5165\u3059\u308b\u5024\uff09<br \/>\n\u7b2c3\u5f15\u6570\uff1aContentValues\uff08\u633f\u5165\u3059\u308b\u5024\u3092\u4fdd\u6301\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09<\/p>\n<p>\u6b21\u306b\u3001\u633f\u5165\u3057\u305f\u30ec\u30b3\u30fc\u30c9\u3092\u7de8\u96c6\u3057\u305f\u304f\u306a\u308a\u307e\u3059<br \/>\n\u305d\u306e\u6642\u306f\u3001Updata\u3092\u4f7f\u7528\u3057\u307e\u3059<\/p>\n<pre style=\"border: dashed #000000 2px\"><code>        values.put(\"id\",1);\r\n        values.put(\"title\",\"test2\");\r\n        int a = wdb.update(\"test_t\",values, \"id = ?\",new String[] {\"1\"});\r\n<\/code><\/pre>\n<p>updata\u306e\u5f15\u6570\u306f4\u3064\u3067\u3059<br \/>\n\u7b2c1\u5f15\u6570\uff1aString\uff08\u30c6\u30fc\u30d6\u30eb\u540d\uff09<br \/>\n\u7b2c2\u5f15\u6570\uff1aContentValues\uff08\u633f\u5165\u3059\u308b\u5024\uff09<br \/>\n\u7b2c3\u5f15\u6570\uff1aString[]\uff08\u5909\u66f4\u3092\u52a0\u3048\u308b\u30ec\u30b3\u30fc\u30c9\u306e\u6761\u4ef6\uff09<br \/>\n\u7b2c4\u5f15\u6570\uff1aString[]\uff08\u5909\u66f4\u3092\u52a0\u3048\u308b\u30ec\u30b3\u30fc\u30c9\u306e\u6761\u4ef6\u306e?\u90e8\u5206\u306e\u914d\u5217\uff09<br \/>\ndelete\u3082updata\u3068\u4f3c\u305f\u3088\u3046\u306a\u3082\u306e\u3067\u3001\u7b2c2\u5f15\u6570\u306e\u633f\u5165\u3059\u308b\u5024\u304c\u306a\u304f3\u3064\u306e\u5f15\u6570\u304b\u3089\u306a\u308a\u307e\u3059<\/p>\n<p>\u6700\u5f8c\u306b\u30ec\u30b3\u30fc\u30c9\u306e\u62bd\u51fa\u3092query\u3067\u884c\u3044\u307e\u3059<\/p>\n<pre style=\"border: dashed #000000 2px\"><code>        Cursor cursor = wdb.query(\"test_t\", new String[]{\"id\", \"title\"}, null,null,null,null,null);\r\n        cursor.moveToFirst();\r\n        cursor.getString(1);\r\n        cursor.close();\r\n<\/code><\/pre>\n<p>query\u306e\u5f15\u6570\u306f7\u3064\u3067\u3059\u304c\u30018\u3064\u306e\u3082\u306e\u3082\u3042\u308a\u307e\u3059<br \/>\n\u7b2c1\u5f15\u6570\uff1aString\uff08\u30c6\u30fc\u30d6\u30eb\u540d\uff09<br \/>\n\u7b2c2\u5f15\u6570\uff1aString[]\uff08\u53d6\u5f97\u3059\u308b\u30ab\u30e9\u30e0\u540d\uff09<br \/>\n\u7b2c3\u5f15\u6570\uff1aString\uff08\u53d6\u5f97\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u306e\u6761\u4ef6\uff09<br \/>\n\u7b2c4\u5f15\u6570\uff1aString[]\uff08\u53d6\u5f97\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u306e\u6761\u4ef6\u306e?\u90e8\u5206\u306e\u914d\u5217\uff09<br \/>\n\u7b2c5\u5f15\u6570\uff1aString\uff08\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b\u30ab\u30e9\u30e0\u540d\uff09<br \/>\n\u7b2c6\u5f15\u6570\uff1aString\uff08\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u305f\u5f8c\u306e\u6761\u4ef6\uff09<br \/>\n\u7b2c7\u5f15\u6570\uff1aString\uff08\u4e26\u3073\u66ff\u3048\u308b\u30ab\u30e9\u30e0\u540d\uff09<br \/>\n\u7b2c8\u5f15\u6570\uff1aString\uff08\u53d6\u5f97\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u306e\u4e0a\u9650\uff09\u203b\u306a\u304f\u3066\u3082\u53ef<\/p>\n<p>query\u306e\u5834\u5408\u306f\u3001Cursor\u304c\u53d6\u5f97\u3067\u304d\u3001\u30ec\u30b3\u30fc\u30c9\u60c5\u5831\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059<br \/>\n\u5bfe\u8c61\u30ec\u30b3\u30fc\u30c9\u304c0\u4ef6\u306e\u6642\u306f\u3001null\u5024\u3068\u306a\u308a\u307e\u3059<br \/>\nCursor\u30af\u30e9\u30b9\u306eget(\u30ab\u30e9\u30e0\u756a\u53f7)\u3067\u30ec\u30b3\u30fc\u30c9\u304b\u3089\u60c5\u5831\u304c\u53d6\u5f97\u3067\u304d\u307e\u3059<br \/>\n\u30ec\u30b3\u30fc\u30c9\u304c\u8907\u6570\u4ef6\u3042\u308b\u6642\u306f\u3001moveToNext()\u3067\u6b21\u306e\u30ec\u30b3\u30fc\u30c9\u3078\u79fb\u52d5\u3057\u307e\u3059<br \/>\n\u4e0d\u8981\u306b\u306a\u3063\u305fCursor\u306fclose()\u3067\u9589\u3058\u307e\u3059<\/p>\n<p>\u6700\u5f8c\u306b\u3001SQLiteDatabase\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306eclose()\u3082\u5fd8\u308c\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046<\/p>\n<h3>\u7d42\u308f\u308a\u306b<\/h3>\n<p>DB\u306f\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u51fa\u6765\u3066\u4fbf\u5229\u3067\u3001\u898f\u6a21\u306e\u5927\u304d\u3044\u30b7\u30b9\u30c6\u30e0\u306a\u3069\u306f\u591a\u304f\u306e\u30e6\u30fc\u30b6\u304c\u53c2\u7167\u3084\u5909\u66f4\u3092\u884c\u3044\u307e\u3059<br \/>\nDB\u306b\u591a\u304f\u306e\u30e6\u30fc\u30b6\u30fc\u304c\u540c\u6642\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u5834\u5408\u306b\u306f\u3001\u5909\u66f4\u4e2d\u306e\u30c7\u30fc\u30bf\u306b\u306f\u89e6\u3063\u3066\u6b32\u3057\u304f\u306a\u3044\u3082\u306e\u3067\u3059<br \/>\n\u5909\u66f4\u4e2d\u306e\u30c7\u30fc\u30bf\u306b\u5909\u66f4\u3092\u52a0\u3048\u3089\u308c\u308b\u3068\u30c7\u30fc\u30bf\u304c\u58ca\u308c\u3066\u3044\u307e\u3059\u53ef\u80fd\u6027\u304c\u3042\u308b\u304b\u3089\u3067\u3059<br \/>\n\u305d\u3046\u3044\u3063\u305f\u6642\u306b\u3001\u6392\u4ed6\u7684\u306a\u51e6\u7406\u3092\u3057\u3066\u304f\u308c\u308b\u3082\u306e\u306b\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u3042\u308a\u307e\u3059<br \/>\n\u6b21\u56de\u306f\u3001\u3053\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u51e6\u7406\u3092\u66f8\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059<\/p>\n<p>\u706b\u66dc\u65e5\u62c5\u5f53\uff1apoppy<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/5079\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/5079\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"\u65b0\u4eba\u6210\u9577\u8a18\u933215th\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/5079\" data-text=\"\u65b0\u4eba\u6210\u9577\u8a18\u933215th\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/5079\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/5079\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306f\u3001DB(DataBase)\u306b\u3064\u3044\u3066\u66f8\u3044\u3066\u3044\u304d\u307e\u3059 DB\u306f\u4e00\u5b9a\u306e\u5f62\u5f0f\u3067\u4f5c\u6210\u3055\u308c\u305f\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u4fdd\u6301\u3059\u308b\u6642\u306b\u4f7f\u7528\u3057\u307e\u3059 Android\u306eDB\u306b\u306fSQLite\u304c\u4f7f\u308f\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u30af\u30a8\u30ea\u3092\u66f8\u304f\u969b\u306b\u306fSQLite\u306b\u5247\u3063\u305f\u8a18\u8ff0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100,12],"tags":[56,79,101],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/5079"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=5079"}],"version-history":[{"count":18,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/5079\/revisions"}],"predecessor-version":[{"id":5107,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/5079\/revisions\/5107"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=5079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=5079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=5079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}