我已经在我的SQLiteOpenHelper onCreate()中创建了我的表
SQLiteException: no such table
or
SQLiteException: no such column
错误。为什么?
注意: (这是每周数十个类似问题的汇总。试图在这里提供一个“规范的”社区wiki问题/答案,以便所有这些问题都可以指向一个很好的参考。)
我已经在我的SQLiteOpenHelper onCreate()中创建了我的表
SQLiteException: no such table
or
SQLiteException: no such column
错误。为什么?
注意: (这是每周数十个类似问题的汇总。试图在这里提供一个“规范的”社区wiki问题/答案,以便所有这些问题都可以指向一个很好的参考。)
当前回答
您可以创建数据库和表
public class DbHelper extends SQLiteOpenHelper {
private static final String DBNAME = "testdatbase.db";
private static final int VERSION = 1;
public DbHelper(Context context) {
super(context, DBNAME, null, VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table BookDb(id integer primary key autoincrement,BookName text,Author text,IssuedOn text,DueDate text,Fine text,Totalfine text");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS BookDb");
onCreate(db);
}
}
注意:如果您想创建另一个表或添加列或没有这样的表,只需增加VERSION
其他回答
Sqliteopenhelper的方法有create方法和upgrade方法,create方法在任何表第一次创建时使用,而upgrade方法将在每次表的列数发生变化时调用。
Sqlite数据库覆盖两种方法
1) onCreate (): 该方法仅在应用程序第一次启动时调用一次。所以它只调用一次
2) onUpgrade () 当我们改变数据库版本时,这个方法被调用,然后这个方法被调用。它用于改变表结构,如在创建DB Schema后添加新列
扩展SQLiteOpenHelper时要记住的要点
super(context, DBName, null, DBversion); - This should be invoked first line of constructor override onCreate and onUpgrade (if needed) onCreate will be invoked only when getWritableDatabase() or getReadableDatabase() is executed. And this will only invoked once when a DBName specified in the first step is not available. You can add create table query on onCreate method Whenever you want to add new table just change DBversion and do the queries in onUpgrade table or simply uninstall then install the app.
数据库名必须以.db结尾,查询字符串必须有结束符(;)
在DatabaseHandler/DatabaseManager类中重新检查您的查询(您曾经使用过的类)