GreenDao- android.database.sqlite.SQLiteException: table TAREAS 没有名为
GreenDao- android.database.sqlite.SQLiteException: table TAREAS has no column named
我正在尝试在我的 table Tareas 中插入数据。我,我正在使用 ORM (GreenDao)。但是当 运行 我的应用程序时,日志猫显示不存在 table 命名为 Tareas。我不明白这一点,因为当我创建数据库时,Tareas Table 有一个名为 Tipo 的列。
03-30 07:51:32.758 29389-29389/tipiwiny.greendao E/AndroidRuntime: FATAL EXCEPTION: main Process: tipiwiny.greendao, PID: 29389 android.database.sqlite.SQLiteException: table TAREAS has no column
named TIPO (code 1): , while compiling: INSERT INTO TAREAS
('_id','DURACION','TIPO','DESCRIPCION','USUARIO','REALIZADA') VALUES
(?,?,?,?,?,?) at
android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
Method) at
android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at
android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at
android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
at
android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
at
android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:994)
at
de.greenrobot.dao.internal.TableStatements.getInsertStatement(TableStatements.java:48)
at de.greenrobot.dao.AbstractDao.insert(AbstractDao.java:293)
at tipiwiny.greendao.CrearTarea.añadirTarea(CrearTarea.java:60)
at tipiwiny.greendao.CrearTarea.access[=14=]0(CrearTarea.java:18)
at tipiwiny.greendao.CrearTarea.onClick(CrearTarea.java:42)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at 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");
}
执行此代码生成tables时,会创建class 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
}
尝试卸载并重新安装该应用程序。
详情:
一个可能的原因可能是您首先创建了 table,运行 迁移,然后添加了字段。
所以可能有两种可能的解决方案:-
1 : 从您的 phone 中删除该应用程序(卸载),它会删除所有 table。然后重新安装将重新创建所有 tables.
2 : 在 DaoMaster 中更改 onUpdate
方法以删除所有 table 并重新创建它们。
/** 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);
}
}
如果您需要任何帮助,请告诉我:)
我正在尝试在我的 table Tareas 中插入数据。我,我正在使用 ORM (GreenDao)。但是当 运行 我的应用程序时,日志猫显示不存在 table 命名为 Tareas。我不明白这一点,因为当我创建数据库时,Tareas Table 有一个名为 Tipo 的列。
03-30 07:51:32.758 29389-29389/tipiwiny.greendao E/AndroidRuntime: FATAL EXCEPTION: main Process: tipiwiny.greendao, PID: 29389 android.database.sqlite.SQLiteException: table TAREAS has no column
named TIPO (code 1): , while compiling: INSERT INTO TAREAS ('_id','DURACION','TIPO','DESCRIPCION','USUARIO','REALIZADA') VALUES (?,?,?,?,?,?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:994) at de.greenrobot.dao.internal.TableStatements.getInsertStatement(TableStatements.java:48) at de.greenrobot.dao.AbstractDao.insert(AbstractDao.java:293) at tipiwiny.greendao.CrearTarea.añadirTarea(CrearTarea.java:60) at tipiwiny.greendao.CrearTarea.access[=14=]0(CrearTarea.java:18) at tipiwiny.greendao.CrearTarea.onClick(CrearTarea.java:42) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at 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");
}
执行此代码生成tables时,会创建class 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
}
尝试卸载并重新安装该应用程序。
详情:
一个可能的原因可能是您首先创建了 table,运行 迁移,然后添加了字段。
所以可能有两种可能的解决方案:-
1 : 从您的 phone 中删除该应用程序(卸载),它会删除所有 table。然后重新安装将重新创建所有 tables.
2 : 在 DaoMaster 中更改 onUpdate
方法以删除所有 table 并重新创建它们。
/** 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);
}
}
如果您需要任何帮助,请告诉我:)