2swan

SQLite 예제2 본문

Programming/Android Example

SQLite 예제2

2swan 2023. 8. 8. 16:50

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/edDB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="DB이름"/>

        <Button
            android:id="@+id/btnDB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="데이터베이스 만들기"/>

    </LinearLayout>



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <EditText
                android:id="@+id/edTable"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="Table이름"/>

            <Button
                android:id="@+id/btnTable"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="테이블 만들기"/>

        </LinearLayout>

    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">.

            <Button
                android:id="@+id/btnInsert"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_margin="3dp"
                android:text="데이터추가"/>

            <Button
                android:id="@+id/btnSelect"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_margin="3dp"
                android:text="데이터조회"/>
        </LinearLayout>


            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="TextViewTextViewTextView"/>
                </LinearLayout>
            </ScrollView>

</LinearLayout>

 

main.activity

public class MainActivity extends AppCompatActivity {
    SQLiteDatabase sqLiteDatabase;
    TextView textView;

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

        EditText edDB = findViewById(R.id.edDB);
        EditText edTable = findViewById(R.id.edTable);
        textView = findViewById(R.id.textView);


        Button btnDB = findViewById(R.id.btnDB);
        Button btnTable = findViewById(R.id.btnTable);
        Button btnInsert = findViewById(R.id.btnInsert);
        Button btnSelect = findViewById(R.id.btnSelect);

        //데이터베이스 생성
        btnDB.setOnClickListener(new View.OnClickListener() {
                                     @Override
                                     public void onClick(View view) {
                                         textView.setText("");
                                         sqLiteDatabase = openOrCreateDatabase(edDB.getText().toString(),
                                                 MODE_PRIVATE, null);

                                         output("데이터 베이스생성 :: " + edDB.getText().toString());
                                     }
                                 });

        //테이블 생성
        btnTable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            String tableName = edTable.getText().toString();
            if(sqLiteDatabase == null) {
                output("테이터베이스를 생성하세요");
                return;
            }
            String sql = "create table if not exists " +tableName+"(" +
                    " id integer primary key autoincrement, "+
                    " name text, "+
                    " age integer, "+
                    " phone text)";
                    sqLiteDatabase.execSQL(sql);
                    output("테이블 생성 : "+tableName);
            }
        });

        //데이터 추가
        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText("");
                String tableName = edTable.getText().toString();
                if(sqLiteDatabase == null){
                    output("데이터베이스를 생성하세요: "+tableName);
                    return;
                }
                if(tableName == null){
                    output("테이블 생성 : "+tableName);
                    return;
                }
                output("btnInsert 호출");
                String sql = "insert into "+ tableName
                        +"(name, age, phone) "+
                        " values('안드로이드', 11, '010-1111-2222')";
                sqLiteDatabase.execSQL(sql);
                output("안드로이드 추가");

            }
        });

        //조회
        btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText("");
                String tableName = edTable.getText().toString();
                if(sqLiteDatabase == null){
                    output("데이터베이스를 생성하세요: "+tableName);
                    return;
                }
                if(tableName == null){
                    output("테이블 생성 : "+tableName);
                    return;
                }
                output("btnSelect 호출");
                String sql = "select * from " + tableName;
                Cursor cursor = sqLiteDatabase.rawQuery(sql,null);
                while (cursor.moveToNext()){
                           int id = cursor.getInt(0);
                           String name = cursor.getString(1);
                           int age = cursor.getInt(2);
                           String phone = cursor.getString(3);
                           output(id + " // " + name + " // "  + age + " // "  + phone );
                }
            }
        });


     }
            private void output(String str) {
                textView.append(str+"\n");
            }

        }

결과 값

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

RecyclerView 목록 만들기  (0) 2023.08.09
Fragment 예제  (0) 2023.08.08
그리드 뷰  (0) 2023.08.04
리스트뷰 동적 추가  (0) 2023.08.04
리스트 뷰 예제  (0) 2023.08.04