在我的应用程序中,我尝试使用以下代码来验证用户电子邮件地址是否存在于数据库中。如果用户电子邮件地址不存在,它会将用户信息插入数据库。但是出现了这条消息,我无法将用户信息输入数据库。我不确定这里有什么问题。
MainActivity.java
public void onCreate(){
helper = new DBHelper(this);
Button loginButton = (Button) findViewById(R.id.login_btn);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if(useremail.length() == 0){
alertMessage = "Please enter Email Address.";
dialogBox();
}
else if (password.length() == 0){
alertMessage = "Please enter Password.";
dialogBox();
}
else {
emailid = useremail.getText().toString();
userpassword = password.getText().toString();
if(!helper.emailidChecking(emailid)){
helper.insert_wbm_user(emailid);
}
else{
alertMessage="User email exists";
dialogBox();
}
}
}
});
}
public void onDestroy(){
super.onDestroy();
helper.close();
}
DBHelper.java
public boolean emailidChecking(String emailid) throws SQLException
{
helper = this.getReadableDatabase();
Log.i(TAG, "emailidChecking:" +emailid);
Cursor c = helper.rawQuery("SELECT * from user_info where emailid='"+emailid+"'", null);
if(c.getCount()==0){
//not in db
Log.i(TAG, "getCount = 0");
return true;
}
if (c != null && !c.isClosed()) {
c.close();
}
if (helper!=null){
helper.close();
}
return false;
}
public long insert_user_info(String emailid) {
ContentValues cv=new ContentValues();
cv=new ContentValues();
cv.put("emailid", emailid);
Log.i(TAG, "insert db");
long createdinMsg = getWritableDatabase().insert("user_info", "emailid", cv);
return createdinMsg;
}
日志猫
01-29 10:03:12.007:D/Cursor(2143):数据库路径:/data/data/com.ff.fbin/databases/wbdb.db 01-29 10:03:12.007: D/Cursor(2143): 表名: null 01-29 10:03:12.007: D/Cursor(2143): SQL: SQLiteQuery: SELECT * from user_info where emailid='asd@hotmail.com' 01-29 10:03:12.007: I/dalvikvm(2143): 终结器抛出未捕获的异常(将被丢弃): 01-29 10:03:12.007: I/dalvikvm(2143): Ljava/lang/IllegalStateException;: 在尚未停用或关闭的 null 上完成游标 android.database.sqlite.SQLiteCursor@405b07e8 01-29 10:03:12.011: I/dalvikvm(2143): 在 android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:620) 01-29 10:03:12.011: I/dalvikvm(2143): 在 dalvik.system.NativeStart.run(Native Method)