sqlite android no such table,“No Such Table” Error found in SQLite Android

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

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.

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 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...).

回答1:

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.

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.

回答2:

I have written a ORM framework for that. 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).

It even creates your table automatically and you just say book1.insert(), done.

回答3:

You must uninstall the applcation and then reinstall it. It should work after that.

回答4:

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;

}

}

回答5:

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.

回答6:

I had this problem, but cleaning the project did not fixed it.

It turned out I passed DATABASE_NAME instead of TABLE_NAME.

回答7:

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.

回答8:

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)"

);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值