4 분 소요

추가, 수정, 삭제 기능 구현입니다.

추가 기능이란 일정 글 쓰기, 연락처 추가를 하는 기능입니다.

Insert문을 사용하여 데이터베이스에 데이터를 추가하도록 했습니다.

수정 기능이란 작성된 일정 또는 연락처의 내용을 수정 하는 기능입니다.

삭제 기능이란 말 그대로 기존 데이터를 삭제 하는 기능입니다.

SQL문이 조금 복잡하나 유튜브에 찾아보시면 설명잘된것들이 많으니 잠깐 보시는것을 추천합니다.

일정 추가, 수정, 삭제 기능

DiaryAddActivity.java (추가)

package com.example.mydiary;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class DiaryAddActivity extends AppCompatActivity {
    private ImageButton button_back;
    private Button button_save;
    private EditText title, content;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary_writing);

        initView();

        //뒤로가기 버튼
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent goBack = new Intent(getApplicationContext(),DiaryActivity.class);
                startActivity(goBack);
                finish();
            }
        });

        //글 추가 버튼
        button_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                insert(title.getText().toString(),content.getText().toString());

                Intent intent = new Intent(getApplicationContext(),DiaryActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    //글 쓰기(Insert 문)
    public void insert(String title, String content){

        DBHelper helper = new DBHelper(this);
        SQLiteDatabase database = helper.getReadableDatabase();

        database.execSQL("INSERT INTO DIARY(title, content) VALUES('" + title + "' , '" + content + "');");
    }

    //뷰 초기화
    public void initView(){
        title = findViewById(R.id.title);
        content = findViewById(R.id.content);
        button_back = findViewById(R.id.button_back);
        button_save = findViewById(R.id.button_save);

    }
}

DiaryDetail.java (수정, 삭제)

package com.example.mydiary;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class DiaryDetail extends AppCompatActivity {

    private ImageButton button_back;
    private Button button_change,button_delete;
    private TextView txtTitleSpace, txtContentSpace;

    public DBHelper helper = new DBHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary_details);

        initViews();

        //Intent를 받아와 상세정보 표시(데이터 연동)
        Intent intent = getIntent();
        String sub_title = intent.getStringExtra("제목");
        String sub_content = intent.getStringExtra("내용");
        txtTitleSpace.setText(sub_title);
        txtContentSpace.setText(sub_content);

        String old_title = sub_title;
        String old_content = sub_content;

        //뒤로가기 버튼클릭
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent goBack = new Intent(getApplicationContext(),DiaryActivity.class);
                startActivity(goBack);
                finish();
            }
        });

        //수정 버튼클릭
        button_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Update(old_title, txtTitleSpace.getText().toString(),old_content, txtContentSpace.getText().toString());

                Intent intent = new Intent(getApplicationContext(),DiaryActivity.class);
                startActivity(intent);
                finish();
            }
        });

        //삭제 버튼클릭
        button_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Delete(txtTitleSpace.getText().toString());

                Intent intent = new Intent(getApplicationContext(),DiaryActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }


    //뷰 초기화
    public void initViews(){
        button_back = findViewById(R.id.button_back);
        button_change = findViewById(R.id.button_change);
        button_delete = findViewById(R.id.button_delete);
        txtTitleSpace = findViewById(R.id.txtTitleSpace);
        txtContentSpace = findViewById(R.id.txtContentSpace);
    }


    //데이터 삭제
    public void Delete(String title) {
        SQLiteDatabase database = helper.getReadableDatabase();
        database.execSQL("DELETE FROM DIARY WHERE title = '" + title + "'");
    }

    //데이터 수정(업데이트)
    public void Update(String old_title, String new_title, String old_content, String new_content){
        SQLiteDatabase database = helper.getWritableDatabase();
        database.execSQL("UPDATE DIARY SET title = '" + new_title + "', content = '" + new_content + "' WHERE title = '" + old_title + "' AND content = '" + old_content + "'");
    }
}

연락처 추가, 수정, 삭제 기능

PhoneBookAddActivity.java (추가)

package com.example.mydiary;


import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.appcompat.app.AppCompatActivity;

public class PhoneBookAddActivity extends AppCompatActivity {
    private ImageButton button_back;
    private EditText input_Name, input_Phone;
    private Button button_add;
    public  DBHelper1 helper = new DBHelper1(this);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phonebook_add);

        initViews();

        //뒤로가기 버튼
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent goBack = new Intent(getApplicationContext(),PhoneBookActivity.class);
                startActivity(goBack);
                finish();
            }
        });

        //연락처 추가버튼
        button_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Insert(input_Name.getText().toString(),input_Phone.getText().toString());

                Intent intent = new Intent(getApplicationContext(),PhoneBookActivity.class);
                startActivity(intent);
                finish();
            }
        });



    }

    //뷰 초기화
    public void initViews(){
        input_Name = findViewById(R.id.input_Name);
        input_Phone = findViewById(R.id.input_Phone);
        button_back = findViewById(R.id.button_back);
        button_add = findViewById(R.id.button_add);
    }

    //데이터 삽입
    public void Insert(String name, String phonenum){
        SQLiteDatabase database = helper.getReadableDatabase();

        database.execSQL("INSERT INTO PHONEBOOK(name, phonenum) VALUES('" + name + "' , '" + phonenum + "');");
    }
}

PhoneBookDetail.java (수정, 삭제)

package com.example.mydiary;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class PhoneBookDetail extends AppCompatActivity {

    private ImageButton button_back2, imageButton2;
    private Button button_change2,button_delete2;
    private EditText txtPhonenumSpace, txtNameSpace;

    public DBHelper1 helper = new DBHelper1(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phonebook_details);

        initViews();

        //Intent를 받아와 상세정보 표시(데이터 연동)
        Intent intent = getIntent();
        String sub_name = intent.getStringExtra("이름");
        String sub_phonenum = intent.getStringExtra("전화번호");
        txtNameSpace.setText(sub_name);
        txtPhonenumSpace.setText(sub_phonenum);

        String old_name = sub_name;
        String old_phonenum = sub_phonenum;

        //뒤로가기 버튼클릭
        button_back2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent goBack = new Intent(getApplicationContext(),PhoneBookActivity.class);
                startActivity(goBack);
                finish();
            }
        });

        //수정 버튼클릭
        button_change2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Update(old_name, txtNameSpace.getText().toString(),old_phonenum, txtPhonenumSpace.getText().toString());

                Intent intent = new Intent(getApplicationContext(),PhoneBookActivity.class);
                startActivity(intent);
                finish();
            }
        });

        //삭제 버튼클릭
        button_delete2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Delete(txtNameSpace.getText().toString());

                Intent intent = new Intent(getApplicationContext(),PhoneBookActivity.class);
                startActivity(intent);
                finish();
            }
        });

        //전화 걸기
        imageButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tel = "tel:" + txtPhonenumSpace.getText().toString();
                startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(tel)));
            }
        });
    }


    //뷰 초기화
    public void initViews(){
        button_back2 = findViewById(R.id.button_back2);
        button_change2 = findViewById(R.id.button_change2);
        button_delete2 = findViewById(R.id.button_delete2);
        imageButton2 = findViewById(R.id.imageButton2);
        txtNameSpace = findViewById(R.id.txtNameSpace);
        txtPhonenumSpace = findViewById(R.id.txtPhonenumSpace);
    }


    //데이터 삭제
    public void Delete(String name) {
        SQLiteDatabase database = helper.getReadableDatabase();
        database.execSQL("DELETE FROM PHONEBOOK WHERE name = '" + name + "'");

    }

    //데이터 수정
    public void Update(String old_name, String new_name, String old_phonenum, String new_phonenum){
        SQLiteDatabase database = helper.getWritableDatabase();
        database.execSQL("UPDATE PHONEBOOK SET name = '" + new_name + "', phonenum = '" + new_phonenum + "' WHERE name = '" + old_name + "' AND phonenum = '" + old_phonenum + "'");
    }


}

댓글남기기