数据库出错

时间:2013-05-17 01:38:17

标签: android sqlite

请帮助我在我的代码中找到问题。我正在尝试检查用户是否输入了有效的电子邮件地址以及是否已存在。如果用户通过某种格式输入了无效的电子邮件地址,我已经陷阱了,我已经检查过电子邮件是否存在,而且问题是当用户输入新的有效电子邮件地址时,我的应用程序崩溃了。这是我的代码:

else if (!Email.equals(""))
{
    if(Email.equals(storedEmail))   //Check if the email address already exist
       {
           Toast.makeText(getApplicationContext(), "Email address already exist.",Toast.LENGTH_LONG).show();
       }


    else if (!matcherObj.matches()) //it will check if the user enter valid email
      {
        Toast.makeText(getApplicationContext(), "Invalid email address.",Toast.LENGTH_LONG).show();
        txtEmail.setText("");
      }
    else // if the email address is not existing and is valid it will save to the database
      {
        db.Register(newPatientInfo(Fname,Mname,Lname,Suffix,Birthday,Homeno,MobileNo,Email,Brgy,Province,CityMun,Zip,CFname,CLname,DClinic,DClinicAdd));
        Toast.makeText(getApplicationContext(),"Saved!", Toast.LENGTH_LONG).show();
        Clear();
      }

}

这是我在databasehandler中的代码

public String Patient_Emailexist(String p_email)                
{               
  SQLiteDatabase db = this.getReadableDatabase();               
  Cursor c = db.query(TABLE_PATIENT, null, PATIENT_EMAIL + "=?", new String[]{String.valueOf(p_email)},null, null, null);                                               
    if (c == null)              
      {                        
        c.moveToFirst();                                    
      }                 
         c.moveToFirst();               
         String patient_email = c.getString(c.getColumnIndex(PATIENT_EMAIL));               
         return patient_email;  
}

每当我输入应该保存在数据库中的有效新电子邮件地址时,我的应用程序就会崩溃。提前谢谢!

发现错误

05-17 10:27:17.410:E / AndroidRuntime(678):致命异常:主05-17 10:27:17.410:E / AndroidRuntime(678):android.database.CursorIndexOutOfBoundsException:请求索引0,大小0 05-17 10:27:17.410:E / AndroidRuntime(678):在android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)05-17 10:27:17.410:E / AndroidRuntime(678) :at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)05-17 10:27:17.410:E / AndroidRuntime(678):at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)05-17 10:27:17.410:E / AndroidRuntime(678):at com.example.palsproject.DatabaseHandler.Patient_Emailexist(DatabaseHandler.java:353)05-17 10:27:17.410:E / AndroidRuntime(678):at com.example .palsproject.PatientRegistration $ 1.onClick(PatientRegistration.java:95)05-17 10:27:17.410:E / AndroidRuntime(678):at android.view.View.performClick(View.java:4084)05-17 10: 27:17.410:E / AndroidRuntime(678):在androi d.view.View $ PerformClick.run(View.java:16966)05-17 10:27:17.410:E / AndroidRuntime(678):at android.os.Handler.handleCallback(Handler.java:615)05-17 10:27:17.410:E / AndroidRuntime(678):在android.os.Handler.dispatchMessage(Handler.java:92)05-17 10:27:17.410:E / AndroidRuntime(678):在android.os.Looper .loop(Looper.java:137)05-17 10:27:17.410:E / AndroidRuntime(678):在android.app.ActivityThread.main(ActivityThread.java:4745)05-17 10:27:17.410:E / AndroidRuntime(678):at java.lang.reflect.Method.invokeNative(Native Method)05-17 10:27:17.410:E / AndroidRuntime(678):at java.lang.reflect.Method.invoke(Method.java :511)05-17 10:27:17.410:E / AndroidRuntime(678):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)05-17 10:27:17.410:E / AndroidRuntime(678):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)05-17 10:27:17.410:E / AndroidRuntime(678):at dalvik.system.NativeStart.main(原生方法)

2 个答案:

答案 0 :(得分:1)

问题在于:

Cursor c = db.query(TABLE_PATIENT, null, PATIENT_EMAIL + "=?", new String[]{String.valueOf(p_email)},null, null, null);                                               
if (c == null)              
{                        
    c.moveToFirst();                                    
}                 
c.moveToFirst();               
String patient_email = c.getString(c.getColumnIndex(PATIENT_EMAIL));               

首先,如果c等于null,c.moveToFirst()将会崩溃,但这是一个单独的问题。

接下来,online help for getString()说:

  

结果以及当列值为null或列类型不是字符串类型时此方法是否抛出异常是实现定义的。

正如您所说,记录不在数据库中,因此列值将为null。你可能想要:

String patient_email = null;
if (!c.isNull(c.getColumnIndex(PATIENT_EMAIL)) && c.getType (c.getColumnIndex(PATIENT_EMAIL)) == FIELD_TYPE_STRING)
    patient_email = c.getString(c.getColumnIndex(PATIENT_EMAIL));               

答案 1 :(得分:0)

你正在做的是你正在检查游标是否为null,即使它是null也会在if之后的语句被执行,因此对于getstring它将为null forofbound。我修改了你的代码

public String Patient_Emailexist(String p_email)                
{        
     String patient_email="";
     SQLiteDatabase db = this.getReadableDatabase();               
     Cursor c = db.query(TABLE_PATIENT, null, PATIENT_EMAIL + "=?", new String[]{String.valueOf(p_email)},null, null, null);                                               
if (c == null)              
  {                        
    return patient_email;                                   
  }
 else                 
 {    c.moveToFirst();               
     String patient_email = c.getString(c.getColumnIndex(PATIENT_EMAIL)); 
 }              
     return patient_email;  
}

我已将patient_email初始化为“”,因此它是null“”将返回其他字符串