이름을 입력하는 edittext의 값을 읽어오고,
성별을 선택하는 radiogroup 내의 radiobutton의 값을 읽어와서
그 두 값을 데이터베이스에 삽입하고 싶습니다..
데이터베이스를 열고, 테이블을 만드는 것까지는 되는데 값 넣는 곳에서 오류가 납니다ㅠㅠㅠㅠ
해결 방법 알려주시면 정말 정말 감사하겠습니다!
public class MainActivity extends AppCompatActivity {
Button m_btnOpenDB;
TextView m_txtLog;
EditText m_edtDbname;
String m_szDatabase;
SQLiteDatabase m_database;
EditText m_edtTableName;
String m_szTableName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_btnOpenDB = (Button)findViewById(R.id.btnOpenDB);
m_txtLog = (TextView)findViewById(R.id.txtLog);
m_edtDbname = (EditText)findViewById(R.id.edtDbname);
m_edtTableName = (EditText)findViewById(R.id.edtTableName);
}
public void onBtnDbOpenClicked(View v){
m_szDatabase = m_edtDbname.getText().toString();
try{
m_database = openOrCreateDatabase(m_szDatabase, Context.MODE_PRIVATE, null);
AppendLog(m_szDatabase + " is created.");
} catch (Exception e) {
}
}
private void AppendLog(String msg) {
m_txtLog.append(msg + "\n");
}
public void onBtnCreateTableClicked(View v){
m_szTableName = m_edtTableName.getText().toString();
try{
if (m_database != null) {
m_database.execSQL("CREATE TABLE " + m_szTableName + " ("
+ " _id integer PRIMARY KEY autoincrement, "
+ "name text, "
+ "gender text"
+ ");" );
AppendLog(m_szTableName + " is created.");
}
else {
AppendLog("Please, create Database first.");
}
}catch (Exception e) {
e.printStackTrace();
}
}
public void onBtnAppendDataClicked(View v){
EditText m_name = (EditText)findViewById(R.id.name);
RadioGroup rg_gender = (RadioGroup)findViewById(R.id.radiogroup);
RadioButton rb_male = (RadioButton)findViewById(R.id.male);
RadioButton rb_female = (RadioButton)findViewById(R.id.female);
String str_gender = "";
String sz_name = m_name.getText().toString();
if(m_szTableName == null)
m_szTableName = m_edtTableName.getText().toString();
if(rg_gender.getCheckedRadioButtonId() == R.id.male) {
str_gender = rb_male.getText().toString();
} else if (rg_gender.getCheckedRadioButtonId() == R.id.female) {
str_gender = rb_female.getText().toString();
}
try{
if(m_database != null){
m_database.execSQL("insert into " + m_szTableName
+ " values ('"+sz_name+"','"+str_gender+"');");
AppendLog("Record is appended.");
}
else {
AppendLog("Please, open the Database first.");
}
} catch(Exception e){
e.printStackTrace();
}
}