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

SQLiteException: no such table

or

SQLiteException: no such column

错误。为什么?

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


当前回答

当需要创建表时,第一次调用onCreate。我们需要重写这个方法,在这里我们编写了由SQLiteDatabase执行的表创建脚本。execSQL方法。在第一次部署中执行后,此方法将不再被调用。

onUpgrade 当数据库版本升级时调用此方法。假设第一次部署时,数据库版本为1,在第二次部署时,数据库结构发生了变化,例如在表中增加了额外的列。假设数据库版本现在是2。

其他回答

没有这样的表,主要是当你没有打开SQLiteOpenHelper类与getwritabledata(),在此之前,你还必须调用make构造函数与databasename & version。 当SQLiteOpenHelper类中给出的版本号中有升级值时,OnUpgrade将被调用。

下面是代码片段(没有找到这样的列可能是因为列名中的拼写):

public class database_db {
    entry_data endb;
    String file_name="Record.db";
    SQLiteDatabase sq;
    public database_db(Context c)
    {
        endb=new entry_data(c, file_name, null, 8);
    }
    public database_db open()
    {
        sq=endb.getWritableDatabase();
        return this;
    }
    public Cursor getdata(String table)
    {
        return sq.query(table, null, null, null, null, null, null);
    }
    public long insert_data(String table,ContentValues value)
    {
        return sq.insert(table, null, value);
    }
    public void close()
    {
        sq.close();
    }
    public void delete(String table)
    {
        sq.delete(table,null,null);
    }
}
class entry_data extends SQLiteOpenHelper
{

    public entry_data(Context context, String name, SQLiteDatabase.CursorFactory factory,
                      int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase sqdb) {
        // TODO Auto-generated method stub

        sqdb.execSQL("CREATE TABLE IF NOT EXISTS 'YOUR_TABLE_NAME'(Column_1 text not null,Column_2 text not null);");

    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          onCreate(db);
    }

}

SQLiteOpenHelper onCreate()和onUpgrade()回调在数据库实际打开时被调用,例如通过调用getWritableDatabase()。在创建数据库帮助对象本身时,不会打开数据库。

SQLiteOpenHelper数据库文件的版本。版本号是传递给构造函数的int参数。在数据库文件中,版本号存储在PRAGMA user_version中。

onCreate()仅在数据库文件不存在且刚刚创建时运行。如果onCreate()成功返回(不抛出异常),则假定数据库是使用所请求的版本号创建的。作为暗示,你不应该自己在onCreate()中捕获SQLExceptions。

onUpgrade()仅在数据库文件存在但存储的版本号低于构造函数中请求的版本号时调用。onUpgrade()应该将表模式更新到请求的版本。

当在代码(onCreate())中更改表模式时,应该确保数据库已更新。两种主要方法:

Delete the old database file so that onCreate() is run again. This is often preferred at development time where you have control over the installed versions and data loss is not an issue. Some ways to delete the database file: Uninstall the application. Use the application manager or adb uninstall your.package.name from the shell. Clear application data. Use the application manager. Increment the database version so that onUpgrade() is invoked. This is slightly more complicated as more code is needed. For development time schema upgrades where data loss is not an issue, you can just use execSQL("DROP TABLE IF EXISTS <tablename>") in to remove your existing tables and call onCreate() to recreate the database. For released versions, you should implement data migration in onUpgrade() so your users don't lose their data.

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

数据库名必须以.db结尾,查询字符串必须有结束符(;)

从模拟器或设备卸载应用程序。再次运行应用程序。(OnCreate()不执行时,数据库已经存在)