취소

커뮤니티

가이드

서포트

전체

 

 

퍼갈 때에는 반드시 저작자의 허락과 저작자의 이름(아이디)를 기록하어야 합니다.

저작자는 Snails(tjdtnsu)입니다.

제발 덧글 좀 적어주세요. 강의 적는 시간은 1시간이지만 덧글은 1분도 걸리지 않습니다.

 

참고 : 이 강좌는 초보자를 위한 Eclipse를 사용하였습니다.

올리는 곳 : 제 네이버 블로그, 디벨로이드 카페

업로드 시간 : 매주 일요일 오후 11시

 난이도 : ★★★★★★★





















 

네 이제 할게 슬슬 없어지기 시작합니다. 

오늘은 데이터베이스로 가겠습니다. 

 

 

여기서 데이터베이스의 활용 순서는 

(데이터베이스 생성 -> 테이블 생성 -> 레코드 추가)     -> 데이터 조회 

로 일단 생성을 해야겠죠

 

코드를 보면서 이해를 해야 할 듯합니다.(엄청난 길이는 스포)

 

 

 

 

 

 

xml 파일 

 

<LinearLayout xmlns:androhttp://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"    xmlns:tools="http://schemas.android.com/tools"    android:layout_    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingTop="10dp" >

    <LinearLayout      android:orientation="horizontal"     android:layout_     android:layout_height="wrap_content"     android:paddingLeft="10dp"     >     <Button    android:   android:layout_   android:layout_height="wrap_content"   android:   android:text="데이터베이스 생성"   android:textSize="14dp"   />  <EditText     android:      android:layout_       android:layout_height="wrap_content"       android:hint="데이터베이스 이름 입력"      android:text="Database.db"      android:textSize="18dp"      /> </LinearLayout> <LinearLayout      android:orientation="horizontal"     android:layout_     android:layout_height="wrap_content"     android:paddingLeft="10dp"     >     <Button    android:   android:layout_   android:layout_height="wrap_content"   android:   android:text="테이블 생성"   android:textSize="14dp"   />  <EditText     android:      android:layout_       android:layout_height="wrap_content"       android:hint="테이블 이름 입력"      android:text="Table1"      android:textSize="18dp"      /> </LinearLayout> <TextView    android:     android:layout_      android:layout_height="wrap_content"      android:text="상태 : "     android:textSize="14dp"     android:paddingLeft="10dp"     android:layout_marginTop="10dp"     />

</LinearLayout>  

 java 파일

 

package naver.maybeeasyandroidprogramming; import android.app.Activity;import android.content.ContentValues;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView; public class MainActivity extends Activity {     String databaseName;    String tableName;    TextView status;    boolean databaseCreated = false;    boolean tableCreated = false;     SQLiteDatabase db;     @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         final EditText databaseNameInput = (EditText) findViewById(R.id.databaseNameInput); // EditText 값 불러옴        final EditText tableNameInput = (EditText) findViewById(R.id.tableNameInput);         Button createDatabaseBtn = (Button) findViewById(R.id.createDatabaseBtn);        createDatabaseBtn.setOnClickListener(new OnClickListener() { //누르면 이벤트 발생            public void onClick(View v) {                databaseName = databaseNameInput.getText().toString();                createDatabase(databaseName); // createDatabase함수 실행            }        });         Button createTableBtn = (Button) findViewById(R.id.createTableBtn); // 위와 같음        createTableBtn.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                tableName = tableNameInput.getText().toString();                createTable(tableName);                int count = insertRecord(tableName);                println(count + " records inserted.");            }        });         status = (TextView) findViewById(R.id.status);     }     private void createDatabase(String name) {  //여기서부터 createDatabase 함수        println("creating database [" + name + "].");         try {            db = openOrCreateDatabase(                    name,                    MODE_WORLD_WRITEABLE,                    null); //데이터베이스 생성                 databaseCreated = true; //정상적이면 true            println("database is created.");        } catch(Exception ex) {            ex.printStackTrace();            println("database is not created."); // 아니면 false        }    }     private void createTable(String name) { //여기서부터 createTable        println("creating table [" + name + "].");         db.execSQL("create table if not exists " + name + "("                + " _id integer PRIMARY KEY autoincrement, "                + " name text, "                + " age integer, "                + " phone text);" ); //테이블 생성         tableCreated = true;    }     private int insertRecord(String name) {        println("inserting records into table " + name + ".");         int count = 3;        db.execSQL( "insert into " + name + "(name, age, phone) values ('John', 20, '010-7788-1234');" );        db.execSQL( "insert into " + name + "(name, age, phone) values ('Mike', 35, '010-8888-1111');" );        db.execSQL( "insert into " + name + "(name, age, phone) values ('Sean', 26, '010-6677-4321');" );         return count;    }      private void println(String msg) {        Log.d("SampleDatabase", msg);        status.append("\n" + msg);     }      @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }     }  

 

뭐 이렇습니다.

내일은 데이터를 업그레이드해보도록 하겠습니다.

 

</td></tr></tbody></table>