“没有这样的表"在 SQLite Android 中发现错误 [英] "No Such Table" Error found in SQLite Android

查看:23
本文介绍了“没有这样的表"在 SQLite Android 中发现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 SQLite 数据库,但我真的很讨厌处理任何后端的东西,充满热情.我已经遇到了一个看似简单的问题.

I am trying to learn about SQLite databases, but I really hate dealing with any back-end stuff, with a passion. I'm already hitting walls with a seemingly simple problem.

这是我认为来自 DatabaseHelper 类的重要代码

Here is the code that I think matters from the DatabaseHelper class

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "Library";

    public static final String TABLE_NAME = "books";
    public static final String TITLE = "title";
    public static final String AUTHOR = "author";
    public static final String ISBN = "isbn";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, isbn TEXT)");
    }


    public boolean insertBook(String title, String author, String isdn) {

        try {
            SQLiteDatabase db = getWritableDatabase();

            ContentValues cv = new ContentValues();
            cv.put(TITLE, title);
            cv.put(AUTHOR, author);
            cv.put(ISBN, isdn);

            db.insert(TABLE_NAME, null, cv);
            db.close();

            return true;
        } catch (Exception exp) {
            exp.printStackTrace();

            return false;
        }
    }
}

这是我主要活动中的代码

And this is the code in my main activity

dbHelper = new DatabaseHelper(this);

dbHelper.insertBook("Harry Potter", "JK", "1000");
dbHelper.insertBook("Hamlet", "Shakespeare", "500");

Eclipse 告诉我 insertBook() 方法中存在错误.它说没有这样的桌书:....我不知道我在这里做错了什么.更令人沮丧的是,在它完美运行之前只有几分钟,然后(我认为)我删除了表,它只是出于任何原因再次创建它,即使此代码自我第一次创建以来没有改变(我想……)

Eclipse is telling me that there is an error in the insertBook() method. It says that there is no such table books: .... I have no idea what I am doing wrong here. What makes it more frustrating is that only a couple of minutes before it was working perfectly, then (I think) I dropped the table and it just create it again for whatever reason, even though this code has not changed since I first created it (I think...).

推荐答案

您的设备上有一个旧版本的数据库,其中有(空)数据库,但没有 books 表.如果这是您的选择,只需卸载并重新安装该应用即可.

There is an older version of the database on your device, which does have the (empty) database in place, but not the books table. If that's an option for you, just uninstall and reinstall the app.

稍后,当您想在最终用户设备上的生产过程中向数据库添加新表,但保留现有数据时,添加新表、更改架构或升级数据的指定挂钩是 SQLiteOpenHelper 的 >onUpgrade 方法.

Later, when you'd like to add a new table to the database during production on end-user devices, but keep existing data, the designated hook to add new tables, alter the schema or upgrade your data is the onUpgrade method of your SQLiteOpenHelper.

这篇关于“没有这样的表"在 SQLite Android 中发现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆