特定事件后,Android应用会一直崩溃

时间:2017-11-21 10:44:02

标签: android sqlite login registration

我是android的新编码。我编写了一个非常简单的寄存器和登录方法。 每次我在注册后尝试登录时,应用程序都会停止工作。

我的OnClickListener

bLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String username = etUsername.getText().toString();
            String password = etPassword.getText().toString();

            String pass = helper.searchPass(username);
            if(password.equals(pass))
            {
                Intent i = new Intent(LoginActivity.this, UserAreaActivity.class);
                LoginActivity.this.startActivity(i);
            }
            else
            {
                Toast wrgPass = Toast.makeText(LoginActivity.this, "Username and password don't match!" , Toast.LENGTH_SHORT);
                wrgPass.show();
            }


        }
    });

我的searchPass方法

public String searchPass(String username)
{
    db = this.getReadableDatabase();
    String query = "select username, password from "+ TABLE_NAME;
    Cursor cursor = db.rawQuery(query, null);
    String a,b;
    //a is Username and b is Password
    b = "not found";
    if(cursor.moveToFirst())
    {
        do{
            a = cursor.getString(0);
            b = cursor.getString(1);

            if (a.equals(username))
            {
                b = cursor.getString(1);
                break;
            }
        }
        while(cursor.moveToNext());

    }

    return b;

}

我也宣称SQLite DB

public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";
SQLiteDatabase db;

private static final String TABLE_CREATE = "create table " +TABLE_NAME + "("
        + COLUMN_ID +"integer primary key,"
        + COLUMN_NAME + " text,"
        + COLUMN_EMAIL + " text,"
        + COLUMN_PASSWORD + " text,"
        + COLUMN_USERNAME + " text )";

public DatabaseHelper(Context context)
{
    super(context , DATABASE_NAME , null , DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db){
    db.execSQL(TABLE_CREATE);
    this.db = db;

}

堆栈跟踪

Process: com.example.lenovo.loginappv2, PID: 6022
android.database.sqlite.SQLiteException: no such column: username (code 1): , while compiling: select username, passwordcontacts
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)

我不确定我哪里出错了。如果可以为解决方案提供解释将会很有帮助,因为我还是Android语言的新手。

编辑:将Imgur添加到stackTrace

编辑2:删除了Imgur并添加了几行StackTrace

1 个答案:

答案 0 :(得分:1)

您的查询错误。

将其更改为

String theParameter = " WHERE " + "username='" + username + "'";
String query = "SELECT * FROM " + TABLE_NAME + theParameter;

这是你完整的方法:

 public String searchPass(String username)
{
    db = this.getReadableDatabase();
    Sring theParameter = " WHERE " + "username='" + username + "'";
    String query = "SELECT * FROM " + TABLE_NAME + theParameter;
    Cursor cursor = db.rawQuery(query, null);
    String a,b;
    //a is Username and b is Password
    b = "not found";
    if(cursor.moveToFirst())
    {
        do{
            a = cursor.getString(0);
            b = cursor.getString(1);

            if (a.equals(username))
            {
                b = cursor.getString(1);
                break;
            }
        }
        while(cursor.moveToNext());

    }

    return b;

}