Skip to content

create table in kotlin

  • sqlite DB연동
  • db값을 table에 집어넣기
 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
// MainActivitiy.kt
class MainActivity : AppCompatActivity() {
    lateinit var dbHelper : DBHelper
    lateinit var database : SQLiteDatabase
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dbHelper = DBHelper(this, "newdb.db", null, 1)
        database = dbHelper.writableDatabase

//        insert values into database
        var contentValues = ContentValues()
        contentValues.put("txt","Bulgogi")

        database.insert("mytable",null,contentValues)

//        select from db and print something
        var c : Cursor = database.query("mytable",null,null,null,null,null,null)
//        while(c.moveToNext()){
//            System.out.println("txt : "+c.getString(c.getColumnIndex("txt")));
//        }

        setContentView(R.layout.activity_main)
        var stringAtext: TextView? = findViewById<TextView?>(R.id.stringA)
        c.moveToNext()
        stringAtext?.text = c.getString(c.getColumnIndex("txt"))
        stringAtext?.setBackgroundColor(Color.parseColor("#FA8293"))


    }
}
 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!-- activity_main.xml -->
<?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">

    <TableLayout
        android:id="@+id/aaba"
        android:stretchColumns="*"
        android:layout_width="313dp"
        android:layout_height="234dp"
        tools:layout_editor_absoluteX="49dp"
        tools:layout_editor_absoluteY="56dp">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/stringA"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="a"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="b"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>