我正在尝试在android中执行基本登录表单。这是代码的修剪版本。基本上,如果用户输入无效的登录详细信息,则应触发警报。但是,当我点击登录按钮时,我收到一个错误。 下面发布的是我的代码。
AWSDynamoDBPutItemInput *request = [[AWSDynamoDBPutItemInput alloc] init];
发布的是我的数据库助手类
Alert_Dialouge alert = new Alert_Dialouge();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
context = this;
final DataBaseHelper hl = new DataBaseHelper(context);
AlertDialog alertDialog = new AlertDialog.Builder(
Main_activity.this).create();
Button b1 = (Button) findViewById(R.id.login);
v1 = (TextView) findViewById(R.id.TEXT_STATUS_ID);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
c = c + 1;
EditText user = (EditText) findViewById(R.id.username);
EditText pwd = (EditText) findViewById(R.id.password);
String username = user.getText().toString();
String password = pwd.getText().toString();
db = hl.getReadableDatabase();
db.beginTransaction();
String select = "SELECT * from " + " " + hl.TABLE_NAME+ " "+
"where Username = admin and Password = admin;" ;
Cursor c = db.rawQuery(select, null);
if ((username.equals("") && password.equals("")) ||
password.equals("") || username.equals("")||c.getCount()==0) {
alert.showAlertDialog(Main_activity.this, "Please enter your credentials", false);
Intent intent = new Intent(getApplicationContext(), Main_activity.class);
startActivity(intent);
finish();
} else if(c.getCount()>0) {
c.moveToFirst();
System.out.println("hiiii");
}
c.close();
}
});
}
}
发布的是stackTrace:
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME ="db1";
public static final int DATABASE_VERSION=8;
public static final String COLUMN_ID = "_id";
public static final String TABLE_NAME ="malware_users1";
public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " +"
"+ TABLE_NAME +"( _id INTEGER PRIMARY KEY AUTOINCREMENT,Username
VARCHAR(255),Password VARCHAR(255))";
public static final String DELETE_TABLE= "DROP TABLE IF EXISTS " +
TABLE_NAME;
public DataBaseHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DELETE_TABLE);
oncreate(db);
}
public void insertData(String Username,String Password)
{
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
ContentValues values;
try
{
values = new ContentValues();
values.put("Username",Username);
values.put("Password",Password);
long i =db.insert(TABLE_NAME,null,values);
Log.i("Insert",i+"");
db.setTransactionSuccessful();
}
catch (SQLiteException e)
{
e.printStackTrace();
}
finally {
db.endTransaction();
db.close();
}
}
}
答案 0 :(得分:2)
请检查我更改的查询
String select = "SELECT * from " + " " + hl.TABLE_NAME+ " "+
"where Username = 'admin' and Password = 'admin'" ;
请检查一下,让我知道它解决了这个问题吗?
答案 1 :(得分:2)
更好的方法是使用selectionArgs参数
String select = "SELECT * from " + " " + hl.TABLE_NAME+ " "+
"where Username = ? and Password = ? ";
Cursor c = db.rawQuery(select, new String[] {"admin", "admin});
Here您可以找到文档
答案 2 :(得分:1)
问题出在你的选择查询
上更改此
String select = "SELECT * from " + " " + hl.TABLE_NAME+ " "+
"where Username = admin and Password = admin;" ;
到
String username="admin";
String password="admin";
String select = "SELECT * from " + hl.TABLE_NAME+
" where Username ='"+username+"' and Password ='"+password+"'" ;