Java 在 SQLite Android 中发现“没有这样的表”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6554269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"No Such Table" Error found in SQLite Android
提问by Mark D
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.
我正在尝试了解 SQLite 数据库,但我真的很讨厌处理任何后端的东西,充满热情。我已经遇到了一个看似简单的问题。
Here is the code that I think matters from the DatabaseHelper
class
这是我认为在DatabaseHelper
课堂上很重要的代码
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 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...).
Eclipse 告诉我方法中有错误insertBook()
。它说有no such table books: ...
。我不知道我在这里做错了什么。更令人沮丧的是,在它完美运行之前只有几分钟,然后(我认为)我删除了表,它只是出于任何原因再次创建它,即使此代码自我第一次创建以来没有改变(我认为...)。
采纳答案by Thilo-Alexander Ginkel
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.
您的设备上有一个旧版本的数据库,其中有(空)数据库,但没有 books 表。如果这是您的选择,只需卸载并重新安装该应用程序。
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
.
稍后,当您想在最终用户设备上的生产过程中向数据库添加新表,但保留现有数据时,添加新表、更改架构或升级数据的指定挂钩是onUpgrade
您的SQLiteOpenHelper
.
回答by AhmetB - Google
I have written a ORM framework for that. https://github.com/ahmetalpbalkan/orman
我为此编写了一个 ORM 框架。https://github.com/ahmetalpbalkan/orman
You can easily write Android applications using SQLite with that. It uses your Java classes (Book, in this case) as database tables (entities).
您可以使用 SQLite 轻松编写 Android 应用程序。它使用您的 Java 类(在本例中为 Book)作为数据库表(实体)。
It even creates your table automatically and you just say book1.insert()
, done.
它甚至会自动创建您的表格,您只需说book1.insert()
,完成。
回答by Matthieu
If for some reason, you dropped the table, you might want to delete the database to force the application to recreate it correctly. Use adb shell
and find the database in /data/data/[package_name]/databases/
. You can just delete the file.
如果由于某种原因删除了表,您可能希望删除数据库以强制应用程序正确地重新创建它。使用adb shell
并在数据库中找到/data/data/[package_name]/databases/
。您可以删除该文件。
回答by Faraz Ahmed
You must uninstall the applcation and then reinstall it. It should work after that.
您必须卸载该应用程序,然后重新安装它。在那之后它应该工作。
回答by BlackHawk
db.execSQL(
"CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, isbn TEXT)"
);
remove space after the table name, it should be like this.
去掉表名后面的空格,应该是这样的。
db.execSQL(
"CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, isbn TEXT)"
);
回答by Ali Ziaee
try this one for example for insert:
试试这个,例如插入:
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);
***try
{
db.insert(TABLE_NAME, null, cv);
}
catch ( SQLiteException e)
{
onCreate(db);
db.insert(TABLE_NAME, null, cv);
}***
db.close();
return true;
} catch (Exception exp) {
exp.printStackTrace();
return false;
}
}
回答by Destiner
I had this problem, but cleaning the project did not fixed it.
It turned out I passed DATABASE_NAME
instead of TABLE_NAME
.
我有这个问题,但清理项目并没有解决它。
结果我通过DATABASE_NAME
而不是TABLE_NAME
.
回答by Manish Bansal
Make sure that onCreate
method called. I am also facing similar type of problem when creating multiple table. If you create a separate class make sure you first clear all the storage data of your app and then again run it ..It will work fine for me.
确保该onCreate
方法被调用。创建多个表时,我也面临类似类型的问题。如果您创建一个单独的类,请确保首先清除应用程序的所有存储数据,然后再次运行它..它对我来说很好用。
回答by Sankar
Note:
笔记:
External Database You Access Time must add for the path in Sqliteopenhelper class
您访问时间必须为Sqliteopenhelper 类中的路径添加外部数据库
public final static String DATABASE_PATH = "/data/data/com.example.shortcuts/databases/";
public final static String DATABASE_PATH = "/data/data/com.example.shortcuts/databases/";
com.example.shortcuts=Your Package name
com.example.shortcuts=你的包名
回答by Sajid khan
Nothing about this made any sense, as the table existed from the very beginning of the DB, so we tried different things:
这没有任何意义,因为该表从数据库的一开始就存在,所以我们尝试了不同的方法:
uninstalling app
claning cache / data
changing database version number
forcing to copy the database even if it already existed
卸载应用
克隆缓存/数据
更改数据库版本号
强制复制数据库,即使它已经存在