[해야할 일]
(1)activity_main_student.xml 에서 버튼들 밑 EditText 생성
(2)StudentDBManager.java에서 DB 연결 및 각 메서드 생성
(3)MainStudentActivity.java에서 onClick 메서드와 연결
(4)실행 및 결과
(1)activity_main_student.xml 에서 버튼들 밑 EditText 생성
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
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainStudentActivity">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button
android:id="@+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="추가" />
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="수정" />
<Button
android:id="@+id/query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="질의" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="삭제" />
</LinearLayout>
<EditText
android:id="@+id/resultView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
|
(2)StudentDBManager.java에서 DB 연결 및 각 메서드 생성
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
|
package com.heybaby.app0816;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class StudentDBManager {
static final String DB_STUDENTS = "Students.db";
static final String TABLE_STUDENTS = "Students";
static final int DB_VERSION = 1;
Context mcontext = null;
private SQLiteDatabase mDatabase = null;
static private StudentDBManager mDbManager = null;
private StudentDBManager (Context context){
mcontext = context;
mDatabase = context.openOrCreateDatabase(DB_STUDENTS, Context.MODE_PRIVATE, null); //데이터베이스 생성!
mDatabase.execSQL("CREATE TABLE IF NOT EXISTS Students (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"number TEXT," +
"name TEXT," +
"department TEXT," +
"grade INTEGER );");
}
static public StudentDBManager getInstance(Context context){
if(mDbManager == null){
mDbManager = new StudentDBManager(context);
}
return mDbManager;
}
public long insert(ContentValues contentValues){
/*mDatabase.execSQL("");*/
return mDatabase.insert(TABLE_STUDENTS, null, contentValues);
}
public Cursor query(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
mDatabase.rawQuery("", null);
//mDatabase.rawQuery("sql 문", null);
return mDatabase.query(TABLE_STUDENTS, columns, selection, selectionArgs, groupBy, having, orderBy);
}
public int update(ContentValues contentValues, String where ,String[] stringArgs){
return mDatabase.update(TABLE_STUDENTS, contentValues, where, stringArgs);
}
public int delete(String where, String[] whereAgrs){
return mDatabase.delete(TABLE_STUDENTS, where, whereAgrs);
}
}
|
(3)MainStudentActivity.java에서 onClick 메서드와 연결
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
|
package com.heybaby.app0816;
import android.content.ContentValues;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainStudentActivity extends AppCompatActivity {
EditText editText = null;
StudentDBManager dbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_student);
//결과 출력을 위한 edit 캐스팅
editText = findViewById(R.id.resultView);
//데이터베이스 컨트롤 위한 메니져 생성
dbManager = StudentDBManager.getInstance(this);
}
public void onClick(View view){
switch (view.getId()){
case R.id.insert :
ContentValues contentValues = new ContentValues();
contentValues.put("number", "20180819");
contentValues.put("name", "김진아");
contentValues.put("department", "accounting");
contentValues.put("grade", 1);
long insertId = dbManager.insert(contentValues);
editText.setText("레코드 추가" + insertId);
break;
case R.id.update :
ContentValues values = new ContentValues();
values.put("name", "고둘리");
int updateCnt = dbManager.update(values, null, null);
editText.setText("레코드 갱신 : " + updateCnt );
break;
case R.id.delete :
int deleteCnt = dbManager.delete("_id>1", null);
editText.setText("레레코드 삭제 : " +deleteCnt);
break;
case R.id.query :
String[] columns = {"_id", "number", "name", "department", "grade"};
Cursor c = dbManager.query(columns, null, null, null, null, null);
if(c!=null){
editText.setText("");
while(c.moveToNext()){
int id = c.getInt(0);
String number = c.getString(1);
String name = c.getString(2);
String department = c.getString(3);
int grade = c.getInt(4);
editText.append("id: " + id + "\n number: " + number + "\n name: " + name + " \n department: " + department + "\n grade: " + grade + "------------------------------- \n");
}
editText.append("\n total Count : " + c.getCount());
c.close();
}
break;
}
}
}
|
(4)실행 및 결과
댓글 없음:
댓글 쓰기