GreenDao- android.database.sqlite.SQLiteException:table TAREAS没有名为

时间:2016-03-30 12:04:36

标签: android database sqlite greendao

我正在尝试在我的桌子Tareas中插入数据。我,我使用ORM(GreenDao)。但是当运行我的应用程序时,日志猫显示没有名为Tareas的表。我不明白这一点,因为当我创建了我的数据库时,Tareas Table有一个名为Tipo的列。

  

03-30 07:51:32.758 29389-29389 / tipiwiny.greendao E / AndroidRuntime:FATAL EXCEPTION:main进程:tipiwiny.greendao,PID:29389 android.database.sqlite.SQLiteException:table TAREAS没有列

     

命名为TIPO(代码1):,同时编译:INSERT INTO TAREAS   ('_id','DURACION','TIPO','DESCRIPCION','USUARIO','REALIZADA')价值观   (?,?,?,?,?,?) 在   android.database.sqlite.SQLiteConnection.nativePrepareStatement(母语   方法)at   android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)                                                                              在   android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)                                                                              在   android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)                                                                              在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)                                                                              在   android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)                                                                              在   android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:994)                                                                              在   de.greenrobot.dao.internal.TableStatements.getInsertStatement(TableStatements.java:48)                                                                              at de.greenrobot.dao.AbstractDao.insert(AbstractDao.java:293)                                                                              attipiwiny.greendao.CrearTarea.añadirTarea(CrearTarea.java:60)                                                                              在tipiwiny.greendao.CrearTarea.access $ 000(CrearTarea.java:18)                                                                              at tipiwiny.greendao.CrearTarea $ 1.onClick(CrearTarea.java:42)                                                                              在android.view.View.performClick(View.java:4756)                                                                              在android.view.View $ PerformClick.run(View.java:19749)                                                                              在android.os.Handler.handleCallback(Handler.java:739)                                                                              在android.os.Handler.dispatchMessage(Handler.java:95)                                                                              在android.os.Looper.loop(Looper.java:135)                                                                              在android.app.ActivityThread.main(ActivityThread.java:5221)                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                              在java.lang.reflect.Method.invoke(Method.java:372)                                                                              atcom.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我的数据库定义如下:

 private static void createDatabase(Schema schema) {
    Entity usuario = schema.addEntity("Usuario");
    usuario.addIdProperty();
    usuario.addStringProperty("nombre").notNull();
    usuario.addStringProperty("apellidos").notNull();
    usuario.addStringProperty("password").notNull();
    usuario.addStringProperty("roll").notNull();

    Entity habilidad = schema.addEntity("Habilidad");
    habilidad.addIdProperty();
    Property usuarioIdProperty = habilidad.addLongProperty("usuarioId").notNull().getProperty();
    habilidad.addToOne(usuario, usuarioIdProperty);
    habilidad.addIntProperty("tipo").notNull();

    Entity tarea=schema.addEntity("Tareas");
    tarea.addIdProperty();

    tarea.addIntProperty("duracion").notNull();
    tarea.addIntProperty("tipo").notNull();
    tarea.addStringProperty("descripcion").notNull();
    tarea.addStringProperty("usuario");
    tarea.addBooleanProperty("realizada");


}

当您执行此代码以生成表时,将创建类TareasDao并且方法createTable为:

  /** Creates the underlying database table. */
public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
    String constraint = ifNotExists? "IF NOT EXISTS ": "";
    db.execSQL("CREATE TABLE " + constraint + "'TAREAS' (" + //
            "'_id' INTEGER PRIMARY KEY ," + // 0: id
            "'DURACION' INTEGER NOT NULL ," + // 1: duracion
            "'TIPO' INTEGER NOT NULL ," + // 2: tipo
            "'DESCRIPCION' TEXT NOT NULL ," + // 3: descripcion
            "'USUARIO' TEXT," + // 4: usuario
            "'REALIZADA' INTEGER);"); // 5: realizada
}

1 个答案:

答案 0 :(得分:3)

尝试卸载并重新安装该应用。

详细信息:

一个可能的原因可能是您先创建了表,运行了迁移,然后再添加了字段。

所以可能有两种可能的解决方案: -

1:从手机中删除应用程序(卸载),它将删除所有表格。然后重新安装将重新创建所有表。

2:更改DaoMaster中的onUpdate方法以删除所有表并重新创建它们。

/** WARNING: Drops all table on Upgrade! Use only during development. */
public static class DevOpenHelper extends OpenHelper {
    public DevOpenHelper(Context context, String name, CursorFactory factory) {
        super(context, name, factory);
        Log.i("greenDAO", "DevOpenHelper: constructor called");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
        //Remember to remove this line before moving to production.
       dropAllTables(db,true); 
       onCreate(db);
    }
}

如果您需要任何帮助,请告诉我们。)