我已经在我的SQLiteOpenHelper onCreate()中创建了我的表

SQLiteException: no such table

or

SQLiteException: no such column

错误。为什么?

注意: (这是每周数十个类似问题的汇总。试图在这里提供一个“规范的”社区wiki问题/答案,以便所有这些问题都可以指向一个很好的参考。)


当前回答

onCreate ()

When we create DataBase at a first time (i.e Database is not exists) onCreate() create database with version which is passed in SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) onCreate() method is creating the tables you’ve defined and executing any other code you’ve written. However, this method will only be called if the SQLite file is missing in your app’s data directory (/data/data/your.apps.classpath/databases). This method will not be called if you’ve changed your code and relaunched in the emulator. If you want onCreate() to run you need to use adb to delete the SQLite database file.

onUpgrade()

SQLiteOpenHelper应该调用超级构造函数。 onUpgrade()方法只会在版本整数大于应用程序中运行的当前版本时被调用。 如果希望调用onUpgrade()方法,则需要增加代码中的版本号。

其他回答

在DatabaseHandler/DatabaseManager类中重新检查您的查询(您曾经使用过的类)

Sqliteopenhelper的方法有create方法和upgrade方法,create方法在任何表第一次创建时使用,而upgrade方法将在每次表的列数发生变化时调用。

如果你忘记提供一个“name”字符串作为构造函数的第二个参数,它会创建一个“内存中”数据库,当你关闭应用程序时,这个数据库会被擦除。

您可以创建数据库和表

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

也许我太晚了,但我想分享我简短而甜蜜的答案。 请检查答案相同的问题。它肯定会对你有帮助。没有更深层次的规范。

如果你对创建表的语法有信心,那么当你在同一个表中添加新列时,可能会发生这种情况,因为…

1)从设备上卸载并重新运行。

OR

2)设置—> app—> ClearData

OR

3)在DatabaseHandler类中修改DATABASE_VERSION(如果你添加了新列,它会自动升级)

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

OR

4)在“DatabaseHandler”类中更改DATABASE_NAME(我也面临同样的问题。但是我通过改变DATABASE_NAME成功了。)