Notice
Recent Posts
Recent Comments
Link
ยซ   2025/04   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags
more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐ŸŒฑ dreaming DiNO

[Android Studio] SQL database ๋งŒ๋“ค๊ธฐ, create, insert, select ๋ณธ๋ฌธ

Android/Android Studio

[Android Studio] SQL database ๋งŒ๋“ค๊ธฐ, create, insert, select

MK_____ 2021. 11. 11. 00:21
package com.example.database;

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;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText editText;
    EditText editText2;
    TextView textView;

    //database ๊ฐ์ฒด
    SQLiteDatabase database;

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

        editText = findViewById(R.id.editText);
        editText2 = findViewById(R.id.editText2);
        textView = findViewById(R.id.textView);

        //database ๊ฐ์ฒด์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ฒ„ํŠผ ํด๋ฆญ
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String databaseName = editText.getText().toString();
                createDatabase(databaseName);
            }
        });

        //table ์ƒ์„ฑ๋ฆญํ•˜๊ธฐ ์œ„ํ•œ ๋ฒ„ํŠผ ํด๋ฆญ
        Button button2 = findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tableName = editText2.getText().toString();
                createTable(tableName);
            }
        });

        //์ปฌ๋Ÿผ ํ•œ์ค„ ์ถ”๊ฐ€ (INSERT) ์œ„ํ•œ ๋ฒ„ํŠผ ํด๋ฆญ
        Button button3 = findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                insertRecord();
            }
        });

        //SELECT
        Button button4 = findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                executeQuery();
            }
        });



    }

    public void createDatabase(String databaseName){
        println("createDatabase() ํ˜ธ์ถœ");

        try {
            //database ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜
            database = openOrCreateDatabase(databaseName, MODE_PRIVATE, null);
            println("๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ๋จ" + databaseName);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void createTable(String tableName){
        println("createTable() ํ˜ธ์ถœ");

        if ( database == null ){
            println("๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”");
            return;
        }

        String sql = "CREATE TABLE " + tableName + "(_id integer PRIMARY KEY autoincrement, name text, age integer, mobile text)";
        database.execSQL(sql);
        println("ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๋จ, ์ด๋ฆ„ : " + tableName);
    }

    public void insertRecord(){
        println("insertRecord() ํ˜ธ์ถœ");

        if ( database == null ){
            println("๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”");
            return;
        }

        String tableName = editText2.getText().toString();
        if (tableName == null){
            println("ํ…Œ์ด๋ธ” ์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ์„ธ์š”");
            return;
        }

        String sql = "INSERT INTO " + tableName + " (name, age, mobile) values ('์šฉ๋ฏธ๊ฒฝ', 30, '010-3923-9995')";
        database.execSQL(sql);
        println("๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€ํ•จ");
    }

    public void executeQuery(){
        println("executeQuery ํ˜ธ์ถœ๋จ");

        String tableName = editText2.getText().toString();
        if (tableName == null){
            println("ํ…Œ์ด๋ธ” ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์‹œ์˜ค");
        }
        String sql = "SELECT _id, name, age, mobile from " + tableName;
       Cursor cursor = database.rawQuery(sql, null);
       int recordCnt = cursor.getCount();
       println("๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜ : " + recordCnt);

       for (int i = 0; i < recordCnt; i++ ){
           cursor.moveToNext();

           int id = cursor.getInt(0); // ์ฒซ๋ฒˆ์จฐ ์ปฌ๋Ÿผ ๊ฐ’ (๊ณ ์œ  ๋ฒˆํ˜ธ๋กœ ์ง€์ •ํ•ด์คฌ์Œ)
           String name = cursor.getString(1);
           int age = cursor.getInt(2);
           String mobile = cursor.getString(3);

           println("๋ ˆ์ฝ”๋“œ #" + i + " : id=> " + id +" name=> " + name + " age=> "+ age +" mobile=> " +mobile);
       }

       cursor.close();// ํ•œ์ •๋œ ์ž์›์ด๋ผ ๋‹ซ์•„์ค˜์•ผ ํ•จ

    }

    public void println(String data){
        textView.append(data + "\n");
    }
}