/data/data/<package_name/>/databases
ํด๋์ ํ์ผ๋ก ์ ์ฅ๋จ. (๋ค๋ฅธ ์ฑ๊ณผ ๊ณต์ ๋ถ๊ฐ) // ๋ง์ง๋ง ์ธ์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฒ์ ์ ๋ณด
class DBHelper(context: Context): SQLiteOpenHelper(context, "testdb", null, 1) {
// ...
}
val db: SQLiteDatabase = DBHelper(this).writableDatabase
package com.tutorial.c59
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class DBHelper(context: Context): SQLiteOpenHelper(context, "testdb", null, 1) {
override fun onCreate(db: SQLiteDatabase?) {
val studentSql = """
create table tb_member(
_id integer primary key autoincrement,
name not null,
email,
phone)
"""
db?.execSQL(studentSql)
db?.execSQL("insert into tb_member (name, email, phone) " +
"values ('haeun', 'jxlhe46@gmail.com', '01012345678')")
}
override fun onUpgrade(db: SQLiteDatabase?, p1: Int, p2: Int) {
// Drop the older tables
db?.execSQL("drop table tb_member")
// Create table again
onCreate(db)
}
}
package com.tutorial.c59
import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
val db: SQLiteDatabase = DBHelper(this).readableDatabase
val cursor = db.rawQuery("select name from tb_member", null)
if(cursor.moveToFirst()){
Toast.makeText(this, cursor.getString(0), Toast.LENGTH_SHORT).show()
}
db.close()
}
}
}
val db = openOrCreateDatabase("testdb", Context.MODE_PRIVATE, null)
public void execSQL(String sql, Object[] bindArgs)
db.execSQL("create table USER_TB(" +
"_id integer primary key autoincrement," +
"name not null," +
"phone")
db.execSQL("insert into USER_TB (name, phone) values (?,?)", arrayOf("kkang", "01012345678"))
public Cursor rawQuery(String sql, String[] selectionArgs)
val cursor = db.rawQuery("select * from USER_TB", null)
while(cursor.moveToNext()){ // ๋ค์ ํ์ผ๋ก ์ด๋
val name = cursor.getString(0) // ์ด์ ์ธ๋ฑ์ค ์ง์
val phone = cursor.getString(1)
}
package com.tutorial.c60
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class DBHelper(context: Context): SQLiteOpenHelper(context, "memodb", null, 1) {
override fun onCreate(db: SQLiteDatabase?) {
val memoSQL = """
create table tb_memo(
_id integer primary key autoincrement,
title,
content)
"""
db?.execSQL(memoSQL)
}
override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
TODO("Not yet implemented")
}
}
package com.tutorial.c60
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val titleView = findViewById<EditText>(R.id.add_title)
val contentView = findViewById<EditText>(R.id.add_content)
val addBtn = findViewById<Button>(R.id.add_btn)
addBtn.setOnClickListener {
val title = titleView.text.toString()
val content = contentView.text.toString()
val db = DBHelper(this).writableDatabase
db.execSQL("insert into tb_memo (title, content) values (?, ?)",
arrayOf(title, content))
db.close()
val intent = Intent(this, ReadActivity::class.java)
startActivity(intent)
}
}
}
package com.tutorial.c60
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class ReadActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
val titleView = findViewById<TextView>(R.id.read_title)
val contentView = findViewById<TextView>(R.id.read_content)
val db = DBHelper(this).readableDatabase
// ์ต์ ์
๋ ฅ ๋ฐ์ดํฐ 1๊ฑด์ ์ป์ด์ ํ
์คํธ๋ทฐ์ ์ถ๋ ฅํ๊ธฐ
val cursor = db.rawQuery("select title, content from tb_memo " +
"order by _id desc limit 1", null)
while(cursor.moveToNext()){
titleView.text = cursor.getString(0)
contentView.text = cursor.getString(1)
}
db.close()
}
}
val values = ContentValues()
values.put("name", "kkang")
values.put("phone", "01012345678")
db.insert("USER_TB", null, values) // ์นผ๋ผ ๋ฐ์ดํฐ ์ฝ์
execSQL() ๋์ insert() ์ฌ์ฉํ๊ธฐ
package com.tutorial.c60
import android.content.ContentValues
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val titleView = findViewById<EditText>(R.id.add_title)
val contentView = findViewById<EditText>(R.id.add_content)
val addBtn = findViewById<Button>(R.id.add_btn)
addBtn.setOnClickListener {
val title = titleView.text.toString()
val content = contentView.text.toString()
val db = DBHelper(this).writableDatabase
// db.execSQL("insert into tb_memo (title, content) values (?, ?)",
// arrayOf(title, content))
val values = ContentValues()
values.put("title", title)
values.put("content", content)
db.insert("tb_memo", null, values)
db.close()
val intent = Intent(this, ReadActivity::class.java)
startActivity(intent)
}
}
}
rawQuery() ๋์ query() ์ฌ์ฉํ๊ธฐ
package com.tutorial.c60
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class ReadActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
val titleView = findViewById<TextView>(R.id.read_title)
val contentView = findViewById<TextView>(R.id.read_content)
val db = DBHelper(this).readableDatabase
// ์ต์ ์
๋ ฅ ๋ฐ์ดํฐ 1๊ฑด์ ์ป์ด์ ํ
์คํธ๋ทฐ์ ์ถ๋ ฅํ๊ธฐ
// val cursor = db.rawQuery("select title, content from tb_memo " +
// "order by _id desc limit 1", null)
val cursor = db.query("tb_memo", arrayOf("title, content"),
null, null, null, null,
"_id desc limit 1")
while(cursor.moveToNext()){
titleView.text = cursor.getString(0)
contentView.text = cursor.getString(1)
}
db.close()
}
}