728x90

# 실행 화면

# activity_main.xml

<?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=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="수업명 : "/>
        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="인원 : "/>
        <EditText
            android:id="@+id/num"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/init"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="초기"/>
        <Button
            android:id="@+id/insert"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="입력"/>
        <Button
            android:id="@+id/update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="수정"/>
        <Button
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="삭제"/>
        <Button
            android:id="@+id/select"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="조회"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FBC72C"
        android:gravity="center"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/result_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingLeft="12dp"
            android:background="@null"/>
        <EditText
            android:id="@+id/result_num"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingLeft="12dp"
            android:background="@null"/>
    </LinearLayout>
</LinearLayout>

# MainActivity.java

package kr.ac.skuniv.dbtest;

import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    DBHelper myHelper;
    SQLiteDatabase db;
    EditText edit_name, edit_num, result_name, result_num;
    Button init, insert, update, delete, select;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("수업 관리 DB");

        edit_name = (EditText) findViewById(R.id.name);
        edit_num = (EditText) findViewById(R.id.num);
        result_name = (EditText) findViewById(R.id.result_name);
        result_num =  (EditText) findViewById(R.id.result_num);

        init = (Button) findViewById(R.id.init);
        insert = (Button) findViewById(R.id.insert);
        update = (Button) findViewById(R.id.update);
        delete = (Button) findViewById(R.id.delete);
        select = (Button) findViewById(R.id.select);

        myHelper = new DBHelper(this);

        init.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db = myHelper.getWritableDatabase();
                myHelper.onUpgrade(db, 1, 2);

                result_name.setText("");
                result_num.setText("");

                db.close();
            }
        });

        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String name = edit_name.getText().toString();
                    String num = edit_num.getText().toString();

                    db = myHelper.getWritableDatabase();
                    db.execSQL("INSERT INTO class VALUES('" + name + "'," + num + ");");
                    show();
                    db.close();
                }catch (Exception e) {
                    e.printStackTrace();
                    edit_name.setText("");
                    edit_num.setText("");
                }
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String name = edit_name.getText().toString();
                    String num = edit_num.getText().toString();

                    db = myHelper.getWritableDatabase();
                    db.execSQL("UPDATE class SET num=" + num + " WHERE name=" + "\"" + name + "\"");
                    show();
                } catch (Exception e) {
                    e.printStackTrace();
                    edit_name.setText("");
                    edit_num.setText("");
                }
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String name = edit_name.getText().toString();

                    db = myHelper.getWritableDatabase();
                    db.execSQL("DELETE FROM class WHERE name=" + "\"" + name + "\"");
                    db.close();
                    show();
                } catch (Exception e) {
                    e.printStackTrace();
                    edit_name.setText("");
                    edit_num.setText("");
                }
            }
        });

        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show();
            }
        });
    }
    public void show(){
        db = myHelper.getReadableDatabase();
        Cursor cursor;
        cursor = db.rawQuery("SELECT * FROM class;", null);

        StringBuilder name = new StringBuilder("수업명" + "\r\n" + "--------" + "\r\n");
        StringBuilder num = new StringBuilder("인원" + "\r\n" + "--------" + "\r\n");

        while (cursor.moveToNext()) {
            name.append(cursor.getString(0)+"\r\n");
            num.append(cursor.getString(1)+"\r\n");
        }
        result_name.setText(name.toString());
        result_num.setText(num.toString());

        cursor.close();
        db.close();
    }
}

# DBHelper.java

package kr.ac.skuniv.dbtest;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

class DBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "class.db";
    private static final int DATABASE_VERSION = 2;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE class (name text PRIMARY KEY, num integer);");
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS class");
        onCreate(db);
    }
}
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기