在数据库中搜索一行不起作用,字符串比较错误

时间:2015-10-15 20:37:45

标签: java android android-sqlite

我正在尝试在android studio中创建一个注册登录应用程序,我在注册部分遇到了一些问题。这是我在RegisterActivity中的代码:

public void addUser (View view) {
    StringBuffer errors = new StringBuffer();
    StringBuffer noerrors = new StringBuffer();
    errors.append("");
    noerrors.append("");

    String firstname = FirstName.getText().toString();
    String lastname = LastName.getText().toString();
    String username = UserName.getText().toString();
    String password = UserPassoword.getText().toString();
    String retypepassword = RetypePassword.getText().toString();
    String email = UserEmail.getText().toString();

    if(!searchUsername(username)) {
        errors.append("Username already exists\n");
        UserName.clearComposingText();

    }

    if(!searchEmail(email)) {
        errors.append("Email already in use\n");
        UserEmail.clearComposingText();
    }

    if(password.equals(retypepassword)){
        errors.append("Password retyped incorrectly");
        UserPassoword.clearComposingText();
        RetypePassword.clearComposingText();
    }

    userDbHelper = new UserDbHelper(context);
    sqLiteDatabase = userDbHelper.getWritableDatabase();


    if(errors.toString().equals(noerrors.toString())) {
        userDbHelper.addInformation(firstname, lastname, username, password, email, sqLiteDatabase);
        Toast.makeText(getBaseContext(), "Register successful!", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();

    } else {
        Toast.makeText(this, errors, Toast.LENGTH_LONG).show();
    }

    userDbHelper.close();
}

public boolean searchUsername(String username) {

    userDbHelper = new UserDbHelper(getApplicationContext());
    sqLiteDatabase = userDbHelper.getReadableDatabase();

    Cursor cursor = userDbHelper.getUsername(username, sqLiteDatabase);

    if(cursor.moveToFirst()) {
        return true;
    } else {
        return false;
    }
}

这是我的数据库助手

public class UserDbHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "USERINFO.DB";
    private static final int DATABASE_VERSION = 1;
    private static final String CREATE_QUERY =
            "CREATE TABLE " + UserContract.NewUserInfo.TABLE_NAME + "(" + UserContract.NewUserInfo.FIRST_NAME + " TEXT," +
                    UserContract.NewUserInfo.LAST_NAME + " TEXT," + UserContract.NewUserInfo.USER_NAME + " TEXT," +
                    UserContract.NewUserInfo.USER_PASSWORD + " TEXT," + UserContract.NewUserInfo.USER_EMAIL + " TEXT);";

    public UserDbHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_QUERY);
    }

    public void addInformation (String firstname, String lastname, String username, String password, String email, SQLiteDatabase db) {

        ContentValues contentValues = new ContentValues();
        contentValues.put(UserContract.NewUserInfo.FIRST_NAME, firstname);
        contentValues.put(UserContract.NewUserInfo.LAST_NAME, lastname);
        contentValues.put(UserContract.NewUserInfo.USER_NAME, username);
        contentValues.put(UserContract.NewUserInfo.USER_PASSWORD, password);
        contentValues.put(UserContract.NewUserInfo.USER_EMAIL, email);

        db.insert(UserContract.NewUserInfo.TABLE_NAME, null, contentValues);

        Log.e("DATABASE OPERATIONS", "One row inserted");
    }

    public Cursor getUsername (String username, SQLiteDatabase db) {

        String[] projections = {UserContract.NewUserInfo.FIRST_NAME, UserContract.NewUserInfo.LAST_NAME,
                UserContract.NewUserInfo.USER_PASSWORD, UserContract.NewUserInfo.USER_EMAIL};
        String selection = UserContract.NewUserInfo.USER_NAME + " LIKE ?";
        String[] selection_args = {username};

        Cursor cursor = db.query(UserContract.NewUserInfo.TABLE_NAME, projections, selection, selection_args, null, null, null);

        return cursor;
    }
}

在我的注册表单中,每当我正确地重新输入密码时,我的字符串缓冲区显示“密码重新输入不正确”,当我重新输入错误时,此消息不会出现。

在数据库助手中,searchEmail方法与searchUsername相同,只进行了一些更改,以便为电子邮件进行分析。无论我输入数据库中是否存在的用户名/电子邮件,都会附加“用户名已存在”和“已使用的电子邮件”。

我也不明白为什么.clearComposingText()无法清除我的edittext字段。

1 个答案:

答案 0 :(得分:0)

我发现了问题。这两种方法searchUsername //Create TextView to display status of Wifi TextView wifitext = FindViewById<TextView>(Resource.Id.WifiTextView); //Configuring Wifi connection var connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService); var activeConnection = connectivityManager.ActiveNetworkInfo; if (activeConnection != null && activeConnection.IsConnected) { wifitext.Text = "WIFI AVAILABLE"; string[] urladdress = new string[] { "https://www.google.com/", "https://www.yahoo.com/"}; for (int i = 0; i < urladdress.Length; i++) { string url = urladdress[i]; //Call async method Task returnedTask = Task_MethodAsync(url); } } else wifitext.Text = "WIFI UNAVAILABLE"; } public async Task Task_MethodAsync(string url) { LinearLayout ll = FindViewById<LinearLayout>(Resource.Id.linearLayout1); WebClient client = new WebClient(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Stream listurl = client.OpenRead(url); StreamReader reader = new StreamReader(listurl); stopwatch.Stop(); // listurl.Close(); var time = Convert.ToString(stopwatch.Elapsed); 需要将View对象作为参数。