查询列

时间:2016-11-07 05:24:37

标签: android sql

我从SQL查询数据时遇到索引超出范围的错误。这是代码。实际上我做了什么,我把pin从edittext保存到数据库,现在比较输入引脚和数据库条目以便登录。

public class LoginSCreen extends AppCompatActivity {
    EditText enterpin;
    Button login;
    SQLiteDatabase db;
    PinHelper dbhelper;
    Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_screen);

        enterpin= (EditText) findViewById(R.id.enterpin);
        login= (Button) findViewById(R.id.login);

        dbhelper= new PinHelper(this);
        db= dbhelper.getReadableDatabase();

        final  String pin=enterpin.getText().toString();
        int pins= cursor.getColumnIndex(PinContract.NoteEntry.COLUMN_password);
        final String pwd= cursor.getString(pins);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(pin==getSinlgeEntry(pwd)){
                    Intent in= new Intent(LoginSCreen.this, CatalogActivity.class);
                    startActivity(in);
                }else
                    Snackbar.make(view, "Pin is Incorrect", Snackbar.LENGTH_SHORT).show();
            }
        });
    }

    public String getSinlgeEntry(String passWord)
    {
        Cursor cursor=db.query("LOGIN", null, " PASSWORD=?", new String[]{passWord}, null, null, null);

        if(cursor.getCount()<1) // UserName Not Exist
        {
            cursor.close();
            return "NOT EXIST";
        }

        cursor.moveToFirst();
        String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
        cursor.close();
        return password;
    }
}

这是错误:

  

致命的例外:主要

     

过程:com.amitkhatkar.privatenotes,PID:9780
  java.lang.RuntimeException:无法启动活动ComponentInfo {com.amitkhatkar.privatenotes / com.amitkhatkar.privatenotes.LoginSCreen}:android.database.CursorIndexOutOfBoundsException:索引-1请求,大小为0
  在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
  在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)&gt;在android.app.ActivityThread.-wrap11(ActivityThread.java)
  在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)
  在android.os.Handler.dispatchMessage(Handler.java:102)
  在android.os.Looper.loop(Looper.java:148)
  在android.app.ActivityThread.main(ActivityThread.java:5417)
  at java.lang.reflect.Method.invoke(Native Method)
  在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

     

引起:android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为0

     

在android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
  在android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)&gt;在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)                                                                                   在com.amitkhatkar.privatenotes.LoginSCreen.onCreate(LoginSCreen.java:40)                                                                                   在android.app.Activity.performCreate(Activity.java:6237)                                                                                   在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)                                                                                   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)                                                                                   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                                   在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                   在android.os.Looper.loop(Looper.java:148)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:0)

以下代码行

int pins= cursor.getColumnIndex(PinContract.NoteEntry.COLUMN_password);
final String pwd= cursor.getString(pins);
getSingleEntry()方法中初始化光标时,

会给出错误。所以此时你的光标可能会为空。

此外,你甚至没有在任何地方使用这些字符串。

答案 1 :(得分:0)

你的问题就在这一行:

 if(cursor.getCount()<1) // UserName Not Exist

游标起始索引为0,因此空检查失败。 使用:

if(cursor.getCount >0)
//Your logic here
else
// UserName Not Exist