ํฌํผ ํด๋์ค
ํฌํผํด๋์ค๋ ๊ธฐ์กด์ ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ์๋์ง, ์๋ค๋ฉด ๋ฒ์ ์ ๋ฌด์์ด๊ณ ์ ๊ทธ๋ ์ด๋๋ฅผ ์ด๋ป๊ฒ ํ ๊ฒ์ธ์ง ๋ฑ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ ์ด๋ธ ๊ตฌ์กฐ๋ฅผ ๋ฐ๊พธ๋๋ฐ ์ฐ์ผ ์ ์๋ค.
์คํ ํฌํผ
๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ ํ ์ด๋ธ์ ์ญ์ ํ๋ฉด ๊ธฐ์กด์ ๋ฐ์ดํฐ๊ฐ ๋ ์๊ฐ๊ธฐ ๋๋ฌธ์, ์๋ ์ฌ์ฉ์๋ ์ ๋ฐ์ดํธ๋ฅผ ํด์ฃผ๊ณ , ์ ๊ท ์ฌ์ฉ์๋ ์๋ก ์ ๊ท๋ก ๋ง๋ค์ด์ค์ผํ๋ค. ์ด๋ฅผ ์ง์ํด์ฃผ๋ ๊ฒ์ด ์คํํฌํผ์ด๋ค.
์คํํฌํผ๋ ์์ ์ฌ์ง๊ณผ ๊ฐ์ด ์์์ ํด์ ๋ง๋ค์ด์ฃผ๋ฉด ๋๋ค.
์์ 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("ํ
์ด๋ธ ์๋ก ์์ฑ๋จ");
}
}
}
}
์ ์ฝ๋์ ๊ฒฐ๊ณผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํ ๋ฒํผ์ ๋๋ฅด๊ณ , ํ ์ด๋ธ ๋ง๋ค๊ธฐ ๋ฒํผ์ ๋๋ฅด๋ฉด ํ ์ด๋ธ์ด ์ญ์ ๋๊ณ ์ ๋ฒ์ ์ ํ ์ด๋ธ์ด ์์ฑ๋๊ฒ ๋ง๋ค์๋ค.
'APP > ANDROID' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ถ์คํธ์ฝ์ค] ์๋๋ก์ด๋ ์นด๋ฉ๋ผ ์ฐ์ด์ ๋ํ๋ด๊ธฐ (0) | 2020.03.20 |
---|---|
[๋ถ์คํธ์ฝ์ค] ์ธํฐ๋ท ์ฐ๊ฒฐ์ํ ํ์ธ (0) | 2020.03.11 |
[๋ถ์คํธ์ฝ์ค] SQL์ ์ด์ฉํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ์ฉ ์์ (0) | 2020.03.11 |
[๋ถ์คํธ์ฝ์ค] ๋คํธ์ํน/HTTP (0) | 2020.03.05 |
[๋ถ์คํธ์ฝ์ค] ์ค๋ ๋(Thread) (0) | 2020.03.04 |