无法绑定索引2处的参数,因为索引超出范围

时间:2018-03-26 15:18:20

标签: java android-studio android-sqlite

我正在努力解决这个问题。任何人都可以帮我解决这个问题。请在查询语句的where子句中告诉我出错的地方。我想从所有列中读取值,然后将它们显示出来。

这是我的查询代码:

public class MainActivity extends AppCompatActivity {
SQLiteDatabase db;
SQLiteOpenHelper openHelper;
Button btnlogin, btnsigup;
EditText loguname, logpass;
Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnlogin = (Button) findViewById(R.id.btnlogin);
    btnsigup = (Button) findViewById(R.id.btnpass);
    loguname = (EditText) findViewById(R.id.loguname);
    logpass = (EditText) findViewById(R.id.logpass);
    openHelper = new DatabaseHelper(this);
    db = openHelper.getReadableDatabase();
    btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String username =loguname.getText().toString();
            String pass =logpass.getText().toString();
            cursor = db.rawQuery("SELECT *FROM " + DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_2 + "='?' AND " + DatabaseHelper.COL_4 + "='?' ", new String[]{username, pass});
            if (cursor != null) {
                if (cursor.getCount() > 0) {
                    Intent intent = new Intent(MainActivity.this, MapsActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(MainActivity.this, "Login Failed", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
    btnsigup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent1 = new Intent(MainActivity.this, SignUp.class);
            startActivity(intent1);
        }
    });
}

}

1 个答案:

答案 0 :(得分:1)

您忘记在SELECT * FROM之间添加空格,并且OH GOD SPIDERS提及删除问号周围的单引号:=?而不是='?'

试试此代码

String query = "SELECT * FROM " + DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_2 + "=? AND " + DatabaseHelper.COL_4 + "=? ";
cursor = db.rawQuery(query, new String[]{username, pass});