티스토리 뷰
1. 먼저 DatabaseHelper 클래스를 만든다.
package com.example.sqlite_sample;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db"; // 1. 데이터베이스 네임
public static final String TABLE_NAME = "students"; //2. 테이블 네임
// 3. 테이블 항목 네임
public static final String COL_1 = "ID";
public static final String COL_2 = "Name";
public static final String COL_3 = "Surname";
public static final String COL_4 = "Marks";
public DatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+ TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
// 추가하기
public boolean insertData(String name, String surname, String marks){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
long result = db.insert(TABLE_NAME, null,contentValues);
if(result == -1)
return false;
else
return true;
}
}
2. 레이아웃에 추가 입력할것들을 만든다.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="7dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/editText_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginTop="19dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Surname"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText_name"
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/editText_marks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="19dp"
android:layout_marginTop="29dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Marks"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText_surname"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="52dp"
android:layout_marginTop="55dp"
android:text="ADD"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText_marks" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. MainActivity에 다음과 같이 코딩한다.
package com.example.sqlite_sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editName, editSurname, editMarks;
Button btnAddData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
editName = findViewById(R.id.editText_name);
editSurname = findViewById(R.id.editText_surname);
editMarks = findViewById(R.id.editText_marks);
btnAddData = findViewById(R.id.button_add);
AddData();
}
public void AddData() {
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(editName.getText().toString(),
editSurname.getText().toString(),
editMarks.getText().toString() );
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
});
}
}
실행해서 확인해본다.
1. 실행에서 입력한다.
2. 데이터베이스파일이 잘 생성됐는지 확인하고 따로 저장해서 입력된 내용이 맞는지 확인해본다.
3. 입력된 내용이 맞는지 확인은 DB Browser for SQLite 에서 확인하면 된다.
'프로그래밍 > 안드로이드 앱프로그래밍' 카테고리의 다른 글
안드로이드 SQLite 업데이트(수정하기) (0) | 2021.04.08 |
---|---|
안드로이드 sqlite 추가하고 보여주기 (0) | 2021.04.08 |
FFMpeg를 이용해 안드로이드 동영상 편집 앱 만들기 (0) | 2020.06.24 |
Importing Image From Camera (0) | 2020.06.24 |
서버/클라이언트 소켓(SOCKET) 통신하기 (3) | 2020.06.24 |
댓글