public void insert(View target){
String mBirth = birth_year.getText().toString();
String mName = name.getText().toString();
String mHome = hometown.getText().toString();
Toast.makeText(getApplicationContext(), mBirth+mName+mHome ,
Toast.LENGTH_SHORT).show();
db.execSQL("INSERT INTO member VALUES (null, '"+ mBirth + "', '"
+ mName + "', '" + mHome + "');");
Toast.makeText(getApplicationContext(), "saved.",
Toast.LENGTH_SHORT).show();
birth_year.setText("");
name.setText("");
hometown.setText("");
}
public void search(View target){
Cursor cursor;
cursor = db.rawQuery("SELECT b_year, b_name, b_city FROM member;",null);
String mBirth="Year\n", mName="Name\n", mHome="City\n";
while(cursor.moveToNext()){
int gBirth = cursor.getInt(0);
String gName = cursor.getString(1);
String gHome = cursor.getString(2);
mBirth += (Integer.toString(gBirth)+'\n');
mName += (gName+'\n');
mHome += (gHome+'\n');
}
btext.setText(mBirth);
ntext.setText(mName);
htext .setText(mHome);
Toast.makeText(getApplicationContext(), mBirth+mName+mHome ,
Toast.LENGTH_SHORT).show();
}
我遇到的问题是数据库无法正常工作。
当我执行insert()
时,它不会在birth_year
Edit_Text
上写任何内容,我希望db.execSQL()
会抛出SQLiteException
。
但是不会抛出异常,当我执行search()
时,gBirth
为0
。 result
结果显示在链接中。我想在执行insert()
birth_year
时没有写任何内容时抛出异常。
我该如何解决?