SQLiteDatabase.query()无数据返回:Android登录系统查询失败求助
Troubleshooting SQLite Query Not Returning Rows for Login Validation
Hey there! Let's dig into why your SQLite query isn't pulling any matching rows when trying to log in. I’ve gone through your code, and here are the most likely issues to fix, plus actionable steps to get your login flow working:
1. First, Confirm Registration Data is Saved Correctly
The #1 culprit here is usually that the user’s email and password aren’t actually being stored in the database during registration.
- Double-check your registration screen code: Are you using
ContentValuescorrectly to insert the credentials? Did you trim whitespace from the input fields before saving? (Users often accidentally add spaces at the start/end of emails or passwords.) - Example of proper registration insertion:
ContentValues values = new ContentValues(); values.put(loginEntry.COLUMN_EMAIL_ID, registerEmail.getText().toString().trim()); values.put(loginEntry.COLUMN_PASSWORD, registerPassword.getText().toString().trim()); long newRowId = db.insert(loginEntry.TABLE_NAME, null, values);
2. Fix Mismatched Table/Column Names
SQLite is case-insensitive by default, but typos or inconsistent naming will break your query:
- Verify that
loginEntry.TABLE_NAME,COLUMN_EMAIL_ID, andCOLUMN_PASSWORDexactly match the names you used in your database creation SQL. For example, if your create table statement usesemailbut your constant isCOLUMN_EMAIL_ADDRESS, the query won’t find the column. - Example of a valid create table statement (ensure it aligns with your constants):
private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + loginEntry.TABLE_NAME + " (" + loginEntry._ID + " INTEGER PRIMARY KEY," + loginEntry.COLUMN_EMAIL_ID + " TEXT UNIQUE NOT NULL," + loginEntry.COLUMN_PASSWORD + " TEXT NOT NULL)";
3. Refine Your Query Logic & Clean Inputs
Your current checkEntry() method can use a few tweaks to avoid common pitfalls:
- Add
trim()to the email and password inputs to eliminate accidental whitespace that would break matching. - Always close your
Cursor(use afinallyblock) to avoid memory leaks. - Updated
checkEntry()method:public boolean checkEntry(String email, String password){ db = dbHelper.getReadableDatabase(); String[] projection = {loginEntry.COLUMN_EMAIL_ID, loginEntry.COLUMN_PASSWORD}; String selection = loginEntry.COLUMN_EMAIL_ID + " = ? AND " + loginEntry.COLUMN_PASSWORD + " = ?"; String[] selectionArgs = {email.trim(), password.trim()}; // Trim whitespace from inputs Cursor cursor = null; try { cursor = db.query(loginEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null); int rowCount = cursor.getCount(); Log.d("LoginDebug", "Found " + rowCount + " matching rows for email: " + email.trim()); demoText.setText("Rows returned " + rowCount); return rowCount == 1; } finally { if (cursor != null) { cursor.close(); // Guarantee cursor is closed to prevent leaks } } }
4. Debug by Inspecting the Database Directly
To confirm what’s actually stored in your database:
- Use Android Studio’s Device File Explorer to pull the SQLite database file from your app’s data directory.
- Open it with a tool like SQLiteStudio or DB Browser for SQLite to check if registration entries exist, and if the email/password values match what you’re entering during login.
5. Check for Case Sensitivity or Encryption Gaps
- If your database uses
COLLATE BINARY(case-sensitive), an email likeTest@Example.comwon’t matchtest@example.com. Modify your query to make it case-insensitive:String selection = "LOWER(" + loginEntry.COLUMN_EMAIL_ID + ") = LOWER(?) AND " + loginEntry.COLUMN_PASSWORD + " = ?"; - If you encrypt passwords during registration (which you absolutely should for security!), make sure you apply the same encryption to the login password before comparing it to the stored value.
内容的提问来源于stack exchange,提问作者Mark Dohner
相关产品推荐
相关产品推荐

