本文转自:http://instinctcoder.com/android-studio-sqlite-database-multiple-tables-example/
In this tutorial we going to do a little complex SQL statement which I think will help you to further understand how to work with multiple tables in SQLite database.(Sorry, i don’t like things that are complicated too but when it come to more tables…Thing has to be a little bit complex then it’s only become make sense). And, we’ve a tutorial that helps readers who are new in SQLite database so I strongly suggest you to go through this tutorial. Tool Used
1. Android Studio 1.5.1
To Do
In this tutorial we will going to create a simple app that allow to Create, Retrieve, Update, and Delete, student records and will directly output the query’s result to Logcat’s window panel.
View Demo
Table Structure
Sample Data
Screen Layout
From above image, these are few things we would like to show you
1. Display Student-Course-Grade – This button will show you how the INNER JOIN work
2. Display Course Name-Grade-Total – This button will show you how the COUNT, GROUP BY, ORDER BY work
3. COURSE NOT TAKE BY STUDENT – This button will show you how the LEFT OUTER JOIN work
4. FAIL STUDENT MAJOR IN ‘BU’ – This button will show you how to UPDATE 2 Tables In a Single Statement
5. DELETE ‘BU’ STUDENT FROM SCHOOL – This button will show you how the EXECUTE 2 DELETE Statements IN Transaction Block
Code The Layout
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
>
Button
>
Button
>
Button
>
Button
>
Button
>
Button
>
TextView
>
>
|
Code
1. Let’s start create project, SQLiteDBMultiTbl. The setting of the project are show as below images which is quite straight forward.
2. Next, we’ll first create all packages as show in below image and then go through 1 by 1.
After all the packages has created as above image, please navigate to data > model. Inside this package will contains all the Classes/Schema that will use it to create table in SQLite database. Inside this package, add all classes name and code as below
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
;
/**
* Created by Tan on 1/26/2016.
*/
{
;
;
// Labels Table Columns names
;
;
;
;
{
;
}
{
;
}
{
;
}
{
;
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
;
/**
* Created by Tan on 1/26/2016.
*/
{
;
;
// Labels Table Columns names
;
;
;
;
{
;
}
{
;
}
{
;
}
{
;
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
;
/**
* Created by Tan on 1/26/2016.
*/
{
;
;
// Labels Table Columns names
;
;
;
;
;
;
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
;
/**
* Created by Tan on 1/27/2016.
*/
{
;
;
// Labels Table Columns names
;
;
;
;
;
;
;
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
}
|
5. Let’s move on to data package, In order for us to create tables we need to use these classes SQLiteDatabase, SQLiteDatabase is the class for us to perform CRUD function, and SQLiteOpenHelper, SQLiteOpenHelper is a helper class to manage database creation and version management. We’ll create a class and name it DBHelper.java and paste the following contents.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
;
/**
* Created by Tan on 1/26/2016.
*/
;
;
;
;
;
;
;
;
;
;
;
;
;
{
//version number to upgrade database version
//each time if you Add, Edit table, you need to change the
//version number.
;
// Database Name
;
;
{
;
}
Override
{
//All necessary tables you like to create will create here
;
;
;
;
}
Override
{
;
// Drop table if existed, all data will be gone!!!
;
;
;
;
;
}
}
|
6. We’ll show you how to handle the database connection in better way in term of avoid any chances of memory leak and access to your android database in thread safe, blah, blah… In short, a better solution. We’re just trying to tell you D-R-Y and we know there are people out there already did their research and come out solution on handling the database connection and one of those is dmytrodanylyk. So, We as a programmer, need to keep in mind to D-R-Y. :). And now, in the same data package create a class name DatabaseManager.java and paste the following code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
;
;
;
/**
* Created by Tan on 1/26/2016.
*/
{
;
;
;
;
{
{
;
;
}
}
{
{
+
;
}
;
}
{
;
{
// Opening new database
;
}
;
}
{
;
{
// Closing database
;
}
}
}
|
6. Now, let’s move on to the next package data > repo, the purpose of this package is for us to keep all database related query. And, We’ll begin with CourseRepo.java class
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
;
;
;
;
;
;
;
;
;
;
/**
* Created by Tan on 1/26/2016.
*/
{
;
{
;
}
{
;
}
{
;
;
;
;
;
// Inserting Row
;
;
;
}
{
;
;
;
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
;
;
;
;
;
;
;
/**
* Created by Tan on 1/26/2016.
*/
{
;
{
;
}
{
;
}
{
;
;
;
;
;
// Inserting Row
;
;
;
}
{
;
;
;
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
/**
* Created by Tan on 1/27/2016.
*/
{
;
{
}
;
{
;
}
{
;
;
;
;
;
// Inserting Row
;
;
}
{
;
;
;
}
{
;
;
;
KEY_StudID
KEY_Name
KEY_CourseId
KEY_Grade
KEY_MajorId
KEY_StudID
KEY_CourseId
_MajorId
;
;
;
// looping through all rows and adding to list
{
{
;
;
;
;
;
;
;
;
;
;
}
;
;
;
}
//Show you the INNER JOIN
//Only the matched records between tables will show, To join records from both tables,
//For example, to join StudentCourse and Course Table, Course.CourseId = StudentCourse.CourseId
{
;
;
;
KEY_CourseId
KEY_Name
KEY_Grade
TABLE
KEY_CourseId
KEY_Name
_Name
;
;
;
// looping through all rows and adding to list
{
{
;
;
;
;
;
;
;
}
;
;
;
}
//Show you the LEFT OUTER JOIN
//The data from LEFT will be shown even not all records matched
//In below query, Course will be the left table.
//and if you like to see all data from Course,
//remove this line WHERE RunningID IS NULL
{
;
;
;
KEY_CourseId
KEY_Name
TABLE
KEY_CourseId
;
;
;
// looping through all rows and adding to list
{
{
;
;
;
;
;
}
;
;
;
}
//Show you update IN SINGLE statement with multiple tables
{
;
+
+
+
+
+
+
;
{
;
;
;
;
{
;
{
;
}
;
}
//Show you DELETE 2 statement execute in TRANSACTIONS
//The reason we use TRANSACTIONS is because we need to make
//sure data will delete properly so that if anything
//happened during the execution of the statement is either COMPLETE or ROLLBACK
//means either both tables data deleted or both tables not delete at all
{
;
=
;
=
;
{
;
;
;
;
;
;
{
;
{
;
}
;
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
;
;
;
;
;
;
;
/**
* Created by Tan on 1/26/2016.
*/
{
;
{
;
}
{
;
}
{
;
;
;
;
;
// Inserting Row
;
;
}
{
;
;
;
}
}
|
7. Let’s move on to App package, and app.java class under this package, this class will be the first file to be execute when the application running and it’s only execute once, the purpose of this file is to keep variable that we need to share across all the packages and these variables are mean to create one time and only has 1 instance in entire application life cycle, and we call this – Singleton.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
;
;
;
;
;
/**
* Created by Tan on 1/26/2016.
*/
{
;
;
Override
)
{
;
;
;
;
}
{
;
}
}
|
8. We now move to the model package, all files under this package we serve as ViewModel which is use it to store data retrieve from database and then pass this view into activity to display. And then add the following class into this package
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
;
/**
* Created by Tan on 1/28/2016.
*/
{
;
;
;
;
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
;
/**
* Created by Tan on 1/28/2016.
*/
{
;
;
{
;
}
{
;
}
{
;
}
{
;
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
;
/**
* Created by Tan on 1/27/2016.
*/
{
;
;
;
;
;
;
{
;
}
{
;
}
;
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
{
;
}
}
|
9. Now, go back to MainActivity.java. Paste the following code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
{
;
;
Override
{
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
}
{
;
;
;
;
;
;
;
;
//Insert Sample data if the table is empty
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
}
{
;
;
+
+
+
+
+
+
)
;
;
{
+
+
+
+
+
+
)
;
}
;
}
{
;
;
+
+
+
;
;
{
+
+
+
;
}
;
}
{
;
;
;
+
;
;
{
+
;
}
;
}
{
;
;
}
{
;
;
}
Override
{
// Inflate the menu; this adds items to the action bar if it is present.
;
;
}
Override
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
;
//noinspection SimplifiableIfStatement
{
;
}
;
}
Override
{
{
;
{
;
{
;
{
;
{
;
{
;
}
}
}
|
10. You may have question, How the IDE know need to execute App >app.java first? Good question!, They won’t know unless we configure this in app> src > main > AndroidManifest.xml and add the highlighted line as shown below
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
?>
>
application
>
activity
>
>
>
>
>
>
>
>
|
OK, That’s all for this tutorial and you will see result as above video if everything went smooth. A’m i confuse you? :). Please feel free to comment.