2swan

SQLite 일기장 앱 본문

Programming/Android Example

SQLite 일기장 앱

2swan 2023. 8. 9. 03:09
열 이름 데이터 형식
diaryDate char(10)
content varchar(500)

 

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=".MainActivity3">

    <DatePicker
        android:id="@+id/datePicker1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:calendarViewShown="false"
        android:datePickerMode="spinner" />

    <EditText
        android:id="@+id/edtDiary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="6"
        android:background="#FFFF99"/>

    <Button
        android:id="@+id/btnWrite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="수정하기"
        android:textSize="20sp" />

</LinearLayout>

 

main.activity

public class MainActivity3 extends AppCompatActivity {
    SQLiteDatabase sqLiteDatabase;
    String  diaryDate;
    MyDBHelper myDBHelper;
    Button btnWrite;
    EditText edtDiary;
    DatePicker datePicker;

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

        datePicker = findViewById(R.id.datePicker1);
        edtDiary = findViewById(R.id.edtDiary);
        btnWrite = findViewById(R.id.btnWrite);
        myDBHelper = new MyDBHelper(this);

        //DB 설정
        sqLiteDatabase = myDBHelper.getWritableDatabase();
        myDBHelper.onUpgrade(sqLiteDatabase,1,2);

        //초기화
        //오늘날짜
        Calendar calendar  = Calendar.getInstance();
        int cYear = calendar.get(Calendar.YEAR);
        int cMonth = calendar.get(Calendar.MONTH);
        int cDay = calendar.get(Calendar.DAY_OF_MONTH);
        diaryDate = cYear+"-"+(cMonth+1)+"-"+cDay;
        edtDiary.setText(diaryDate);
        btnWrite.setEnabled(true);
        //오늘 날짜의 일기 선택
        edtDiary.setText(readDiary(diaryDate));

        //날짜선택
        datePicker.init(cYear, cMonth, cDay, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int year, int month, int day) {
                diaryDate = year + "-" + (month+1) + "-" + day;
                //선택된 날짜 일기 select
                edtDiary.setText(readDiary(diaryDate));
            }

        });

        //쓰기
        btnWrite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sqLiteDatabase = myDBHelper.getWritableDatabase();
                String sql = "insert into myDiary values(' "+diaryDate+" '," +
                    " ' "+edtDiary.getText().toString()+" ')";

                if(btnWrite.getText().equals("수정하기")){
                    sql = "update myDiary " +
                            "set content = ' "+edtDiary.getText().toString()+" ' " +
                            " write diaryDate = ' " + diaryDate+ " ' ";
                }
                sqLiteDatabase.execSQL(sql);
                sqLiteDatabase.close();
                Toast.makeText(MainActivity3.this, "입력",Toast.LENGTH_SHORT).show();
                }
             });
        }

    private String readDiary(String diaryDate){
        String strResult = "";
        sqLiteDatabase = myDBHelper.getReadableDatabase();
                String sql = "select * from myDiary where diaryDate =' "+diaryDate+" ' " ;
        Cursor  cursor = sqLiteDatabase.rawQuery(sql, null);
        if(cursor.moveToNext()) {
            strResult = cursor.getString(0);
            btnWrite.setText("수정하기");
            Toast.makeText(this, "일기조회", Toast.LENGTH_SHORT).show();

        }else{
            btnWrite.setText("새로저장");
            edtDiary.setHint("");
            edtDiary.setHint("일기 없음");
        }
        return strResult;

    }

    //내부 클래스
    public static class  MyDBHelper extends SQLiteOpenHelper {
        //생성자
        public MyDBHelper(Context context){
            super(context, "myDB", null,1);
        }
        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("create table if not exists myDiary(diaryDate char(10), content varchar(500))");
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
            sqLiteDatabase.execSQL("drop table if exists myDiary");
            onCreate(sqLiteDatabase);
        }
    }

}

결과 값

'Programming > Android Example' 카테고리의 다른 글

RecyclerView 예제  (0) 2023.08.09
RecyclerView Image  (0) 2023.08.09
RecyclerView 목록 만들기  (0) 2023.08.09
Fragment 예제  (0) 2023.08.08
SQLite 예제2  (0) 2023.08.08