본문 바로가기
IT/Android

안드로이드 SQLite DB 활용한 고객등록과 조회 3-2

by Jeami 2013. 7. 19.
반응형


지난 포스팅에 이어 "안드로이드 SQLite DB 활용한 고객등록과 조회" 이어가도록 하겠습니다.

일단 DBManager.java 파일까지 진행되었는데요. 코딩 설명은 자세하게 기재하지는 않았습니다.

코딩에 보면 주석으로 달아놓은 부분을 참고해 주시고요.

이번에는 좀 더 설명을 곁들여서 포스팅 해보도록 하겠습니다.



이 글이 도움이 되신다면, 로그인 필요없이 추천버튼 클릭 부탁드립니다.

최고의 정보로 보답드릴게요^^




파일이 어떻게 서로 연관되어 있는지 살펴볼까요.


strings.xml : 앱 라벨에 대한 텍스트 리소스 정의

main.xml : 고객정보 화면 출력 부분과 버튼 부분을 만들어줍니다.

CustomerListActiviry.java : 프로젝트 생성시 정의되는 메인 액티비티인데요.

  SQliteOpenHelper 클래스로부터 상속받아 만든 DBManager 클래스를

  이용하여 DB와 테이블을 생성합니다.

  기본 로직은 customers 테이블에서 고객정보를 추출 후 main.xml로 만든

  화면 레이아웃에 정보를 뿌립니다. 화면에 생성된 등록버튼을 클릭하면

  CustomerRegActivity.java 클래스가 호출되는데요. 그 다음 과정을 보면

  AndroidManifest.xmlstrings.xml에 정의된 Text 리소스를 이용하여

  어플리케이션명 & 액티비티명을 지정합니다.


연습할때는 책에 나와있는 순서대로 코딩을 진행하면 되지만, 실무라든가 어플개발시에는 각 파일간의

관계를 즉 개념을 제대로 알고 있어야 키보드로 코딩을 할 수 있습니다. 그렇기 때문에, 단순코딩보다는

개념을 이해하고, 각 챕터에 새로이 나오는 문법을 일단 눈에 익숙하게 해두시면 좋을 것 같네요^^

 




* 지난번 포스팅에 strings.xml 을 깜빡했네요^^ 사실 코딩할 내용이 없어서 무시했는데요. 다음과 같습니다. *

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">고객관리</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="act_join_form">고객목록</string>
       
    <string name="name">성명</string>
    <string name="sex">성별</string>
    <string name="receive">수신여부</string>
</resources>




사용 클래스 및 메소드 이해

Cursor :

moveToNext() : cursor를 다음행으로 이동

getString(int) : 요청된 열의 값을 문자열로 반환

close() : 커서 닫기


SQLiteDatabase

rawQuery(String, string[]) : string형으로 작성된 조회문을 실행한 결과로 생긴 데이터셋에 대한

    cursor을 반환.


SQLiteOpenHelper

getReadableDatabase() : 데이터베이스를 생성하거나 오픈함.

       데이터베이스 객체인 SQLiteDatabase 객체 반환.


TextView

append(ChatSequence) : 명시된 문자를 TextView에 첨부



SQL 구문에 대한 이해

간단한 예로 살펴보도록 할게요.

==> select name, sex, sms from customers

select 를 이용하여 DB를 추출하면, 데이터가 존재할 경우 커서(Cursor)는 BOF(Begin Of File)를 가르킵니다. 즉 파일의 첫번째 줄 맨 앞에 있다는 말입니다.

Cursor 클래스의 moveToNext() 메소드를 이용하여 다음 행으로 이동하여 데이터를 조회하게 되고, 만약 마지막 행의 다음인 EOF(End Of File)를 가르키고 있다면 moveToNext() 는 null 값을 반환합니다.

cursor가 임의의 행을 가르키고 있을때 첫 번째 열, 두 번쨰 열 등의 데이터를 추출할 때는 getString(0), getString(1)과 같이 실행하여 데이터를 읽어옵니다.




안드로이드


join_form.xml ==> 고객등록 폼으로, 프로그램 실행 후 처음 레이아웃에서 "등록"버튼을 클릭했을 때 이동하게 되는 레이아웃입니다.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
   >
   <!-- 이름입력 -->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
     >
        <TextView
            android:layout_width="60sp"
            android:layout_height="wrap_content"
            android:text="이름:  "
            android:textSize="26dp"
            android:textColor="#FF0000"
            android:background="#00FF00"
        />
        <EditText
             android:id="@+id/edit_name"
             android:layout_width="fill_parent"
               android:layout_height="wrap_content"  
         />   
    </LinearLayout>
   
   <!-- 성별입력 -->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
     >
        <TextView
            android:layout_width="60sp"
            android:layout_height="wrap_content"
            android:text="성별:  "
        />
        <RadioGroup
             android:id="@+id/radiogroup_sex"
             android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:orientation="horizontal"
         >
             <RadioButton
                 android:id="@+id/radio_male"
                 android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="남자  "
              />
             <RadioButton
                 android:id="@+id/radio_female"
                 android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text=" 여자"
              />
         </RadioGroup>   
    </LinearLayout>
   
     <!-- 수신여부 -->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
     >
        <TextView
            android:layout_width="60sp"
            android:layout_height="wrap_content"
            android:text="sms"
        />
        <CheckBox
            android:id="@+id/check_sms"
             android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="SMS"
         />   
    </LinearLayout>
   
       <!-- 버튼 -->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
     >
        <Button
             android:id="@+id/button_store"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="등록버튼"  
             android:textSize="26dp"
         />
        <Button
             android:id="@+id/button_list"
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:text="목록버튼"  
             android:textSize="26dp"
         />   
    </LinearLayout>
   
</LinearLayout>



다음 포스팅에서는 CustomerRegActivity.java 파일과 AndroidManifest.xml 편집까지 마무리해보도록 할게요. 소스가 생각보다 기네요^^



반응형

loading