{"id":309,"date":"2018-12-24T20:01:23","date_gmt":"2018-12-24T11:01:23","guid":{"rendered":"https:\/\/dalomo.net\/blog\/?p=309"},"modified":"2018-12-24T20:05:45","modified_gmt":"2018-12-24T11:05:45","slug":"androidstudio%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%81%bf%e3%81%9f%e3%81%84bmi%e8%a8%88%e7%ae%97%e2%91%a1","status":"publish","type":"post","link":"https:\/\/dalomo.net\/blog\/2018\/12\/24\/309\/","title":{"rendered":"AndroidStudio\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u4f7f\u3063\u3066\u307f\u305f\u3044&#038;BMI\u8a08\u7b97\u2461"},"content":{"rendered":"<h2>\u3084\u3063\u3068SQLite<\/h2>\n<p>BMI\u306e\u8a08\u7b97\u304c\u3067\u304d\u305f\u306e\u3067\u3001\u8a08\u7b97\u7d50\u679c\u3092DB\u306b\u4fdd\u5b58\u3057\u3088\u30fc\u3002\u3067\u3001\u4fdd\u5b58\u3057\u305f\u306e\u3092\u53d6\u308a\u51fa\u305d\u30fc\u3002<\/p>\n<h3>DB\u30d8\u30eb\u30d1\u30fc\u30af\u30e9\u30b9<\/h3>\n<p>\u306a\u308b\u3082\u306e\u304c\u5fc5\u8981\u3089\u3057\u3044\u3002\u30b3\u30d4\u30da\u3057\u3066\u3053\u3046\u3002<\/p>\n<pre>public class DatabaseHelper extends SQLiteOpenHelper {\r\n    private static final String DATABASE_NAME = \"bmihistory.db\";\r\n    private static final int DATABASE_VERSION = 1;\r\n\r\n    public DatabaseHelper(Context context) {\r\n        super(context, DATABASE_NAME, null, DATABASE_VERSION);\r\n    }\r\n\r\n    @Override\r\n    public void onCreate(SQLiteDatabase db) {\r\n        StringBuilder sb = new StringBuilder();\r\n        sb.append(\"CREATE TABLE bmihistory (\");\r\n        sb.append(\"_id INTEGER PRIMARY KEY,\");\r\n        sb.append(\"date TEXT,\");\r\n        sb.append(\"height REAL,\");\r\n        sb.append(\"weight REAL,\");\r\n        sb.append(\"result REAL\");\r\n        sb.append(\");\");\r\n        String sql = sb.toString();\r\n\r\n        db.execSQL(sql);\r\n\r\n    }\r\n\r\n    @Override\r\n    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\r\n\r\n    }\r\n}<\/pre>\n<p>SQL\u6587\u306e\u300c,\u300d\u304c\u629c\u3051\u3066\u3066\u6570\u6642\u9593\u30cf\u30de\u3063\u305f\u3002\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306e\u5f15\u6570\u306fcontext\u3060\u3051\u3067\u3088\u304b\u3063\u305f\u307f\u305f\u3044\u3002IDE\u3055\u3093\u306b\u4f5c\u3063\u3066\u3082\u3089\u3063\u3066\u305f\u3089\u3001\u306a\u3093\u304b\u8272\u3093\u306a\u306e\u304c\u3064\u3044\u3066\u304d\u3066\u3066\u3001\u305d\u308c\u306f\u6d88\u3057\u305f\u3002onCreate\u306fDB\u304c\u306a\u3044\u6642\u4e00\u56de\u3060\u3051\u547c\u3070\u308c\u308b\u307f\u305f\u3044\u3002<\/p>\n<pre>DatabaseHelper helper = new DatabaseHelper(BMIActivity.this);\r\n        SQLiteDatabase db = helper.getWritableDatabase();\r\n\r\n        try {\r\n            String sqlInsert = \"INSERT INTO bmihistory (_id, date, height, weight, result) VALUES (?, ?, ?, ?, ?)\";\r\n            SQLiteStatement statement = db.compileStatement(sqlInsert);\r\n\/\/            statement.bindLong(1, 1);\r\n            statement.bindString(2, resTime);\r\n            statement.bindDouble(3, dHeight);\r\n            statement.bindDouble(4, valueWeight.doubleValue());\r\n            statement.bindDouble(5, Double.valueOf(result));\r\n\r\n            statement.executeInsert();\r\n        } finally {\r\n            db.close();\r\n        }<\/pre>\n<p>\u3053\u308c\u3067\u30c7\u30fc\u30bf\u4fdd\u5b58\u304c\u3067\u304d\u3066\u308b\u306f\u305a\u2026\u3002\u4eca\u306ftry-finally\u3058\u3083\u306a\u304f\u3066<\/p>\n<pre>        try (SQLiteDatabase db = helper.getWritableDatabase()){\r\n            String sqlInsert = \"INSERT INTO bmihistory (_id, date, height, weight, result) VALUES (?, ?, ?, ?, ?)\";\r\n            SQLiteStatement statement = db.compileStatement(sqlInsert);\r\n\/\/            statement.bindLong(1, 1);\r\n            statement.bindString(2, resTime);\r\n            statement.bindDouble(3, dHeight);\r\n            statement.bindDouble(4, valueWeight.doubleValue());\r\n            statement.bindDouble(5, Double.valueOf(result));\r\n\r\n            statement.executeInsert();\r\n        }<\/pre>\n<p>\u3053\u3046\u3067\u3044\u3044\u307f\u305f\u3044\u3002\u610f\u5473\u306f\u3088\u304f\u5206\u304b\u3063\u3066\u306a\u3044\u3002<\/p>\n<p>\u3067\u3001\u3053\u3053\u3067\u78ba\u8a8d\u306e\u305f\u3081\u306bSQLAndroid\u3092\u4f7f\u304a\u3046\u3068\u3057\u305f\u3093\u3060\u3051\u3069<\/p>\n<blockquote><p>run-as: package has corrupt installation: net.dalomo.lifecalculator<\/p><\/blockquote>\n<p>\u3068\u51fa\u3066\u3057\u307e\u3063\u3066\u4f55\u3082\u8868\u793a\u3055\u308c\u306a\u3044\u3002\u306a\u305c\u3060\u3002\u8ae6\u3081\u3066\u4ed6\u306e\u30b5\u30a4\u30c8\u3067\u898b\u305f\u3001Terminal\u304b\u3089adb shell\u2192run-as\u3082\u3084\u3063\u3066\u307f\u305f\u3051\u3069\u30c0\u30e1\u3002\u306a\u3093\u304broot\u6a29\u9650\u304c\u5fc5\u8981\u3089\u3057\u3044\u3002\u3057\u3087\u3046\u304c\u306a\u3044\u306e\u3067\u666e\u901a\u306b\u30b3\u30fc\u30c9\u66f8\u3044\u3066\u53d6\u5f97\u3057\u3066\u307f\u308b\u3002\u8da3\u65e8\u304c\u305a\u308c\u3066\u304d\u3066\u308b\u306a\u3002<\/p>\n<h3>DB\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97<\/h3>\n<pre>DatabaseHelper helper = new DatabaseHelper(BMIActivity.this);\r\ntry (SQLiteDatabase db = helper.getWritableDatabase()) {\r\n    String sql = \"SELECT * FROM bmihistory\";\r\n    Cursor cursor = db.rawQuery(sql, null);\r\n    StringBuilder sb = new StringBuilder();\r\n\r\n    while (cursor.moveToNext()) {\r\n        int idxDate = cursor.getColumnIndex(\"date\");\r\n        int idxheight = cursor.getColumnIndex(\"height\");\r\n        int idxweight = cursor.getColumnIndex(\"weight\");\r\n        int idxresult = cursor.getColumnIndex(\"result\");\r\n        sb.append(cursor.getString(idxDate));\r\n        sb.append(\", \");\r\n        sb.append(cursor.getString(idxheight));\r\n        sb.append(\", \");\r\n        sb.append(cursor.getString(idxweight));\r\n        sb.append(\", \");\r\n        sb.append(cursor.getString(idxresult));\r\n        sb.append(\"\\n\");\r\n    }\r\n\r\n    TextView tv = findViewById(R.id.textView2);\r\n    tv.setText(sb.toString());\r\n\r\n    cursor.close();\r\n}<\/pre>\n<p><a href=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2018\/12\/Screenshot_20181224-194435.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-311\" src=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2018\/12\/Screenshot_20181224-194435-145x300.jpg\" alt=\"\" width=\"145\" height=\"300\" srcset=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2018\/12\/Screenshot_20181224-194435-145x300.jpg 145w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2018\/12\/Screenshot_20181224-194435-768x1593.jpg 768w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2018\/12\/Screenshot_20181224-194435-494x1024.jpg 494w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2018\/12\/Screenshot_20181224-194435.jpg 1080w\" sizes=\"auto, (max-width: 145px) 100vw, 145px\" \/><\/a><\/p>\n<p>\u3067\u304d\u305f\u30fc\u3002\u3067\u304d\u305f\u3093\u3060\u3051\u3069\u3001\u306a\u3093\u3060\u304b\u306a\u3041\u3002DB\u3092DB\u306e\u307e\u307e\u7de8\u96c6\u3057\u3066\u307f\u305f\u304f\u3066\u59cb\u3081\u305f\u3093\u3060\u3051\u3069\u3001\u6700\u7d42\u7684\u306bBottun\u62bc\u3057\u3066TextView\u306b\u8868\u793a\u3059\u308b\u3044\u3064\u3082\u306e\u3084\u3064\u306b\u306a\u3063\u305f\u3002\u307e\u3041\u3001\u53d6\u5f97\u306e\u4ed5\u65b9\u304c\u3055\u308f\u308a\u3060\u3051\u3067\u3082\u5206\u304b\u3063\u305f\u304b\u3089\u3044\u3063\u304b\u30fc\u3002<\/p>\n<p>\u305f\u3060\u3001\u3053\u306e\u5834\u5408\u3060\u3068\u3001\u4e00\u3064\u306e\u8a08\u7b97\u306b\u3064\u304d\u4e00\u3064\u306eDB\u3063\u3066\u611f\u3058\u306b\u306a\u308b\u306e?\u3042\u3001\u3044\u3084\u3001TABLE\u3092\u5897\u3084\u305b\u3070\u3044\u3044\u306e\u304b\u306a?\u3042\u308c\u3001\u3067\u3082\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306f\u4e00\u56de\u3057\u304b\u547c\u3070\u308c\u306a\u3044\u3057\u306a\u2026\u3002\u3061\u3087\u3063\u3068\u3053\u3093\u304c\u3089\u304c\u3063\u3066\u304d\u305f\u304b\u3089\u307e\u305f\u3042\u3068\u3067\u8003\u3048\u3088\u3046\u3002\u3066\u3044\u3046\u304b\u3053\u3093\u306a\u3093\u3060\u304b\u3089GUI\u3067\u78ba\u8a8d\u3057\u305f\u3044\u3093\u3060\u3051\u3069\u306a\u3002CUI\u308f\u304b\u3093\u306a\u3044\u3088\u3002<\/p>\n<h3>\u53c2\u8003<\/h3>\n<p>[affi id=2]<\/p>\n<p><a href=\"https:\/\/www.dbonline.jp\/sqlite\/type\/index1.html\">https:\/\/www.dbonline.jp\/sqlite\/type\/index1.html<\/a><\/p>\n<p><a href=\"https:\/\/qiita.com\/zuccyi\/items\/d9c185588a5628837137\">https:\/\/qiita.com\/zuccyi\/items\/d9c185588a5628837137<\/a><\/p>\n<p><a href=\"https:\/\/www.dbonline.jp\/sqlite\/insert\/index1.html\">https:\/\/www.dbonline.jp\/sqlite\/insert\/index1.html<\/a><\/p>\n<p><a href=\"https:\/\/wa3.i-3-i.info\/word12448.html\">https:\/\/wa3.i-3-i.info\/word12448.html<\/a><\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/25179590\/android-sqlite-db-pull-error\">https:\/\/stackoverflow.com\/questions\/25179590\/android-sqlite-db-pull-error<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3084\u3063\u3068SQLite BMI\u306e\u8a08\u7b97\u304c\u3067\u304d\u305f\u306e\u3067\u3001\u8a08\u7b97\u7d50\u679c\u3092DB\u306b\u4fdd\u5b58\u3057\u3088\u30fc\u3002\u3067\u3001\u4fdd\u5b58\u3057\u305f\u306e\u3092\u53d6\u308a\u51fa\u305d\u30fc\u3002 DB\u30d8\u30eb\u30d1\u30fc\u30af\u30e9\u30b9 \u306a\u308b\u3082\u306e\u304c\u5fc5\u8981\u3089\u3057\u3044\u3002\u30b3\u30d4\u30da\u3057\u3066\u3053\u3046\u3002 public class DatabaseHelper  &hellip; <a href=\"https:\/\/dalomo.net\/blog\/2018\/12\/24\/309\/\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":311,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[3,6,27,4,26],"class_list":["post-309","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-8","tag-android","tag-androidstudio","tag-sqlite","tag-4","tag-26"],"_links":{"self":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/comments?post=309"}],"version-history":[{"count":4,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"predecessor-version":[{"id":314,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions\/314"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/media\/311"}],"wp:attachment":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/tags?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}