为什么这个代码运行之后数据库中没有插入数据呢,怎么修改一下,谢谢了
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SQLiteDatabase database = dbHelper.getReadableDatabase();
String username = mEditText.getText().toString();
Cursor cursor = database.rawQuery("select * from user;", null);
String muser;
cursor.moveToFirst();
while(!cursor.isAfterLast()){
muser = cursor.getString(cursor.getColumnIndex("username"));
if (muser.equals(username)||muser==null) {
Toast.makeText(mContext, "用户名已存在或无效,换一个",Toast.LENGTH_SHORT).show();
database.close();
check = true;
break;
} else {
check = false;
register();
break;
}
}
}});}
public void register(){
String pass1 = mEditText2.getText().toString();
String pass2 = mEditText3.getText().toString();
String username = mEditText.getText().toString();
String password;
SQLiteDatabase database2 = dbHelper.getWritableDatabase();
if (pass1!=null&&pass1.equals(pass2)&&check==false) {
password = pass1;
database2.beginTransaction();
ContentValues values = new ContentValues();
values.put("username", username);
Log.i("11", username);
values.put("password", password);
Log.i("", password);
database2.insert("user", null, values);
database2.setTransactionSuccessful();
database2.endTransaction();
database2.close();
Toast.makeText(mContext, "注册成功", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(mContext, "两次密码不一致", Toast.LENGTH_LONG).show();
}
解决方案:20分