teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

ソースを修正

2018/09/23 05:03

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -69,7 +69,7 @@
69
69
  }
70
70
 
71
71
  }
72
-
72
+ ```
73
73
  ### 試したこと
74
74
 
75
75
  MainActivityでは

4

必要でない部分を削除

2018/09/23 05:03

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -69,168 +69,7 @@
69
69
  }
70
70
 
71
71
  }
72
- ```
73
- ```ここに言語を入力
74
- package com.example.lavi.mojitest;
75
72
 
76
- import android.content.Context;
77
- import android.content.res.AssetManager;
78
- import android.database.SQLException;
79
- import android.database.sqlite.SQLiteDatabase;
80
- import android.database.sqlite.SQLiteOpenHelper;
81
- import android.database.sqlite.SQLiteStatement;
82
- import android.util.Log;
83
-
84
- import java.io.BufferedReader;
85
- import java.io.IOException;
86
- import java.io.InputStream;
87
- import java.io.InputStreamReader;
88
- import java.util.ArrayList;
89
- import java.util.Iterator;
90
- import java.util.List;
91
-
92
- public class WordsDatabaseHelper extends SQLiteOpenHelper {
93
- static final private String DB_NAME = "words.sqlite";
94
- static final private int VERSION = 1;
95
- private String[] fileName={"category","data","large","middle","small","lang"};
96
- private Context context;
97
- private String[] data;
98
- private int[][] intData;
99
- int intCol;
100
-
101
-
102
- public WordsDatabaseHelper(Context context) {
103
- super(context, DB_NAME, null, VERSION);
104
- this.context = context;
105
- }
106
-
107
- @Override
108
- public void onOpen(SQLiteDatabase db) {
109
- super.onOpen(db);
110
- }
111
-
112
- private void buildTable(SQLiteDatabase db){
113
- String[] createSQL={
114
- "CREATE TABLE [category] (" +
115
- "[category_id] INTEGER NOT NULL UNIQUE," +
116
- "[large_id] INTEGER DEFAULT '0'," +
117
- "[middle_id] INTEGER DEFAULT '0'," +
118
- "[small_id] INTEGER DEFAULT '0'," +
119
- "[lang_id] INTEGER DEFAULT '0'," +
120
- "[sample] VARCHAR(255),\n" +
121
- "PRIMARY KEY([category_id])" +
122
- ");"
123
-
124
- ,"CREATE TABLE [data](" +
125
- "[category_id] INTEGER, " +
126
- "[number] INTEGER, " +
127
- "[data] VARCHAR(255), " +
128
- "PRIMARY KEY([category_id],[number])" +
129
- ");"
130
-
131
- ,"CREATE TABLE [large](" +
132
- "[large_id] INTEGER PRIMARY KEY, " +
133
- "[large] VARCHAR(255));"
134
-
135
- ,"CREATE TABLE [middle](" +
136
- "[middle_id] INTEGER PRIMARY KEY, " +
137
- "[large_id] INTEGER, " +
138
- "[middle_num] INTEGER, " +
139
- "[middle] VARCHAR(255));"
140
-
141
- ,"CREATE TABLE [small](" +
142
- "[small_id] INTEGER PRIMARY KEY, " +
143
- "[small] VARCHAR(255));"
144
-
145
- ,"CREATE TABLE [lang](" +
146
- "[lang_id] INTEGER PRIMARY KEY," +
147
- "[lang] VARCHAR(255));"
148
- };
149
- String[] insertSQL={
150
- " (category_id,large_id,middle_id,small_id,lang_id,sample) VALUES(?,?,?,?,?,?)",
151
- " (category_id,number,data) VALUES(?,?,?)",
152
- " (large_id,large) VALUES(?,?)",
153
- " (middle_id,large_id,middle_num,middle) VALUES(?,?,?,?)",
154
- " (small_id,small) VALUES(?,?)",
155
- " (lang_id,lang) VALUES(?,?)"};
156
-
157
- for (int i = 0; i < fileName.length; i++) {
158
- List<String> strList=readFile(fileName[i]);
159
-
160
- db.execSQL(createSQL[i]);
161
- db.beginTransaction();
162
- try {
163
- for (int j = 1; j <strList.size()-1; j++) {
164
- String[] strValue=(strList.get(j).split(","));
165
-
166
- SQLiteStatement sql = db.compileStatement(
167
- "INSERT INTO "+fileName[i]+insertSQL[i]);
168
- for (int k = 0; k <strValue.length ; k++) {
169
- strValue[k]=strValue[k].replaceAll("\"","");//余計な"を消す
170
- if(isNum(strValue[k])) {
171
- sql.bindLong(k+1, Integer.valueOf(strValue[k]));
172
- }else {
173
- sql.bindString(k+1, strValue[k]);
174
- }
175
- }
176
- sql.executeInsert();
177
-
178
- }
179
- db.setTransactionSuccessful();
180
- } catch (SQLException e) {
181
- e.printStackTrace();
182
- } finally {
183
- db.endTransaction();
184
- }
185
- }
186
- }
187
-
188
- private boolean isNum(String number) {
189
- try {
190
- Integer.parseInt(number);
191
- return true;
192
- } catch (NumberFormatException e) {
193
- return false;
194
- }
195
- }
196
-
197
- private List<String> readFile(String filename){
198
- //csvファイル読み込み
199
- AssetManager assetManager = context.getResources().getAssets();
200
- List<String> list = null;
201
-
202
- try {
203
- InputStream inputStream = assetManager.open(filename+".csv");
204
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
205
- BufferedReader br = new BufferedReader(inputStreamReader);
206
- String line;
207
- list = new ArrayList<String>();
208
- line = br.readLine();
209
- while ((line = br.readLine()) != null) {
210
- list.add(line);
211
- }
212
- br.close();
213
- } catch (IOException e) {
214
- e.printStackTrace();
215
- }
216
- return list;
217
- }
218
-
219
- @Override
220
- public void onCreate(SQLiteDatabase db) {
221
- buildTable(db);
222
- }
223
-
224
- @Override
225
- public void onUpgrade(SQLiteDatabase db, int old_v, int new_v) {
226
- db.execSQL("DROP TABLE IF EXISTS words");
227
- onCreate(db);
228
- }
229
- }
230
-
231
- ```
232
-
233
-
234
73
  ### 試したこと
235
74
 
236
75
  MainActivityでは
@@ -239,7 +78,6 @@
239
78
  としてもエラーは出なかったです。
240
79
 
241
80
  ### 補足情報(FW/ツールのバージョンなど)
242
- windows10 64bit
243
81
  android studio 3.1.4
244
82
  classpath 'com.android.tools.build:gradle:3.1.4'
245
83
  testImplementation 'junit:junit:4.12'

3

開発環境追記

2018/09/23 05:02

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -168,7 +168,6 @@
168
168
  for (int k = 0; k <strValue.length ; k++) {
169
169
  strValue[k]=strValue[k].replaceAll("\"","");//余計な"を消す
170
170
  if(isNum(strValue[k])) {
171
- Log.d("", "にゃ:"+i+"j="+k+"val="+strValue[k]);
172
171
  sql.bindLong(k+1, Integer.valueOf(strValue[k]));
173
172
  }else {
174
173
  sql.bindString(k+1, strValue[k]);
@@ -240,6 +239,13 @@
240
239
  としてもエラーは出なかったです。
241
240
 
242
241
  ### 補足情報(FW/ツールのバージョンなど)
242
+ windows10 64bit
243
+ android studio 3.1.4
244
+ classpath 'com.android.tools.build:gradle:3.1.4'
245
+ testImplementation 'junit:junit:4.12'
246
+ testImplementation 'org.hamcrest:hamcrest-library:1.3'
247
+ androidTestImplementation 'com.android.support.test:runner:1.0.2'
248
+ androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
243
- compileSdkVersion 27
249
+ compileSdkVersion 27
244
250
  minSdkVersion 16
245
251
  targetSdkVersion 27

2

継承したクラスのソースを追加しました。

2018/09/22 08:16

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -70,7 +70,168 @@
70
70
 
71
71
  }
72
72
  ```
73
+ ```ここに言語を入力
74
+ package com.example.lavi.mojitest;
73
75
 
76
+ import android.content.Context;
77
+ import android.content.res.AssetManager;
78
+ import android.database.SQLException;
79
+ import android.database.sqlite.SQLiteDatabase;
80
+ import android.database.sqlite.SQLiteOpenHelper;
81
+ import android.database.sqlite.SQLiteStatement;
82
+ import android.util.Log;
83
+
84
+ import java.io.BufferedReader;
85
+ import java.io.IOException;
86
+ import java.io.InputStream;
87
+ import java.io.InputStreamReader;
88
+ import java.util.ArrayList;
89
+ import java.util.Iterator;
90
+ import java.util.List;
91
+
92
+ public class WordsDatabaseHelper extends SQLiteOpenHelper {
93
+ static final private String DB_NAME = "words.sqlite";
94
+ static final private int VERSION = 1;
95
+ private String[] fileName={"category","data","large","middle","small","lang"};
96
+ private Context context;
97
+ private String[] data;
98
+ private int[][] intData;
99
+ int intCol;
100
+
101
+
102
+ public WordsDatabaseHelper(Context context) {
103
+ super(context, DB_NAME, null, VERSION);
104
+ this.context = context;
105
+ }
106
+
107
+ @Override
108
+ public void onOpen(SQLiteDatabase db) {
109
+ super.onOpen(db);
110
+ }
111
+
112
+ private void buildTable(SQLiteDatabase db){
113
+ String[] createSQL={
114
+ "CREATE TABLE [category] (" +
115
+ "[category_id] INTEGER NOT NULL UNIQUE," +
116
+ "[large_id] INTEGER DEFAULT '0'," +
117
+ "[middle_id] INTEGER DEFAULT '0'," +
118
+ "[small_id] INTEGER DEFAULT '0'," +
119
+ "[lang_id] INTEGER DEFAULT '0'," +
120
+ "[sample] VARCHAR(255),\n" +
121
+ "PRIMARY KEY([category_id])" +
122
+ ");"
123
+
124
+ ,"CREATE TABLE [data](" +
125
+ "[category_id] INTEGER, " +
126
+ "[number] INTEGER, " +
127
+ "[data] VARCHAR(255), " +
128
+ "PRIMARY KEY([category_id],[number])" +
129
+ ");"
130
+
131
+ ,"CREATE TABLE [large](" +
132
+ "[large_id] INTEGER PRIMARY KEY, " +
133
+ "[large] VARCHAR(255));"
134
+
135
+ ,"CREATE TABLE [middle](" +
136
+ "[middle_id] INTEGER PRIMARY KEY, " +
137
+ "[large_id] INTEGER, " +
138
+ "[middle_num] INTEGER, " +
139
+ "[middle] VARCHAR(255));"
140
+
141
+ ,"CREATE TABLE [small](" +
142
+ "[small_id] INTEGER PRIMARY KEY, " +
143
+ "[small] VARCHAR(255));"
144
+
145
+ ,"CREATE TABLE [lang](" +
146
+ "[lang_id] INTEGER PRIMARY KEY," +
147
+ "[lang] VARCHAR(255));"
148
+ };
149
+ String[] insertSQL={
150
+ " (category_id,large_id,middle_id,small_id,lang_id,sample) VALUES(?,?,?,?,?,?)",
151
+ " (category_id,number,data) VALUES(?,?,?)",
152
+ " (large_id,large) VALUES(?,?)",
153
+ " (middle_id,large_id,middle_num,middle) VALUES(?,?,?,?)",
154
+ " (small_id,small) VALUES(?,?)",
155
+ " (lang_id,lang) VALUES(?,?)"};
156
+
157
+ for (int i = 0; i < fileName.length; i++) {
158
+ List<String> strList=readFile(fileName[i]);
159
+
160
+ db.execSQL(createSQL[i]);
161
+ db.beginTransaction();
162
+ try {
163
+ for (int j = 1; j <strList.size()-1; j++) {
164
+ String[] strValue=(strList.get(j).split(","));
165
+
166
+ SQLiteStatement sql = db.compileStatement(
167
+ "INSERT INTO "+fileName[i]+insertSQL[i]);
168
+ for (int k = 0; k <strValue.length ; k++) {
169
+ strValue[k]=strValue[k].replaceAll("\"","");//余計な"を消す
170
+ if(isNum(strValue[k])) {
171
+ Log.d("", "にゃ:"+i+"j="+k+"val="+strValue[k]);
172
+ sql.bindLong(k+1, Integer.valueOf(strValue[k]));
173
+ }else {
174
+ sql.bindString(k+1, strValue[k]);
175
+ }
176
+ }
177
+ sql.executeInsert();
178
+
179
+ }
180
+ db.setTransactionSuccessful();
181
+ } catch (SQLException e) {
182
+ e.printStackTrace();
183
+ } finally {
184
+ db.endTransaction();
185
+ }
186
+ }
187
+ }
188
+
189
+ private boolean isNum(String number) {
190
+ try {
191
+ Integer.parseInt(number);
192
+ return true;
193
+ } catch (NumberFormatException e) {
194
+ return false;
195
+ }
196
+ }
197
+
198
+ private List<String> readFile(String filename){
199
+ //csvファイル読み込み
200
+ AssetManager assetManager = context.getResources().getAssets();
201
+ List<String> list = null;
202
+
203
+ try {
204
+ InputStream inputStream = assetManager.open(filename+".csv");
205
+ InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
206
+ BufferedReader br = new BufferedReader(inputStreamReader);
207
+ String line;
208
+ list = new ArrayList<String>();
209
+ line = br.readLine();
210
+ while ((line = br.readLine()) != null) {
211
+ list.add(line);
212
+ }
213
+ br.close();
214
+ } catch (IOException e) {
215
+ e.printStackTrace();
216
+ }
217
+ return list;
218
+ }
219
+
220
+ @Override
221
+ public void onCreate(SQLiteDatabase db) {
222
+ buildTable(db);
223
+ }
224
+
225
+ @Override
226
+ public void onUpgrade(SQLiteDatabase db, int old_v, int new_v) {
227
+ db.execSQL("DROP TABLE IF EXISTS words");
228
+ onCreate(db);
229
+ }
230
+ }
231
+
232
+ ```
233
+
234
+
74
235
  ### 試したこと
75
236
 
76
237
  MainActivityでは

1

2018/09/22 07:28

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
File without changes