Skip to main content
 首页 » 编程设计

android之当我更正 SQL 表名称中的拼写错误时,为什么我的应用程序会崩溃

2024年02月20日8softidea

 private static final String TABLE_USERS = "Registerd_Accounts"; 

当我把它改成这个时 私有(private)静态最终字符串TABLE_USERS =“Registered_Accounts”;

当我按下按钮或任何使用数据库的东西时,它崩溃了。当我将其改回拼写错误时,它就可以工作。但是“Registerd_Accounts”仅在处理程序中使用,所以怎么了? 在此输入代码。

 package com.set.ultimax.login; 
 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
 
public class DatabaseHandler extends SQLiteOpenHelper { 
 
// All Static variables 
// Database Version 
private static final int DATABASE_VERSION = 1; 
 
// Database Name 
private static final String DATABASE_NAME = "All_Accounts"; 
 
private static final String TABLE_USERS = "Registered_Accounts"; 
 
// Contacts Table Columns names 
private static final String KEY_ID = "id"; 
private static final String KEY_USERS = "userName"; 
private static final String KEY_PASS = "passWord"; 
 
public DatabaseHandler(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 
 
// Creating Tables 
@Override 
public void onCreate(SQLiteDatabase db) { 
    String CREATE_ACCOUNT_TABLE = "CREATE TABLE " + TABLE_USERS + "(" 
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_USERS + " TEXT," 
            + KEY_PASS + " TEXT" + ")"; 
    db.execSQL(CREATE_ACCOUNT_TABLE); 
} 
 
// Upgrading database 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // Drop older table if existed 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS); 
    onCreate(db);// Create tables again 
} 
 
// Adding new User 
void addUsers(Users users) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
 
    ContentValues values = new ContentValues(); 
    values.put(KEY_USERS, users.getUser()); // User Name 
    values.put(KEY_PASS, users.getPassword()); // Password 
 
    // Inserting Row 
    db.insert(TABLE_USERS, null, values); 
    db.close(); // Closing database connection 
} 
public void deleteAll() //Deletes all data in the database 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_USERS,null,null); 
} 
 
public boolean validateUser(String username, String password){ 
    Cursor c = getReadableDatabase().rawQuery( 
            "SELECT * FROM " + TABLE_USERS + " WHERE " 
                    + KEY_USERS + "='" + username +"'AND "+KEY_PASS+"='"+password+"'" ,  null); 
    if (c.getCount() > 0) { 
        return true; 
    } 
    else{return false;} 
 
} 
public boolean sameUser(String username){ 
    Cursor c = getReadableDatabase().rawQuery( 
            "SELECT * FROM " + TABLE_USERS + " WHERE " 
                    + KEY_USERS + "='" + username  + "'" ,  null); 
    if (c.getCount() > 0) { 
        return true; 
    } 
    else{return false;} 
} 
 
} 

这是我的访问器方法

 package com.set.ultimax.login; 
 
  public class Users { 
 
//private variables 
int _id; // Just to have it. 
String _userName; 
String _passWord; 
 
// Empty constructor 
public Users(){ 
 
} 
// constructor 
public Users(String userName, String _passWord){ 
    this._userName = userName; 
    this._passWord = _passWord; 
} 
// getting User 
public String getUser(){ 
    return this._userName; 
} 
 
// getting Password 
public String getPassword(){ 
    return this._passWord; 
} 
} 

请您参考如下方法:

您应该从设备中卸载该应用程序,然后再次安装并检查它。这可能对您有帮助,因为当我们安装应用程序时,我们的应用程序也会创建本地数据库和数据库表。因此,当您安装时,您的本地数据库已使用拼写错误的表名创建,并且您的应用程序尝试找到正确的拼写表。