1 |
package soo.data.db; |
---|
2 |
|
---|
3 |
import android.app.Activity; |
---|
4 |
import android.content.Context; |
---|
5 |
import android.content.Intent; |
---|
6 |
import android.database.SQLException; |
---|
7 |
import android.database.sqlite.SQLiteDatabase; |
---|
8 |
import android.os.Bundle; |
---|
9 |
import android.util.Log; |
---|
10 |
import android.view.View; |
---|
11 |
import android.view.View.OnClickListener; |
---|
12 |
import android.widget.Button; |
---|
13 |
import android.widget.EditText; |
---|
14 |
import android.widget.Toast; |
---|
15 |
|
---|
16 |
public class DBTest extends Activity |
---|
17 |
implements OnClickListener |
---|
18 |
{ |
---|
19 |
/** Called when the activity is first created. */ |
---|
20 |
private Button createB,dropB,insertB,updateB,deleteB,selectB; |
---|
21 |
private EditText et1,et2,et3; |
---|
22 |
private SQLiteDatabase db; |
---|
23 |
|
---|
24 |
@Override |
---|
25 |
public void onCreate(Bundle savedInstanceState) { |
---|
26 |
super.onCreate(savedInstanceState); |
---|
27 |
setContentView(R.layout.main); |
---|
28 |
|
---|
29 |
createB = (Button)findViewById(R.id.button01); |
---|
30 |
dropB = (Button)findViewById(R.id.button02); |
---|
31 |
insertB = (Button)findViewById(R.id.button03); |
---|
32 |
updateB = (Button)findViewById(R.id.button04); |
---|
33 |
deleteB = (Button)findViewById(R.id.button05); |
---|
34 |
selectB = (Button)findViewById(R.id.button06); |
---|
35 |
|
---|
36 |
createB.setOnClickListener(this); |
---|
37 |
dropB .setOnClickListener(this); |
---|
38 |
insertB.setOnClickListener(this); |
---|
39 |
updateB.setOnClickListener(this); |
---|
40 |
deleteB.setOnClickListener(this); |
---|
41 |
selectB.setOnClickListener(this); |
---|
42 |
|
---|
43 |
et1 = (EditText)findViewById(R.id.editText01); |
---|
44 |
et2 = (EditText)findViewById(R.id.editText02); |
---|
45 |
et3 = (EditText)findViewById(R.id.editText03); |
---|
46 |
|
---|
47 |
openDb(); |
---|
48 |
} |
---|
49 |
|
---|
50 |
private final static String DB_NAME="test.db"; |
---|
51 |
private final static int DB_MODE = Context.MODE_PRIVATE; |
---|
52 |
private final static String TABLE_NAME="ADDRESS"; |
---|
53 |
private void openDb(){ |
---|
54 |
db = openOrCreateDatabase(DB_NAME, DB_MODE, null); |
---|
55 |
showToast("DB("+DB_NAME+") ��� 諛��대┝"); |
---|
56 |
|
---|
57 |
//createTable(); |
---|
58 |
} |
---|
59 |
|
---|
60 |
private void createTable(){ |
---|
61 |
String sql = "create table "+TABLE_NAME + |
---|
62 |
"(SEQ integer primary key autoincrement," + |
---|
63 |
" NAME text not null,ADDR,DDATE date)"; |
---|
64 |
|
---|
65 |
try{ |
---|
66 |
db.execSQL(sql); |
---|
67 |
showToast("���釉�"+TABLE_NAME+") ��� ���"); |
---|
68 |
}catch(SQLException se){ |
---|
69 |
Log.e("createTable",se.toString()); |
---|
70 |
} |
---|
71 |
} |
---|
72 |
|
---|
73 |
private void dropTable(){ |
---|
74 |
String sql = "drop table "+TABLE_NAME; |
---|
75 |
|
---|
76 |
try{ |
---|
77 |
db.execSQL(sql); |
---|
78 |
showToast("���釉�"+TABLE_NAME+") ��� ���"); |
---|
79 |
}catch(SQLException se){ |
---|
80 |
Log.e("dropTable",se.toString()); |
---|
81 |
} |
---|
82 |
} |
---|
83 |
|
---|
84 |
private void insertData(String name,String addr){ |
---|
85 |
String sql = "insert into "+TABLE_NAME+"(NAME,ADDR,DDATE) " |
---|
86 |
+ "values ('"+name+"','"+addr+"',date('now'))"; |
---|
87 |
try{ |
---|
88 |
db.execSQL(sql); |
---|
89 |
showToast("���(insert) ���"); |
---|
90 |
}catch(SQLException se){ |
---|
91 |
Log.e("insertData",se.toString()); |
---|
92 |
} |
---|
93 |
|
---|
94 |
} |
---|
95 |
|
---|
96 |
private void updateData(String name,String addr){ |
---|
97 |
String sql = "update "+TABLE_NAME+" " |
---|
98 |
+ " set addr='"+addr+"'," |
---|
99 |
+ " ddate=date('now') " |
---|
100 |
+ " where name ='"+name+"'"; |
---|
101 |
try{ |
---|
102 |
db.execSQL(sql); |
---|
103 |
showToast("���(update) ���"); |
---|
104 |
}catch(SQLException se){ |
---|
105 |
Log.e("updateData",se.toString()); |
---|
106 |
} |
---|
107 |
|
---|
108 |
} |
---|
109 |
|
---|
110 |
private void deleteData(String name,String addr){ |
---|
111 |
String sql = "delete from "+TABLE_NAME+" " |
---|
112 |
+ " where name ='"+name+"'"; |
---|
113 |
try{ |
---|
114 |
db.execSQL(sql); |
---|
115 |
showToast("���(delete) ���"); |
---|
116 |
}catch(SQLException se){ |
---|
117 |
Log.e("deleteData",se.toString()); |
---|
118 |
} |
---|
119 |
|
---|
120 |
} |
---|
121 |
|
---|
122 |
private void showToast(String msg){ |
---|
123 |
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show(); |
---|
124 |
//Toast.makeText(AlertDialog.this, "���ル�?!!", Toast.LENGTH_SHORT).show(); |
---|
125 |
} |
---|
126 |
|
---|
127 |
|
---|
128 |
@Override |
---|
129 |
public void onClick(View v) { |
---|
130 |
// TODO Auto-generated method stub |
---|
131 |
if(v==createB){ |
---|
132 |
createTable(); |
---|
133 |
}else if(v==dropB){ |
---|
134 |
dropTable(); |
---|
135 |
}else if(v==insertB){ |
---|
136 |
String name = et2.getText().toString(); |
---|
137 |
if(name !=null) name=name.trim(); |
---|
138 |
if(name.equals("")){ |
---|
139 |
showToast("�대���諛��������댁� �⑸���"); |
---|
140 |
return; |
---|
141 |
} |
---|
142 |
String addr = et3.getText().toString(); |
---|
143 |
insertData(name,addr); |
---|
144 |
}else if(v==updateB){ |
---|
145 |
String name = et2.getText().toString(); |
---|
146 |
if(name.equals("")){ |
---|
147 |
showToast("�대���諛��������댁� �⑸���"); |
---|
148 |
return; |
---|
149 |
} |
---|
150 |
String addr = et3.getText().toString(); |
---|
151 |
updateData(name,addr); |
---|
152 |
}else if(v==deleteB){ |
---|
153 |
String name = et2.getText().toString(); |
---|
154 |
if(name.equals("")){ |
---|
155 |
showToast("�대���諛��������댁� �⑸���"); |
---|
156 |
return; |
---|
157 |
} |
---|
158 |
String addr = et3.getText().toString(); |
---|
159 |
deleteData(name,addr); |
---|
160 |
}else if(v==selectB){ |
---|
161 |
Intent i = new Intent(this,DBList.class); |
---|
162 |
startActivityForResult(i,1 ); //���瑜�二쇰� 寃���� 1����껌肄��(requestCode) �대�.. |
---|
163 |
} |
---|
164 |
} |
---|
165 |
|
---|
166 |
|
---|
167 |
} |
---|