如何修复“ SQLite无列异常”?

时间:2018-12-26 07:25:41

标签: android android-sqlite android-package-managers android-database android-flavors

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

  

没有这样的列:官员(SQLite代码1):,而在编译时:从UserId =官员的用户中选择*,(操作系统错误-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数据库。还需要在Android设备中查看.db文件。

2 个答案:

答案 0 :(得分:1)

根据您提到的例外情况:

  

“没有这样的列:官员(SQLite代码1):,而在编译时:选择*   来自用户,其中userId =官员,(操作系统错误-2:无此类文件或   目录)”

您在userId(即officer)的值中出现错误。

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

String query = "Select * from " + TABLE_USER + " where userId='" + userid + "'";

答案 1 :(得分: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);"

密钥userName与下面的代码中的UserName不同

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);
}}

注意:数据库中的列名始终区分大小写。