2swan
SQlite 예제 본문
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=".MainActivity2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이름 : "
android:textSize="30sp"/>
<EditText
android:id="@+id/edtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="인원 : "
android:textSize="30sp"/>
<EditText
android:id="@+id/edtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btnInit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="초기화"
android:textSize="20sp"/>
<Button
android:id="@+id/btnInsert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="입력"
android:textSize="20sp"/>
<Button
android:id="@+id/btnSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="조회"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btnUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="수정"
android:textSize="20sp"/>
<Button
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="삭제"
android:textSize="20sp"/>
<Button
android:id="@+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="일괄추가"
android:textSize="20sp"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="8"
android:orientation="horizontal">
<TextView
android:id="@+id/edtNameResult"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="top"
android:textSize="25sp"
android:textStyle="bold"
android:padding="20dp"/>
<TextView
android:id="@+id/edtNumberResult"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="top"
android:textSize="25sp"
android:textStyle="bold"
android:padding="20dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
main.activity
public class MainActivity2 extends AppCompatActivity {
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button btnInit = findViewById(R.id.btnInit);
Button btnInsert = findViewById(R.id.btnInsert);
Button btnSelect = findViewById(R.id.btnSelect);
Button btnTest = findViewById(R.id.btnTest);
Button btnUpdate = findViewById(R.id.btnUpdate);
Button btnDelete = findViewById(R.id.btnDelete);
EditText edtName = findViewById(R.id.edtName);
EditText edtNumber = findViewById(R.id.edtNumber);
TextView edtNumberResult = findViewById(R.id.edtNumberResult);
TextView edtNameResult = findViewById(R.id.edtNameResult);
MyDBHelper myDBHelper = new MyDBHelper(this);
//초기화
btnInit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sqLiteDatabase = myDBHelper.getWritableDatabase();
myDBHelper.onUpgrade(sqLiteDatabase,1,2);
sqLiteDatabase.close();
}
});
//추가
btnInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sqLiteDatabase = myDBHelper.getWritableDatabase();
String sql = "insert into groupTBL values(?,?)";
SQLiteStatement statement = sqLiteDatabase.compileStatement(sql);
statement.bindString(1,edtName.getText().toString());
statement.bindString(2,edtNumber.getText().toString());
statement.execute();
sqLiteDatabase.close();
Toast.makeText(getApplicationContext(), " 입력", Toast.LENGTH_SHORT).show();
}
});
//조회
btnSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sqLiteDatabase = myDBHelper.getReadableDatabase();
String sql = " select * from groupTBL";
Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
String strName = "그룹 이름 : \n";
String strNumber = "그룹 번호 : \n";
while (cursor.moveToNext()){
strName += cursor.getString(0) +"\n";
strNumber += cursor.getString(1) + "\n";
}
edtNameResult.setText(strName);
edtNumberResult.setText(strNumber);
cursor.close();
sqLiteDatabase.close();
}
});
//수정
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(edtName. getText().toString().equals("")){
Toast.makeText(getApplicationContext(),
"이름 입력", Toast.LENGTH_SHORT).show();
return;
}
sqLiteDatabase = myDBHelper.getWritableDatabase();
String sql = "update groupTBL set gNumber = " + edtNumber.getText().toString() +
" where gName='"+edtName.getText().toString().trim()+"' ";
Log.d("update", sql);
sqLiteDatabase.execSQL(sql);
Toast.makeText(getApplicationContext(), "수정 완료", Toast.LENGTH_SHORT).show();
sqLiteDatabase.close();
btnSelect.callOnClick();
}
});
//삭제
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(edtName. getText().toString().equals(" ")){
Toast.makeText(getApplicationContext(),
"이름 입력", Toast.LENGTH_SHORT).show();
return;
}
sqLiteDatabase = myDBHelper.getWritableDatabase();
String sql = "delete from groupTBL where gName = '"+edtName.getText().toString()+"' ";
sqLiteDatabase.execSQL(sql);
Toast.makeText(getApplicationContext(), "삭제 완료", Toast.LENGTH_SHORT).show();
btnSelect.callOnClick();
}
});
//일괄추가
btnTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
public class MyDBHelper extends SQLiteOpenHelper{
public MyDBHelper(@Nullable Context context) {
super(context, "groupDB", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table " +
"groupTBL(gName char(20) primary key," +
"gNumber integer);");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("Drop table if exists groupTBL");
onCreate(sqLiteDatabase);
}
}
}
결과 값
'Programming > Android Function' 카테고리의 다른 글
ImageView & Toast (0) | 2023.08.12 |
---|---|
Intent 화면전환 (0) | 2023.08.12 |
ListView (0) | 2023.08.05 |
StartActivityForResult (0) | 2023.08.05 |
Thread & Handler (0) | 2023.08.05 |