0

In my application, I tried using the following codes to validate the user email address exists in database or not. If the user email address does not exist it will insert the user info into database. But this message appeared and I cant get the user info into the database. I not really sure what the issue here.

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;

}

Logcat

01-29 10:03:12.007: D/Cursor(2143): Database path: /data/data/com.ff.fbin/databases/wbdb.db
01-29 10:03:12.007: D/Cursor(2143): Table name   : 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): Uncaught exception thrown by finalizer (will be discarded):
01-29 10:03:12.007: I/dalvikvm(2143): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@405b07e8 on null that has not been deactivated or closed
01-29 10:03:12.011: I/dalvikvm(2143):   at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:620)
01-29 10:03:12.011: I/dalvikvm(2143):   at dalvik.system.NativeStart.run(Native Method)
4

2 に答える 2

0
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);
try
{
    if(c.getCount()==0){  
        //not in db
       Log.i(TAG, "getCount = 0");
       c.close();   <-------- Close the cursor here.
       return true;
     }
 }
 finally
 {
     c.close();
     if (helper!=null){
        helper.close();
       }
   }
 } 
于 2013-02-01T08:45:36.387 に答える
0
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");
       c.close();
       return true;
   }
   if (c != null && !c.isClosed()) {
       c.close();
   }
   if (helper!=null){
       helper.close();
   }

   return false;

}

c.close();前に含めることで解決した問題return true;

于 2013-01-29T03:41:30.930 に答える