“没有这样的桌子”使用sqliteopenhelper在sqlite android中的错误

发布于 2025-01-25 15:56:20 字数 5437 浏览 2 评论 0原文

我创建了一个扩展SqliteOpenhelper的DBHandler类。我在类别表中遇到错误:

e/sqlitelog:(1)否这样的表:“插入类别(锁定,名称,级别,boolquestions)值(?,?,?,?,?,?)”中的类别e/sqlitelog:(1)没有这样的表:“ select * select * section * where name =?”

ongreate

  db.beginTransaction();
    try {

        db.execSQL("CREATE TABLE " + TABLE_CATEGORY_NAME + "(" + CATEGORY_COL_NAME_NAME + " Text PRIMARY KEY," + CATEGORY_COL_LEVEL_NAME + " INT," + CATEGORY_COL_LOCKED_NAME + " INT," + CATEGORY_COL_BOOLQ_NAME + " INT);");
        db.execSQL("CREATE TABLE " + TABLE_ROUND_NAME + "(" + ROUND_COL_ID_NAME + " INT PRIMARY KEY," + ROUND_COL_POINTS_NAME + " INT," + ROUND_COL_TOTQ_NAME + " INT," + ROUND_COL_CORRECT_NAME + " INT," + ROUND_COL_CATEGORY_NAME + " Text,"
                + "FOREIGN KEY(" + ROUND_COL_CATEGORY_NAME + ") REFERENCES " + TABLE_CATEGORY_NAME + "(" + CATEGORY_COL_NAME_NAME + "));");
        db.execSQL("CREATE TABLE " + TABLE_BOOLQUESTION_NAME + "(" + BOOLQUESTION_COL_ID_NAME + " INT PRIMARY KEY," + BOOLQUESTION_COL_TEXTSE_NAME + " Text," + BOOLQUESTION_COL_TEXTEN_NAME + " Text," + BOOLQUESTION_COL_CORRANSWER_NAME + " INT," + ROUND_COL_CATEGORY_NAME + " Text,"
                + "FOREIGN KEY(" + BOOLQUESTION_COL_CATEGORY_NAME + ") REFERENCES " + TABLE_CATEGORY_NAME + "(" + CATEGORY_COL_NAME_NAME + "));");
    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        db.endTransaction();
    }

所在的位置

    //Tables and columns
private final String TABLE_ROUND_NAME = "ROUND";
private final String ROUND_COL_ID_NAME = "ID";
private final String ROUND_COL_POINTS_NAME = "Points";
private final String ROUND_COL_TOTQ_NAME = "TotalQuestions";
private final String ROUND_COL_CORRECT_NAME = "CorrectAnswers";
private final String ROUND_COL_CATEGORY_NAME = "Category";

private final String TABLE_CATEGORY_NAME = "CATEGORY";
private final String CATEGORY_COL_NAME_NAME = "Name";
private final String CATEGORY_COL_LEVEL_NAME = "Level";
private final String CATEGORY_COL_LOCKED_NAME = "Locked";
private final String CATEGORY_COL_BOOLQ_NAME = "BoolQuestions";

private final String TABLE_BOOLQUESTION_NAME = "BOOLQUESTION";
private final String BOOLQUESTION_COL_ID_NAME = "ID";
private final String BOOLQUESTION_COL_TEXTSE_NAME = "Text_SE";
private final String BOOLQUESTION_COL_TEXTEN_NAME = "Text_EN";
private final String BOOLQUESTION_COL_CORRANSWER_NAME = "CorrectAnswer";
private final String BOOLQUESTION_COL_CATEGORY_NAME = "Category";

//Category name contents
private final String CATEGORY_ROW_ADDSUBTRACT_NAME = "ADD_SUBTRACT";

在onupgrade

   @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


            db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOLQUESTION_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_ROUND_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORY_NAME);
            onCreate(db);

}

,唯一对数据库的调用来自mainActivity的oncreate:inviewey of ot otecreate:inview of

DBHandler.getInstance(this).insertCategory(Category.add_subtract,0,false,false);

onviewScreate inviewScreate :

        categoryModelArrayList.add(DBHandler.getInstance(view.getContext()).getCategoryModel(Category.add_subtract));

为什么不创建类别表?

检索和插入方法看起来像这样:

    public CategoryModel getCategoryModel(Category category) {

    CategoryModel cModel = new CategoryModel();
    String categoryName = categoryENumToString(category);

    SQLiteDatabase db = this.getWritableDatabase();

    db.beginTransaction();
    Cursor cursor = null;

    try {
        cursor = db.rawQuery("SELECT *" + " FROM " + TABLE_CATEGORY_NAME + " WHERE " + CATEGORY_COL_NAME_NAME + " = ?", new String[] {categoryName});
        System.out.println(category + " Count: " + cursor.getCount());
        if(cursor.getCount() > 0) {
            cursor.moveToFirst();
            cModel.setName(cursor.getString(0));
            cModel.setMedal(cursor.getInt(1));
            if(cursor.getInt(2) == 1) {
                cModel.setLocked(true);
            } else {
                cModel.setLocked(false);
            }
            if(cursor.getInt(3) == 1) {
                cModel.setBoolQuestions(true);
            } else {
                cModel.setBoolQuestions(false);
            }
        }
        return cModel;
    }finally {
        if(cursor != null) {
            cursor.close();
        }
        db.endTransaction();
    }
}

public void insertCategory(Category category, int level, boolean locked, boolean boolQ) {

    String sCategory;
    int iLocked;
    int iBoolQ;

    sCategory = categoryENumToString(category);

    if(locked)
        iLocked=1;
    else
        iLocked=0;

    if(boolQ)
        iBoolQ=1;
    else
        iBoolQ=0;

    SQLiteDatabase db = getWritableDatabase();

    db.beginTransaction();
    try {
        ContentValues values = new ContentValues();
        values.put(CATEGORY_COL_NAME_NAME, sCategory);
        values.put(CATEGORY_COL_LEVEL_NAME, level);
        values.put(CATEGORY_COL_LOCKED_NAME, iLocked);
        values.put(CATEGORY_COL_BOOLQ_NAME, iBoolQ);

        db.insertOrThrow(TABLE_CATEGORY_NAME, null, values);
        db.setTransactionSuccessful();

    } catch (Exception e) {

    } finally {
        db.endTransaction();
        db.close();
    }


}

I have created a DBHandler class that extends SQLiteOpenHelper. I am getting an error with the category table:

E/SQLiteLog: (1) no such table: CATEGORY in "INSERT INTO CATEGORY(Locked,Name,Level,BoolQuestions) VALUES (?,?,?,?)" E/SQLiteLog: (1) no such table: CATEGORY in "SELECT * FROM CATEGORY WHERE Name = ?"

onCreate is

  db.beginTransaction();
    try {

        db.execSQL("CREATE TABLE " + TABLE_CATEGORY_NAME + "(" + CATEGORY_COL_NAME_NAME + " Text PRIMARY KEY," + CATEGORY_COL_LEVEL_NAME + " INT," + CATEGORY_COL_LOCKED_NAME + " INT," + CATEGORY_COL_BOOLQ_NAME + " INT);");
        db.execSQL("CREATE TABLE " + TABLE_ROUND_NAME + "(" + ROUND_COL_ID_NAME + " INT PRIMARY KEY," + ROUND_COL_POINTS_NAME + " INT," + ROUND_COL_TOTQ_NAME + " INT," + ROUND_COL_CORRECT_NAME + " INT," + ROUND_COL_CATEGORY_NAME + " Text,"
                + "FOREIGN KEY(" + ROUND_COL_CATEGORY_NAME + ") REFERENCES " + TABLE_CATEGORY_NAME + "(" + CATEGORY_COL_NAME_NAME + "));");
        db.execSQL("CREATE TABLE " + TABLE_BOOLQUESTION_NAME + "(" + BOOLQUESTION_COL_ID_NAME + " INT PRIMARY KEY," + BOOLQUESTION_COL_TEXTSE_NAME + " Text," + BOOLQUESTION_COL_TEXTEN_NAME + " Text," + BOOLQUESTION_COL_CORRANSWER_NAME + " INT," + ROUND_COL_CATEGORY_NAME + " Text,"
                + "FOREIGN KEY(" + BOOLQUESTION_COL_CATEGORY_NAME + ") REFERENCES " + TABLE_CATEGORY_NAME + "(" + CATEGORY_COL_NAME_NAME + "));");
    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        db.endTransaction();
    }

Where

    //Tables and columns
private final String TABLE_ROUND_NAME = "ROUND";
private final String ROUND_COL_ID_NAME = "ID";
private final String ROUND_COL_POINTS_NAME = "Points";
private final String ROUND_COL_TOTQ_NAME = "TotalQuestions";
private final String ROUND_COL_CORRECT_NAME = "CorrectAnswers";
private final String ROUND_COL_CATEGORY_NAME = "Category";

private final String TABLE_CATEGORY_NAME = "CATEGORY";
private final String CATEGORY_COL_NAME_NAME = "Name";
private final String CATEGORY_COL_LEVEL_NAME = "Level";
private final String CATEGORY_COL_LOCKED_NAME = "Locked";
private final String CATEGORY_COL_BOOLQ_NAME = "BoolQuestions";

private final String TABLE_BOOLQUESTION_NAME = "BOOLQUESTION";
private final String BOOLQUESTION_COL_ID_NAME = "ID";
private final String BOOLQUESTION_COL_TEXTSE_NAME = "Text_SE";
private final String BOOLQUESTION_COL_TEXTEN_NAME = "Text_EN";
private final String BOOLQUESTION_COL_CORRANSWER_NAME = "CorrectAnswer";
private final String BOOLQUESTION_COL_CATEGORY_NAME = "Category";

//Category name contents
private final String CATEGORY_ROW_ADDSUBTRACT_NAME = "ADD_SUBTRACT";

onUpgrade is

   @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


            db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOLQUESTION_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_ROUND_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORY_NAME);
            onCreate(db);

}

And the only calls to the database are from MainActivity's onCreate:

DBHandler.getInstance(this).insertCategory(Category.add_subtract,0,false,false);

and from onViewCreated in a fragment:

        categoryModelArrayList.add(DBHandler.getInstance(view.getContext()).getCategoryModel(Category.add_subtract));

Why isn't the CATEGORY table being created?

The retrieve and insert methods look like this:

    public CategoryModel getCategoryModel(Category category) {

    CategoryModel cModel = new CategoryModel();
    String categoryName = categoryENumToString(category);

    SQLiteDatabase db = this.getWritableDatabase();

    db.beginTransaction();
    Cursor cursor = null;

    try {
        cursor = db.rawQuery("SELECT *" + " FROM " + TABLE_CATEGORY_NAME + " WHERE " + CATEGORY_COL_NAME_NAME + " = ?", new String[] {categoryName});
        System.out.println(category + " Count: " + cursor.getCount());
        if(cursor.getCount() > 0) {
            cursor.moveToFirst();
            cModel.setName(cursor.getString(0));
            cModel.setMedal(cursor.getInt(1));
            if(cursor.getInt(2) == 1) {
                cModel.setLocked(true);
            } else {
                cModel.setLocked(false);
            }
            if(cursor.getInt(3) == 1) {
                cModel.setBoolQuestions(true);
            } else {
                cModel.setBoolQuestions(false);
            }
        }
        return cModel;
    }finally {
        if(cursor != null) {
            cursor.close();
        }
        db.endTransaction();
    }
}

public void insertCategory(Category category, int level, boolean locked, boolean boolQ) {

    String sCategory;
    int iLocked;
    int iBoolQ;

    sCategory = categoryENumToString(category);

    if(locked)
        iLocked=1;
    else
        iLocked=0;

    if(boolQ)
        iBoolQ=1;
    else
        iBoolQ=0;

    SQLiteDatabase db = getWritableDatabase();

    db.beginTransaction();
    try {
        ContentValues values = new ContentValues();
        values.put(CATEGORY_COL_NAME_NAME, sCategory);
        values.put(CATEGORY_COL_LEVEL_NAME, level);
        values.put(CATEGORY_COL_LOCKED_NAME, iLocked);
        values.put(CATEGORY_COL_BOOLQ_NAME, iBoolQ);

        db.insertOrThrow(TABLE_CATEGORY_NAME, null, values);
        db.setTransactionSuccessful();

    } catch (Exception e) {

    } finally {
        db.endTransaction();
        db.close();
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2025-02-01 15:56:20

我通过直接使用室友而不是sqlite解决了问题。

I solved the problem by using Room-ORM instead of SQLite directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文