yuweiguocn / greendaoupgradehelper Goto Github PK
View Code? Open in Web Editor NEWTo solve database upgrade of greenDao and just need one line code
To solve database upgrade of greenDao and just need one line code
Please add support for creating new tables.
At the moment i got all the time error, that temporary table can not create, because the table does not exist.
只要吧数据库生成到3.2就可以了
修改数据库路径怎么实现保存在本地sd卡
DatabaseOpenHelper 有两个升级方法
/**
* Delegates to {@link #onUpgrade(Database, int, int)}, which uses greenDAO's database abstraction.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(wrap(db), oldVersion, newVersion);
}
/**
* Override this if you do not want to depend on {@link SQLiteDatabase}.
*/
public void onUpgrade(Database db, int oldVersion, int newVersion) {
// Do nothing by default
}
加密数据库的升级回掉方法是第二个,而普通的数据库是先调用第一个,然后内部再调用第二个;
加密的Helper 直接选择回掉的是第二种方法,
@OverRide
public void onUpgrade(net.sqlcipher.database.SQLiteDatabase db, int oldVersion, int newVersion) {
DatabaseOpenHelper.this.onUpgrade(wrap(db), oldVersion, newVersion);
}
我也查看了您的源码,发现您在收到SQLiteDatabase类型后也会转化成Database,
所以我觉得可以直接重写第二个回掉,
把migrate(SQLiteDatabase db, Class>... daoClasses)改成
migrate(Database db, Class>... daoClasses)
运行run时,报如下错误,类找不到
Exception in thread "main" java.lang.NoClassDefFoundError: org/greenrobot/greendao/generator/Schema
at com.example.MyClass.main(MyClass.java:9)
Caused by: java.lang.ClassNotFoundException: org.greenrobot.greendao.generator.Schema
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
,
compile 'org.greenrobot:greendao-generator:3.2.0'这个版
Does it support unique() constraint? After upgrade the unique does not work anymore.
private static void createTable(Schema schema) {
Entity user = schema.addEntity("User");
user.addIdProperty().primaryKey();
user.addIntProperty("userID").notNull().unique();
user.addStringProperty("name");
}
com.example W/System.err: java.lang.Exception: MIGRATION HELPER - CLASS DOESN'T MATCH WITH THE CURRENT PARAMETERS - Class: int
05-24 11:42:52.156 com.example W/System.err: at com.github.yuweiguocn.library.greendao.MigrationHelper.getTypeByClass(MigrationHelper.java:184)
05-24 11:42:52.156 com.example W/System.err: at com.github.yuweiguocn.library.greendao.MigrationHelper.generateTempTables(MigrationHelper.java:58)
05-24 11:42:52.156 com.example W/System.err: at com.github.yuweiguocn.library.greendao.MigrationHelper.migrate(MigrationHelper.java:30)
05-24 11:42:52.156 com.example W/System.err: at com.example.helper.MySQLiteOpenHelper.onUpgrade(MySQLiteOpenHelper.java:22)
05-24 11:42:52.156 com.example W/System.err: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
05-24 11:42:52.156 com.example W/System.err: at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
05-24 11:42:52.156 com.example W/System.err: at com.example.MyApplication.onCreate(MyApplication.java:89)
05-24 11:42:52.156 com.example W/System.err: at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1012)
05-24 11:42:52.156 com.example W/System.err: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4553)
05-24 11:42:52.156 com.example W/System.err: at android.app.ActivityThread.access$1500(ActivityThread.java:151)
05-24 11:42:52.156 com.example W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
05-24 11:42:52.156 com.example W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
05-24 11:42:52.156 com.example W/System.err: at android.os.Looper.loop(Looper.java:135)
05-24 11:42:52.156 com.example W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
05-24 11:42:52.156 com.example W/System.err: at java.lang.reflect.Method.invoke(Native Method)
05-24 11:42:52.156 com.example W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
05-24 11:42:52.156 com.example W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-24 11:42:52.156 com.example W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
在原有数据库的基础上,在数据库中增加一张表,改怎么做?
改变MyDaoGenerator的表结构之后,怎么让他重新生成dao文件
android.database.sqlite.SQLiteDatabaseLockedException:
怎么实现数据库降级?
之前版本没有加入这个库,现在新版本加入了这个库,为了以防万一,我把所有的表都写了:
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@OverRide
public void onCreateAllTables(Database db, boolean ifNotExists) {
DaoMaster.createAllTables(db, ifNotExists);
}
@OverRide
public void onDropAllTables(Database db, boolean ifExists) {
DaoMaster.dropAllTables(db, ifExists);
}
}, ADao.class, BDao.class, CDao.class, DDao.class, EDao.class);
其中ADao是新增加的,DDao增加了一个字段,其他Dao的字段跟之前版本一样 没动过, 升级之后 有些表的数据没了,有些还在。
你好,我照着文档把库引入后,我去掉了表的一个字段,在运行,用sqlite命令查看表结构,发现该字段还在,不知道这是什么情况呢
添加这个库之后,运行在Android5.0以下的版本都会报出这个错误:
Error:Execution failed for task ':app:transformClassesWithJarMergingForEduchatDebug'.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: de/greenrobot/dao/AbstractDao.class
查了下生成sql语句的时候,数据库的名字没有加引号,比如我的数据库的名字是select ,没有加引号的时候生成的sql语句就会是select * from select 。我在本地先改了
比如:
int改为Integer
long改为Long
我把原来的int等全改成Integer这种类型格式了,升级后发现所有数据都空了。
I have used `net.zetetic:android-database-sqlcipher:3.5.8@aar'.Would it work well?
博主你好:
gradle下引用加载时,一直引用不到,麻烦看下,报错如下
Failed to resolve: com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.3
我在使用greenDao3.0的时候,使用数据库查询 出现
连接池异常,查询字段居然需要40秒以上,,,The connection pool for database '/data/data/com.kuman.comic/databases/MyData.db' has been unable to grant a connection to thread 9878 (RxIoScheduler-77) with flags 0x2 for 30.000002 seconds.
Connections: 1 active, 0 idle, 0 available.
当升级数据库的时候,原先的表增加了字段之后升级就会报错。
降级好像不行
@yuweiguocn
Please add Log infos to mirgation process.
增加了表字段后运行报错,新增的字段为布尔(boolean )
net.sqlcipher.database.SQLiteConstraintException: NOT NULL constraint failed:
数据过多的话,会不会导致卡顿啊
楼主亲测可用么?
我按照楼主的方法,直接把原数据给我清空了 。。。
@yuweiguocn
Could you please implement to set a dafult value for new columns that are defined as 'notNull'?
固定几个表出现重复数据。
DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
restoreData()方法里面 转移数据的时候,抛出异常,
android.database.sqlite.SQLiteConstraintException: 表.增加的字段 may not be NULL (code 19)
数据转移失败,新表为空 ,temp表 未删除, 如何解决 啊 大神!!!!!
1.method: migrate(Database database, Class>... daoClasses)
error line:ReCreateAllTableListener listener = weakListener.get();
if call this method ,the weaklistenner is null.it only init in the others two overload method.
2.in this method :
if (listener != null) {
listener.onDropAllTables(database, true);
printLog("【Drop all table by listener】");
listener.onCreateAllTables(database, false);
printLog("【Create all table by listener】");
} else {
dropAllTables(database, true, daoClasses);
createAllTables(database, false, daoClasses);
}
but the listener can not be null!because the param access modifiers is NOTNULL of the method migrate's.
so,the greendao can not upgrade using migrate(Database database, Class>... daoClasses)
Could you please wrap migrate() in a transaction and throws a SQLException?
The current code destroys the database when a SQLException is thrown.
compile 'org.greenrobot:greendao:3.2.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.4.0'//数据库升级辅助
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
// 升级、数据库迁移操作
MigrationHelper.migrate(db, ArrayThreeBetBeanDao.class, BetBeanDao.class,
ElevenCFBetBeanDao.class, QuickThreeBetBeanDao.class, WelfareBetBeanDao.class, ArrayFiveBetBeanDao.class, SevenStarBetBeanDao.class,
CqsscBetBeanDao.class, HappyPokerBetBeanDao.class);
}
新增一个表可以,但是在原来的表的基础上加了一个字段就不行了
添加的字段: private String lotteryCode;
报错
android.database.sqlite.SQLiteException: no such column: T.LOTTERY_CODE (code 1): , while compiling: SELECT T."_id",T."PLAYMODE",T."PLAYTYPE",T."DAN",T."TUO",T."BETNUMBS",T."LOTTERY_CODE" FROM "ELEVEN_CFBET_BEAN" T where LOTTERY_CODE = ?
麻烦大神帮忙看看
android.database.sqlite.SQLiteException: no such table: AGREE (code 1): , while compiling: SELECT T."ID",T."AGREE_COUNT" FROM "AGREE" T WHERE T."ID"=?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at de.greenrobot.dao.AbstractDao.load(Unknown Source)
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.1' 这个依赖不上 老哥 出了什么问题呢
经多次测试,在实体中添加int类型新数据的时候,就会报错,无法添加数据库
Hello:
I am using ur code for database migration. I want to included old data in the updated version.
old version: Table (column1, column2)
one record in this table
new version: Table(column1, column2, newColumn)
MySQLiteOpenHelper: same as the one in readMe
@OverRide
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
MigrationHelper.migrate(db,TableDao.class);
}
Code:
MigrationHelper.DEBUG = true;
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "temp",
null);
daoMaster = new DaoMaster(helper.getWritableDatabase());
Exception:
E/SQLiteLog: (1) no such column: T.NEW_COLUMN
Btw, I used greendao-generator to generate to updated version of Table.
Did I use the migration correctly?
Thanks in advance.
Error:(33, 47) 警告: [unchecked] 参数化 vararg 类型Class>的堆可能已受污染
请教个问题,有关数据升级问题,我一直是在onUpgrade通过比对oldVersion来写SQL语句实现,如下:
public class MySQLiteOpenHelper extends DaoMaster.OpenHelper{
private static final String TAG = "MySQLiteOpenHelper";
public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
Log.d(TAG, "数据库需要升级,上一个数据的版本号为=" + oldVersion);
switch (oldVersion) {
case 1:
// 往Student表里增加age字段
db.execSQL("alter table STUDENT add column AGE string");
// 添加User表
UserDao.createTable(db, false);
break;
}
}
}
我想知道你这种方法和我这种方法有什么区别,好处在哪?还有我这种方法有什么弊端?麻烦在百忙之中帮我解惑一下,提前谢谢你了!
大佬你好,我现在引用了你最新的库文件之后,修改表字段,出现了这个问题。
【Failed to restore data from temp table 】BlackList_TEMP
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: BlackList.PKBlackList (code 1555)
下面是我的model类,项目中采用UUID作为唯一区分,没有long类型id,并且转换为byte存到数据库中。不知道问题出现在哪里,求指教。
@entity(nameInDb = "BlackList", createInDb = false)
public class BlackListEntity {
@Property(nameInDb = "PKBlackList")
@Convert(converter = UUID2BytesConverter.class, columnType = byte[].class)
public UUID PKBlackList;
@Property(nameInDb = "PKCompany")
@Convert(converter = UUID2BytesConverter.class, columnType = byte[].class)
public UUID PKCompany;
@Property(nameInDb = "Cellphone")
public String Cellphone;
@Property(nameInDb = "Description")
public String Description;
@Property(nameInDb = "DeleteStatus")
public int DeleteStatus;
@Property(nameInDb = "PKUser")
@Convert(converter = UUID2BytesConverter.class, columnType = byte[].class)
public UUID PKUser;
@Property(nameInDb = "CreateTime")
public String CreateTime;
@Property(nameInDb = "TestValu")
public String TestValu;
@Generated(hash = 465873519)
public BlackListEntity(UUID PKBlackList, UUID PKCompany, String Cellphone, String Description,
int DeleteStatus, UUID PKUser, String CreateTime, String TestValu) {
this.PKBlackList = PKBlackList;
this.PKCompany = PKCompany;
this.Cellphone = Cellphone;
this.Description = Description;
this.DeleteStatus = DeleteStatus;
this.PKUser = PKUser;
this.CreateTime = CreateTime;
this.TestValu = TestValu;
}
@Generated(hash = 1391692307)
public BlackListEntity() {
}
}
下面是我的OpenHelper
@OverRide
public void onUpgrade(Database db, int oldVersion, int newVersion) {
LogUtil.e("MigrationHelper", "from oldVersion:" + oldVersion + "to newVersion:" + newVersion);
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@OverRide
public void onCreateAllTables(Database db, boolean ifNotExists) {
DaoMaster.createAllTables(db, ifNotExists);
}
@OverRide
public void onDropAllTables(Database db, boolean ifExists) {
DaoMaster.dropAllTables(db, ifExists);
}
}, BlackListEntityDao.class);
}
如题,报错信息为旧数据 restore 时,执行 REPLACE INTO * SELECT * 时,SQLite 提示字段值不能为 NULL
07-12 15:44:32.985 2846-2846/com.jinrisheng.yinyuehui E/MigrationHelper: 【Failed to restore data from temp table 】MUSIC_TEMP
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: MUSIC._id (code 1555)
at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:732)
at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1676)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at org.greenrobot.a.d.f.a(StandardDatabase.java:37)
at com.jinrisheng.yinyuehui.util.b.b.c(MigrationHelper.java:180)
restoreData的时候应该用 ` 把列名括起来
Hi,
It is possible that the _TEMP tables are not cleanly removed.
Adding
db.execSQL("DROP TABLE IF EXISTS `" + tempTableName + "`;");
to the generateTempTables(...) method resolves this issue, but not sure it is the correct fix.
E/SQLiteLog: (1) table MESSAGE_ITEM_TEMP already exists
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: a.b.c, PID: 9864
java.lang.RuntimeException: Unable to create application com.android.tools.fd.runtime.BootstrapApplication: android.database.sqlite.SQLiteException: table MESSAGE_ITEM_TEMP already exists (code 1): , while compiling: CREATE TEMPORARY TABLE MESSAGE_ITEM_TEMP AS SELECT * FROM MESSAGE_ITEM;
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5364)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.database.sqlite.SQLiteException: table MESSAGE_ITEM_TEMP already exists (code 1): , while compiling: CREATE TEMPORARY TABLE MESSAGE_ITEM_TEMP AS SELECT * FROM MESSAGE_ITEM;
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:990)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:961)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:594)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1675)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1606)
at org.greenrobot.greendao.database.StandardDatabase.execSQL(StandardDatabase.java:37)
at com.github.yuweiguocn.library.greendao.MigrationHelper.generateTempTables(MigrationHelper.java:60)
at com.github.yuweiguocn.library.greendao.MigrationHelper.migrate(MigrationHelper.java:36)
at a.b.c.utils.MySQLiteOpenHelper.onUpgrade(MySQLiteOpenHelper.java:30)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at a.b.c.api.RetrofitController.onCreate(RetrofitController.java:72)
at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:370)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1025)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5361)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Also, if a new database version creates a new database table, then it fails with the following stack trace:
FATAL EXCEPTION: mainProcess: a.b.c.dev, PID: 10520java.lang.RuntimeException: Unable to create application com.android.tools.fd.runtime.BootstrapApplication: android.database.sqlite.SQLiteException: no such table: CHECK_LIST (code 1): , while compiling: CREATE TEMPORARY TABLE CHECK_LIST_TEMP AS SELECT * FROM CHECK_LIST;#################################################################Error Code : 1 (SQLITE_ERROR)Caused By : SQL(query) error or missing database. (no such table: CHECK_LIST (code 1): , while compiling: CREATE TEMPORARY TABLE CHECK_LIST_TEMP AS SELECT * FROM CHECK_LIST#################################################################at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6057)at android.app.ActivityThread.access$1700(ActivityThread.java:218)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1805)at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:145)at android.app.ActivityThread.main(ActivityThread.java:7007)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)Caused by: android.database.sqlite.SQLiteException: no such table: CHECK_LIST (code 1): , while compiling: CREATE TEMPORARY TABLE CHECK_LIST_TEMP AS SELECT * FROM CHECK_LIST;#################################################################
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
at com.github.yuweiguocn.library.greendao.MigrationHelper.migrate(MigrationHelper.java:62)
我没修改任何东西,只升级了schemaVersion,但是你的这个会把我的order表内容全部删除掉,其它表的内容不删除,我感觉应该是我的order表名与sql语句里面order关键字冲突了,希望你能查找到原因后升一版本
你好,题目描述的问题,我测试是要的,但不是十分确定。是否需要增加
String sql = "alter table OFF_LINE_ORDER add uploadSuccessTime long default 0";
db.execSQL(sql);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.