-1

我正在开发一个在 SQLite 中保存登录用户信息的 android 应用程序。但我面临着奇怪的问题。在我的 DBHelper 类中创建表函数创建得很好,插入函数不会引发任何异常,但是当我尝试从我的 SQL Lite DB 检索数据时它会引发异常

没有这样的列:officer(Sqlite 代码 1):,编译时:Select * from User where userId=officer,(操作系统错误 - 2:没有这样的文件或目录)

我尝试多次更改数据库名称和版本,但没有任何效果。我试图从我的 android 设备中查看数据库文件,以确保我的表创建是否正常,但是当我在手机的 Android/data 文件夹中搜索时,所有应用程序包名称都显示在那里,但我的应用程序的包名称不存在。

public class DBHelper extends SQLiteOpenHelper {
// Database Name
private static final String DATABASE_NAME = "CMDB.db";
private static final int DATABASE_VERSION = 2;
// Contacts table name
private static final String TABLE_USER = "User";
public static DBHelper dbHelper;


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

public static DBHelper getInstance(Context context) {
    if (dbHelper == null) {
        dbHelper = new DBHelper(context.getApplicationContext());
    }
    return dbHelper;
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    createTables(sqLiteDatabase);
    Toast.makeText(App.context,"onCreate",Toast.LENGTH_SHORT).show();



}

private void createTables(SQLiteDatabase sqLiteDatabase) {
    try {
    sqLiteDatabase.execSQL(" CREATE TABLE " + TABLE_USER + " (" +
                "userId" + " TEXT PRIMARY KEY," + "userImage" + " TEXT NOT NULL," +
                "userRole" + " TEXT NOT NULL," + "token" + " TEXT NOT NULL," + "userName" + " TEXT NOT NULL,"  + "userEmail" + " TEXT NOT NULL," + "userAddress" + " TEXT NOT NULL," + "mobile" + " TEXT NOT NULL);"
        );
        Toast.makeText(App.context,"createTables",Toast.LENGTH_SHORT).show();
    }catch (Exception ex){
        Toast.makeText(App.context,ex.getMessage(),Toast.LENGTH_SHORT).show();

        ex.printStackTrace();
    }

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    dropTables(sqLiteDatabase);

    // Create tables again
    onCreate(sqLiteDatabase);
}

private void dropTables(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);

}

public void saveUserToDB(User user) {
    try {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("userId", user.userID);
        values.put("userImage", user.userImage);
     values.put("userRole", user.userRole);
        values.put("token", user.token);
        values.put("UserName", user.UserName);
        values.put("userEmail", user.userEmail);
        values.put("userAddress", user.userAddress);
        values.put("mobile", user.mobile);

        db.insert(TABLE_USER, null, values);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public User getUserFromDB(String userid) {
    User user = new User();
    try {
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "Select * from " + TABLE_USER + " where userId=" +userid+ "";

        Cursor cursor = db.rawQuery(query, null);
        if (cursor != null) {
            cursor.moveToFirst();

            user.userID = cursor.getString(0);
            user.userImage = cursor.getString(1);
         user.userRole = cursor.getString(2);
            user.token = cursor.getString(3);
            user.UserName = cursor.getString(4);
            user.userEmail = cursor.getString(5);
            user.userAddress = cursor.getString(6);
            user.mobile = cursor.getString(7);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return user;

}

public void updateUser(User user, String userId) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    Gson gson = new Gson();
    String userJson = gson.toJson(user);
    contentValues.put("user", userJson);
    db.update(TABLE_USER, contentValues, "userId=" + userId, null);
}

public void updateUser(User user) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put("userId", user.userID);
    cv.put("userImage", user.userImage);
  cv.put("userRole", user.userRole);
    cv.put("token", user.token);
    cv.put("UserName", user.UserName);

    cv.put("userEmail", user.userEmail);
    cv.put("userAddress", user.userAddress);
    cv.put("mobile", user.mobile);

    db.update(TABLE_USER, cv, "userId=" + user.userID, null);
}}

我希望使用用户表和提到的列创建 SQLite DB。还需要在 Android Device 中查看 .db 文件。

4

2 回答 2

1

根据您提到的例外情况:

“没有这样的列:officer(Sqlite 代码 1):,编译时:Select * from User where userId=officer,(操作系统错误 - 2:没有这样的文件或目录)”

您的值有错误userId,即officer

尝试在其前后添加单引号,如下所示:

String query = "Select * from " + TABLE_USER + " where userId='" + userid + "'";
于 2018-12-26T08:56:32.503 回答
0

请发布完整的错误跟踪,以便我们可以正确检查错误。从上面的代码和解释中,我可以看到以下查询的一种可能性

sqLiteDatabase.execSQL(" CREATE TABLE " + TABLE_USER + " (" +
                "userId" + " TEXT PRIMARY KEY," + "userImage" + " TEXT NOT NULL," +
                "userRole" + " TEXT NOT NULL," + "token" + " TEXT NOT NULL," + "userName" + " TEXT NOT NULL,"  + "userEmail" + " TEXT NOT NULL," + "userAddress" + " TEXT NOT NULL," + "mobile" + " TEXT NOT NULL);"

密钥userNameUserName下面的代码不同

public void updateUser(User user) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put("userId", user.userID);
    cv.put("userImage", user.userImage);
  cv.put("userRole", user.userRole);
    cv.put("token", user.token);
    cv.put("UserName", user.UserName);

    cv.put("userEmail", user.userEmail);
    cv.put("userAddress", user.userAddress);
    cv.put("mobile", user.mobile);

    db.update(TABLE_USER, cv, "userId=" + user.userID, null);
}}

注意: DB 中的列名始终区分大小写。

于 2018-12-26T07:36:15.167 回答