英文:
"User Name or Password does not match" even when the username and pass exist in database and are valid
问题
我一直在尝试创建一个注册和登录页面,当我只使用用户名和密码时可以正常工作,但是当我添加了“学科”字段后,我无法登录到我创建的帐户,它显示“用户名或密码不匹配”。我很确定错误在我的LoginDataBaseAdapter中,但我不确定错误在哪里。
DataBaseHelper.java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper {
public DataBaseHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
Log.w("TaskDBAdapter", "Upgrading from version " + _oldVersion + " to " + _newVersion + ", which will destroy all old data");
_db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
onCreate(_db);
}
}
LoginDataBaseAdapter.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class LoginDataBaseAdapter {
// ...
static final String DATABASE_CREATE = "create table " + "LOGIN" +
"( " + "ID" + " integer primary key autoincrement," + "USERNAME text,PASSWORD text,SUBJECT text); ";
// ...
public void insertEntry(String userName, String password, String Subject) {
ContentValues newValues = new ContentValues();
newValues.put("USERNAME", userName);
newValues.put("PASSWORD", password);
newValues.put("SUBJECT", Subject);
db.insert("LOGIN", null, newValues);
}
// ...
public String getSinlgeEntry(String userName) {
Cursor cursor = db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
if (cursor.getCount() < 1) {
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
String Subject = cursor.getString(cursor.getColumnIndex("SUBJECT"));
cursor.close();
return password;
}
// ...
}
MainActivity.java+login page
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
// ...
public void signIn(View V) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
final EditText editTextUserName = (EditText) dialog.findViewById(R.id.editTextUserNameToLogin);
final EditText editTextPassword = (EditText) dialog.findViewById(R.id.editTextPasswordToLogin);
Button btnSignIn = (Button) dialog.findViewById(R.id.buttonSignIn);
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String userName = editTextUserName.getText().toString();
String password = editTextPassword.getText().toString();
String storedPassword = loginDataBaseAdapter.getSinlgeEntry(userName);
if (password.equals(storedPassword)) {
Toast.makeText(MainActivity.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
dialog.dismiss();
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("USERNAME", userName);
editor.apply();
Intent myIntent = new Intent(getBaseContext(), signedin.class);
myIntent.putExtra("USERNAME", userName);
startActivity(myIntent);
} else {
Toast.makeText(MainActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
}
// ...
}
SignUPActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SignUPActivity extends Activity {
// ...
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String userName = editTextUserName.getText().toString();
String password = editTextPassword.getText().toString();
String confirmPassword = editTextConfirmPassword.getText().toString();
String Subject = editTextSubject.getText().toString();
if (userName.equals("") || password.equals("") || confirmPassword.equals("") || Subject.equals("")) {
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
if (!password.equals(confirmPassword)) {
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
} else {
loginDataBaseAdapter.insertEntry(userName, password, Subject);
Toast.makeText(getApplicationContext(), "Account Successfully Created", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(myIntent);
}
}
});
// ...
}
Contact.java
public class Contact {
String uname;
String pass;
String subject;
public Contact(String uname, String pass, String subject) {
this.uname = uname;
this.pass = pass;
this.subject = subject;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
注意:这些代码是根据您提供的内容进行的翻译,以确保只包含翻译的部分,而不包含其他内容。如果您有任何问题或需要进一步的帮助,请随时询问。
英文:
I have been trying to create a sign up and login page and using only username and password worked but when I added the field of subject I can't log into the account I create and it says "User Name or Password does not match". I am pretty sure the error is in my LoginDataBaseAdapter but im not sure what is the error.
DataBaseHelper.java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
{
super(context, name, factory, version);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);
}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");
// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// The simplest case is to drop the old table and create a new one.
_db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
// Create a new one.
onCreate(_db);
}
}
LoginDataBaseAdapter.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class LoginDataBaseAdapter
{
static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text,SUBJECT text); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String userName,String password,String Subject)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
newValues.put("SUBJECT", Subject);
// Insert the row into your table
db.insert("LOGIN", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int deleteEntry(String UserName)
{
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
// Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String getSinlgeEntry(String userName)
{
Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
String Subject= cursor.getString(cursor.getColumnIndex("SUBJECT"));
cursor.close();
return password;
}
public void updateEntry(String userName,String password,String Subject)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD",password);
updatedValues.put("SUBJECT", Subject);
String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{userName});
}
}
MainActivity.java+login page
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity
{
Button btnSignIn,btnSignUp;
LoginDataBaseAdapter loginDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create a instance of SQLite Database
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get The Reference Of Buttons
btnSignIn=(Button)findViewById(R.id.buttonSignIN);
btnSignUp=(Button)findViewById(R.id.buttonSignUP);
// Set OnClick Listener on SignUp button
btnSignUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
/// Create Intent for SignUpActivity abd Start The Activity
Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
startActivity(intentSignUP);
}
});
}
// Methos to handleClick Event of Sign In Button
public void signIn(View V)
{
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
// get the References of views
final EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
final EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);
Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);
// Set On ClickListener
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get The User name and Password
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
// fetch the Password form database for respective user name
String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);
// check if the Stored password matches with Password entered by user
if(password.equals(storedPassword))
{
Toast.makeText(MainActivity.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
dialog.dismiss();
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
editor.putString("USERNAME",userName);
editor.apply();
Intent myIntent = new Intent(getBaseContext(), signedin.class);
myIntent.putExtra("USERNAME",userName);
startActivity(myIntent);
}
else
{
Toast.makeText(MainActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
}
@Override
protected void onDestroy() {
super.onDestroy();
// Close The Database
loginDataBaseAdapter.close();
}
}
SignUPActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SignUPActivity extends Activity
{
EditText editTextUserName,editTextPassword,editTextConfirmPassword,editTextSubject;
Button btnCreateAccount;
LoginDataBaseAdapter loginDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
// get Instance of Database Adapter
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get References of Views
editTextUserName=(EditText)findViewById(R.id.editTextUserName);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
editTextSubject=(EditText)findViewById(R.id.editTextSubject);
btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
String confirmPassword=editTextConfirmPassword.getText().toString();
String Subject=editTextSubject.getText().toString();
// check if any of the fields are vaccant
if(userName.equals("")||password.equals("")||confirmPassword.equals("")||Subject.equals(""))
{
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!password.equals(confirmPassword))
{
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
else
{
// Save the Data in Database
loginDataBaseAdapter.insertEntry(userName, password, Subject);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(myIntent);
}
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
loginDataBaseAdapter.close();
}
}
Contact.java
public class Contact {
String uname;
String pass;
String subject;
public Contact(String uname,String pass,String subject)
{
this.uname=uname;
this.pass=pass;
this.subject=subject;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
答案1
得分: 0
首先,您应该浏览您的 SQLite 数据库数据。有不同的工具可以帮助您实现这一点(例如 DB Browser)。如果没有这样做,您只能猜测问题的来源。
分析您的问题测试案例时,有几种可能的情况:
- 在您测试的用户名下没有任何行。由于您的保存登录代码没有错误(看起来是这样的),这可能不是问题的原因。
- 只有一行用户名和保存的密码不同。如果是这样,那么您就是在黑屋子里寻找黑猫了
- 只有一行用户名和保存的密码与您要测试的密码相同。根据您的代码,我认为这不可能。
- 有几行相同的用户名。从技术上讲,这可能是因为您没有添加某些检查来防止将一个用户名与不同密码一起保存。顺便说一下,您有一个名为 "updateEntry" 的方法,但您在任何地方都没有使用过它,只有 "insertEntry"。也许现在是使用它的时候了?
我的第六感告诉我,第四种情况将是获胜者。
英文:
First thing you should do - to browse you sqlite database data. There are different tools that can help you in that (DB Browser for example). Without that you can only guess what is the source of your problem.
There are several possible scenarios you can get analysing your problem test-case:
-
There are no rows in your table with the username you test. Since your save-login code has no mistakes (it seemed so), this is probably not the case.
-
There is only one row with the username and saved password differs. If it's so, you're searching for the black cat in black room
-
There is only one row with the username and saved password the same as you're trying to test. According to your code I think it can't be.
-
There are several rows with the same username. Technically it can be because you haven't put some check to prevent keeping one username with different passwords. By the way, you have method "updateEntry", but you don't use it anywhere, just "insertEntry". May be it's time to use it?
My sixth sense tell me that variant 4 will be the winner
专注分享java语言的经验与见解,让所有开发者获益!
评论