当我运行模拟器时,不幸的是,Parkit停了下来。 我收到以下错误,我不知道它为什么会发生。我想知道是否还有其他人可以对这个问题有所了解。 请帮忙。
java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 0 parameters.
代码在这里:
package com.example.a.parkit;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
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);
}
});
}
}
答案 0 :(得分:0)
您得到的错误是因为您已将2个绑定说明符, ?
括在单引号中 '
因此,2 ?
未被识别为绑定说明符。
但是,对其进行更正会导致查询失败,因为您尚未将关键字SELECT与 *
列指示符分开。
我相信使用以下内容可以纠正错误: -
cursor = db.rawQuery("SELECT * FROM " +
DatabaseHelper.TABLE_NAME +
" WHERE " + DatabaseHelper.COL_2 + "=? AND " + DatabaseHelper.COL_4 + "=? ",
new String[]{username, pass}
);
您使用if (cursor != null)
执行此操作后,这将无效并且无效,因为来自任何SQLiteDatabase方法的返回游标(在此实例中为rawQuery
)将永远不会为空。
此外,完成后应始终关闭光标。
我建议你应该/使用: -
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}
);
int rowcount = cursor.getCount(); //<<<< ADDED to get row count
cursor.close(); //<<<< ADDED to close the Cursor
if (rowcount > 0) {
Intent intent = new Intent(MainActivity.this, MapsActivity.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "Login Failed", Toast.LENGTH_SHORT).show();
}
}
});
如果您使用 推荐的 SQLiteDatabase query
方法,则代码可能是: -
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username =loguname.getText().toString();
String pass =logpass.getText().toString();
cursor = db.query(
DatabaseHelper.TABLE_NAME,
null, // (all columns)
DatabaseHelper.COL_2 + "=? AND " + DatabaseHelper.COL_4 + "=?",
new String[]{username, pass},
null, null, null
);
int rowcount = cursor.getCount(); //<<<< ADDED to get row count
cursor.close(); //<<<< ADDED to close the Cursor
if (rowcount > 0) {
Intent intent = new Intent(MainActivity.this, MapsActivity.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "Login Failed", Toast.LENGTH_SHORT).show();
}
}
});