質問編集履歴

1

コードの追加

2017/04/07 02:13

投稿

edoooooo
edoooooo

score476

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,137 @@
4
4
 
5
5
  どうぞよろしくお願いします。
6
6
 
7
-
7
+ MemoDBHelper
8
+
9
+ ```java
10
+
11
+ package com.example.android.sample.memo;
12
+
13
+
14
+
15
+ import android.content.Context;
16
+
17
+ import android.database.sqlite.SQLiteDatabase;
18
+
19
+ import android.database.sqlite.SQLiteOpenHelper;
20
+
21
+
22
+
23
+ /**
24
+
25
+ * Created by endoutaichi on 2017/04/06.
26
+
27
+ */
28
+
29
+
30
+
31
+ public class MemoDBHelper extends SQLiteOpenHelper {
32
+
33
+
34
+
35
+ public static final String DB_NAME="myapp.db";
36
+
37
+ public static final int DB_VERSION=1;
38
+
39
+ public static final String CREATE_TABLE=
40
+
41
+ "create table " + UserContract.Users.TABLE_NAME + "(" +
42
+
43
+ UserContract.Users._ID+ " integer primary key autoincrement, " +
44
+
45
+ UserContract.Users.COL_TITLE + " title," +
46
+
47
+ UserContract.Users.COL_CONTENT + " content, +" +
48
+
49
+ UserContract.Users.COL_DATA + "data" +
50
+
51
+ ")";
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+ /**"create table"+ UserContract.Users.TABLE_NAME+"("+
60
+
61
+ UserContract.Users._ID+"integer primary key autoincrement,"+
62
+
63
+ UserContract.Users.COL_NAME+"text,"+
64
+
65
+ UserContract.Users.COL_SCORE+"integer)";
66
+
67
+ **/
68
+
69
+ public static final String INIT_TABLE=
70
+
71
+ "insert into users (title,content,data)values" +
72
+
73
+ "('仕事','出張','5月3日'),"+
74
+
75
+ "('遊び','海','8月4日'),"+
76
+
77
+ "('買い物','ららぽーと','3月3日')";
78
+
79
+
80
+
81
+ public static final String DROP_TABLE=
82
+
83
+ "drop table if exists users";
84
+
85
+
86
+
87
+ public MemoDBHelper(Context c){
88
+
89
+ super(c,DB_NAME,null,DB_VERSION);
90
+
91
+ }
92
+
93
+
94
+
95
+
96
+
97
+ @Override
98
+
99
+ public void onCreate(SQLiteDatabase sqLiteDatabase) {
100
+
101
+
102
+
103
+ //create table
104
+
105
+ sqLiteDatabase.execSQL(CREATE_TABLE);
106
+
107
+ //init table
108
+
109
+ sqLiteDatabase.execSQL(INIT_TABLE);
110
+
111
+
112
+
113
+ }
114
+
115
+
116
+
117
+ @Override
118
+
119
+ public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
120
+
121
+
122
+
123
+ //drop table
124
+
125
+ sqLiteDatabase.execSQL(DROP_TABLE);
126
+
127
+ //onCreate
128
+
129
+ onCreate(sqLiteDatabase);
130
+
131
+ }
132
+
133
+ }
134
+
135
+
136
+
137
+ ```
8
138
 
9
139
  MainActivity.java
10
140
 
@@ -288,62 +418,4 @@
288
418
 
289
419
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
290
420
 
291
- at android.os.Handler.dispatchMessage(Handler.java:99)
292
-
293
- at android.os.Looper.loop(Looper.java:213)
294
-
295
- at android.app.ActivityThread.main(ActivityThread.java:4786)
296
-
297
- at java.lang.reflect.Method.invokeNative(Native Method)
298
-
299
- at java.lang.reflect.Method.invoke(Method.java:511)
300
-
301
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
302
-
303
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
304
-
305
- at dalvik.system.NativeStart.main(Native Method)
306
-
307
- Caused by: java.lang.NullPointerException
308
-
309
- at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
310
-
311
- at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
312
-
313
- at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
314
-
315
- at com.example.android.sample.memo.MainActivity.<init>(MainActivity.java:51)
316
-
317
- at java.lang.Class.newInstanceImpl(Native Method)
318
-
319
- at java.lang.Class.newInstance(Class.java:1319)
320
-
321
- at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
322
-
323
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1985)
324
-
325
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095) 
326
-
327
- at android.app.ActivityThread.access$600(ActivityThread.java:137) 
328
-
329
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206) 
330
-
331
- at android.os.Handler.dispatchMessage(Handler.java:99) 
332
-
333
- at android.os.Looper.loop(Looper.java:213) 
334
-
335
- at android.app.ActivityThread.main(ActivityThread.java:4786) 
336
-
337
- at java.lang.reflect.Method.invokeNative(Native Method) 
338
-
339
- at java.lang.reflect.Method.invoke(Method.java:511) 
340
-
341
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
342
-
343
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
344
-
345
- at dalvik.system.NativeStart.main(Native Method) 
346
-
347
-
348
-
349
421
  ```