๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
APP/ANDROID

[๋ถ€์ŠคํŠธ์ฝ”์Šค] ํ—ฌํผ ์‚ฌ์šฉํ•˜๊ธฐ

by mingzoo 2020. 3. 11.

ํ—ฌํผ ํด๋ž˜์Šค

ํ—ฌํผํด๋ž˜์Šค๋Š” ๊ธฐ์กด์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์žˆ๋Š”์ง€, ์žˆ๋‹ค๋ฉด ๋ฒ„์ „์€ ๋ฌด์—‡์ด๊ณ  ์—…๊ทธ๋ ˆ์ด๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•  ๊ฒƒ์ธ์ง€ ๋“ฑ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊พธ๋Š”๋ฐ ์“ฐ์ผ ์ˆ˜ ์žˆ๋‹ค.

์˜คํ”ˆ ํ—ฌํผ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์•ˆ์˜ ํ…Œ์ด๋ธ”์„ ์‚ญ์ œํ•˜๋ฉด ๊ธฐ์กด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚ ์•„๊ฐ€๊ธฐ ๋•Œ๋ฌธ์—, ์›๋ž˜ ์‚ฌ์šฉ์ž๋Š” ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์ฃผ๊ณ , ์‹ ๊ทœ ์‚ฌ์šฉ์ž๋Š” ์ƒˆ๋กœ ์‹ ๊ทœ๋กœ ๋งŒ๋“ค์–ด์ค˜์•ผํ•œ๋‹ค. ์ด๋ฅผ ์ง€์›ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์˜คํ”ˆํ—ฌํผ์ด๋‹ค.

์˜คํ”ˆํ—ฌํผ๋Š” ์œ„์˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ์ƒ์†์„ ํ•ด์„œ ๋งŒ๋“ค์–ด์ฃผ๋ฉด ๋œ๋‹ค.

์œ„์˜ onCreate(), onOpen(), onUpgrade()์˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ

onCreate() : ์–ด๋–ค ์‚ฌ์šฉ์žํ•œํ…Œ ์ด ์•ฑ ๋ฐฐํฌ ํ›„, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์˜คํ”ˆํ–ˆ์„ ๋•Œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์—†์„ ๊ฒฝ์šฐ 

onOpen() : ์ด๋ฏธ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ

onUpgrade() : ๋ฒ„์ „๊ณผ ์ƒ๊ด€์—†์ด ๊ทธ๋ƒฅ ์˜คํ”ˆํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ, ๋ฒ„์ „์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์„ ๊ฐ€์ง

//MainActivity.java
public class MainActivity extends AppCompatActivity {

    EditText editText;
    EditText editText2;
    EditText editText3;
    EditText editText4;
    EditText editText5;

    TextView textView;

    SQLiteDatabase database;

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

        editText = (EditText) findViewById(R.id.editText);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText3 = (EditText) findViewById(R.id.editText3);
        editText4 = (EditText) findViewById(R.id.editText4);
        editText5 = (EditText) findViewById(R.id.editText5);

        textView = (TextView) findViewById(R.id.textView2);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String databaseName = editText.getText().toString();
                openDatabase(databaseName);
            }
        });

        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String tableName = editText2.getText().toString();
                createTable(tableName);
            }
        });

        Button button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String name = editText3.getText().toString().trim();
                String ageStr = editText4.getText().toString().trim();
                String mobile = editText5.getText().toString().trim();

                int age = -1;
                try{
                    age =  Integer.parseInt(ageStr);
                }catch (Exception e){
                    e.printStackTrace();
                }
                insertData(name, age, mobile);
            }
        });

        Button button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String tableName = editText2.getText().toString();
                selectData(tableName);
            }
        });
    }

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

        /*
        database = openOrCreateDatabase(databaseName, MODE_PRIVATE, null);

        if(database != null){
            println("๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์˜คํ”ˆ๋จ");
        }*/

        DatabaseHelper helper = new DatabaseHelper(this, databaseName, null, 3);
        database = helper.getWritableDatabase();
    }


    public void createTable(String tableName){
        println("createTable() ํ˜ธ์ถœ๋จ");
//
        if(database != null){
//            String sql = "create table " + tableName + "(_id integer PRIMARY KEY autoincrement, name text, age integer, mobile text)";
//
//            //๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ›์ง€ ์•Š์•„๋„ ๋˜๋Š” SQL ๋ฌธ์ด ์žˆ์œผ๋ฉด ์‹คํ–‰
//            database.execSQL(sql);
//
//            println("ํ…Œ์ด๋ธ” ์ƒ์„ฑ๋จ");
        }else{
            println("๋จผ์ € ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์˜คํ”ˆํ•˜์„ธ์š”");
        }
    }

    public void insertData(String name, int age, String mobile){
        println("insertData()");
        if(database != null){
            String sql = "insert into customer(name, age, mobile) values(?,?,?)";
            Object[] params = {name, age, mobile};

            database.execSQL(sql, params);

            println("๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ•จํ•จ");
       }else{
            println("๋จผ์ € ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์˜คํ”ˆํ•˜์„ธ์š”");
        }

    }

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

        if(database != null){
            // ๋ฐ์ดํ„ฐ ์กฐํšŒ
            String sql = "select name, age, mobile from " + tableName;

            //rawQuery ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ์ฒด๋Š” Cursor
            //Cursor ๊ฐ์ฒด๋Š” ๊ฐ๊ฐ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ moveToNext ๋ฉ”์†Œ๋“œ๋กœ ๋„˜๊ฒจ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ํ•จ
            Cursor cursor = database.rawQuery(sql, null);
            println("์กฐํšŒ๋œ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜: " + cursor.getCount());

            for(int i = 0; i < cursor.getCount(); i++){
                cursor.moveToNext();
                String name = cursor.getString(0);
                int age = cursor.getInt(1);
                String mobile = cursor.getString(2);

                println("#" + i + " -> " + name + ", " + age + ", " + mobile);
            }

            cursor.close();
        }
    }

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

    class DatabaseHelper extends SQLiteOpenHelper{
        //์ƒ์„ฑ์ž
        public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            println("onCreate() ํ˜ธ์ถœ๋จ");
            String tableName = "customer";
            // ํ…Œ์ด๋ธ”์ด ์—†์œผ๋ฉด ์ƒ์„ฑํ•ด์ฃผ๋Š” ์ฝ”๋“œ
            String sql = "create table if not exists " + tableName + "(_id integer PRIMARY KEY autoincrement, name text, age integer, mobile text)";

            //๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ›์ง€ ์•Š์•„๋„ ๋˜๋Š” SQL ๋ฌธ์ด ์žˆ์œผ๋ฉด ์‹คํ–‰
            db.execSQL(sql);

            println("ํ…Œ์ด๋ธ” ์ƒ์„ฑ๋จ");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            println("onUpgrade ํ˜ธ์ถœ๋จ: " + oldVersion + ", " + newVersion);

            if(newVersion > 1){
                String tableName = "customer";
                db.execSQL("drop table if exists "+ tableName);
                println("ํ…Œ์ด๋ธ” ์‚ญ์ œํ•จ");

                String sql = "create table if not exists " + tableName + "(_id integer PRIMARY KEY autoincrement, name text, age integer, mobile text)";
                db.execSQL(sql);
                println("ํ…Œ์ด๋ธ” ์ƒˆ๋กœ ์ƒ์„ฑ๋จ");
            }
        }
    }
}

์œ„ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์˜คํ”ˆ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ , ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ…Œ์ด๋ธ”์ด ์‚ญ์ œ๋˜๊ณ  ์ƒˆ ๋ฒ„์ „์˜ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

 

728x90