greenDAO icon indicating copy to clipboard operation
greenDAO copied to clipboard

Caused by: android.database.sqlite.SQLiteException: table "DRAFT_ENTITY" already exists (Sqlite code 1 SQLITE_ERROR), (OS error - 11:Try again)

Open parcool opened this issue 5 years ago • 9 comments

device: any description: It crash when I run my app,I don't know how to reproduced the bug,I just run it and reinstall it by android studio in default run way. sometime it will be appear like a boom. usage:

//init it in my application
private void initGreenDao() {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(getApplicationContext(), "songche.db", null);
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
        daoSession = daoMaster.newSession();
    }
//this is the entity:
@Entity
public class DraftEntity {

    @Id(autoincrement = true)
    private Long id;
    @NotNull
    private Integer type;
    private String cover;
    private String title;
    private String videoPath;
    @ToMany(referencedJoinProperty = "did")
    private List<TagModel> tags;
    @ToMany(referencedJoinProperty = "didImageAndDescription")
    private List<ImagesAndDescriptionEntity> imagesAndDescription;
    private long createTime;
    @Transient
    private boolean isOpen;
    /** Used to resolve relations */
    @Generated(hash = 2040040024)
    private transient DaoSession daoSession;
    /** Used for active entity operations. */
    @Generated(hash = 615438973)
    private transient DraftEntityDao myDao;
    @Generated(hash = 865212059)
    public DraftEntity(Long id, @NotNull Integer type, String cover, String title,
            String videoPath, long createTime) {
        this.id = id;
        this.type = type;
        this.cover = cover;
        this.title = title;
        this.videoPath = videoPath;
        this.createTime = createTime;
    }
    @Generated(hash = 165234677)
    public DraftEntity() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Integer getType() {
        return this.type;
    }
    public void setType(Integer type) {
        this.type = type;
    }
    public String getCover() {
        return this.cover;
    }
    public void setCover(String cover) {
        this.cover = cover;
    }
    public String getTitle() {
        return this.title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getVideoPath() {
        return this.videoPath;
    }
    public void setVideoPath(String videoPath) {
        this.videoPath = videoPath;
    }
    public long getCreateTime() {
        return this.createTime;
    }
    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public void setTags(List<TagModel> tags) {
        this.tags = tags;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setOpen(boolean open) {
        isOpen = open;
    }

    public void setImagesAndDescription(List<ImagesAndDescriptionEntity> imagesAndDescription) {
        this.imagesAndDescription = imagesAndDescription;
    }

    /**
     * To-many relationship, resolved on first access (and after reset).
     * Changes to to-many relations are not persisted, make changes to the target entity.
     */
    @Generated(hash = 333099023)
    public List<TagModel> getTags() {
        if (tags == null) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            TagModelDao targetDao = daoSession.getTagModelDao();
            List<TagModel> tagsNew = targetDao._queryDraftEntity_Tags(id);
            synchronized (this) {
                if (tags == null) {
                    tags = tagsNew;
                }
            }
        }
        return tags;
    }
    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    @Generated(hash = 404234)
    public synchronized void resetTags() {
        tags = null;
    }
    /**
     * To-many relationship, resolved on first access (and after reset).
     * Changes to to-many relations are not persisted, make changes to the target entity.
     */
    @Generated(hash = 2035187950)
    public List<ImagesAndDescriptionEntity> getImagesAndDescription() {
        if (imagesAndDescription == null) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            ImagesAndDescriptionEntityDao targetDao = daoSession
                    .getImagesAndDescriptionEntityDao();
            List<ImagesAndDescriptionEntity> imagesAndDescriptionNew = targetDao
                    ._queryDraftEntity_ImagesAndDescription(id);
            synchronized (this) {
                if (imagesAndDescription == null) {
                    imagesAndDescription = imagesAndDescriptionNew;
                }
            }
        }
        return imagesAndDescription;
    }
    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    @Generated(hash = 874194813)
    public synchronized void resetImagesAndDescription() {
        imagesAndDescription = null;
    }
    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 128553479)
    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.delete(this);
    }
    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 1942392019)
    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.refresh(this);
    }
    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 713229351)
    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.update(this);
    }
    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 1500108030)
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getDraftEntityDao() : null;
    }
    

}

log info:

java.lang.RuntimeException: Unable to create application com.songcw.customer.application.SongCheApp: android.database.sqlite.SQLiteException: table &quot;DRAFT_ENTITY&quot; already exists (Sqlite code 1 SQLITE_ERROR), (OS error - 11:Try again)
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6586)
	at android.app.ActivityThread.access$1900(ActivityThread.java:267)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1963)
	at android.os.Handler.dispatchMessage(Handler.java:109)
	at android.os.Looper.loop(Looper.java:207)
	at android.app.ActivityThread.main(ActivityThread.java:7470)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: android.database.sqlite.SQLiteException: table &quot;DRAFT_ENTITY&quot; already exists (Sqlite code 1 SQLITE_ERROR), (OS error - 11:Try again)
	at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
	at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:793)
	at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:769)
	at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
	at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2082)
	at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:2003)
	at org.greenrobot.greendao.database.StandardDatabase.execSQL(StandardDatabase.java:37)
	at com.songcw.customer.home.mvp.model.DraftEntityDao.createTable(DraftEntityDao.java:48)
	at com.songcw.customer.home.mvp.model.DaoMaster.createAllTables(DaoMaster.java:24)
	at com.songcw.customer.home.mvp.model.DaoMaster$OpenHelper.onCreate(DaoMaster.java:83)
	at org.greenrobot.greendao.database.DatabaseOpenHelper.onCreate(DatabaseOpenHelper.java:79)
	at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:412)
	at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:313)
	at org.greenrobot.greendao.database.DatabaseOpenHelper.getWritableDb(DatabaseOpenHelper.java:59)
	at com.songcw.customer.application.SongCheApp.initGreenDao(SongCheApp.java:90)
	at com.songcw.customer.application.SongCheApp.onCreate(SongCheApp.java:67)
	at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1162)
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6566)
	... 8 more
android.database.sqlite.SQLiteException: table &quot;DRAFT_ENTITY&quot; already exists (Sqlite code 1 SQLITE_ERROR), (OS error - 11:Try again)
	at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
	at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:793)
	at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:769)
	at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
	at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2082)
	at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:2003)
	at org.greenrobot.greendao.database.StandardDatabase.execSQL(StandardDatabase.java:37)
	at com.songcw.customer.home.mvp.model.DraftEntityDao.createTable(DraftEntityDao.java:48)
	at com.songcw.customer.home.mvp.model.DaoMaster.createAllTables(DaoMaster.java:24)
	at com.songcw.customer.home.mvp.model.DaoMaster$OpenHelper.onCreate(DaoMaster.java:83)
	at org.greenrobot.greendao.database.DatabaseOpenHelper.onCreate(DatabaseOpenHelper.java:79)
	at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:412)
	at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:313)
	at org.greenrobot.greendao.database.DatabaseOpenHelper.getWritableDb(DatabaseOpenHelper.java:59)
	at com.songcw.customer.application.SongCheApp.initGreenDao(SongCheApp.java:90)
	at com.songcw.customer.application.SongCheApp.onCreate(SongCheApp.java:67)
	at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1162)
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6566)
	at android.app.ActivityThread.access$1900(ActivityThread.java:267)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1963)
	at android.os.Handler.dispatchMessage(Handler.java:109)
	at android.os.Looper.loop(Looper.java:207)
	at android.app.ActivityThread.main(ActivityThread.java:7470)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)

parcool avatar Mar 11 '19 08:03 parcool

and I got this log info on third-platform,does it cause by same reason?

java.lang.RuntimeException: Unable to create application com.songcw.customer.application.SongCheApp: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4998)
	at android.app.ActivityThread.access$1800(ActivityThread.java:170)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1555)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:179)
	at android.app.ActivityThread.main(ActivityThread.java:5769)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
Caused by: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
	at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
	at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
	at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:632)
	at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:318)
	at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:292)
	at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:213)
	at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
	at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
	at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
	at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
	at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
	at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
	at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
	at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:594)
	at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:280)
	at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
	at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
	at org.greenrobot.greendao.database.DatabaseOpenHelper.getWritableDb(DatabaseOpenHelper.java:59)
	at com.songcw.customer.application.SongCheApp.initGreenDao(SongCheApp.java:90)
	at com.songcw.customer.application.SongCheApp.onCreate(SongCheApp.java:67)
	at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1014)
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4995)
	... 8 more
android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
	at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
	at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
	at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:632)
	at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:318)
	at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:292)
	at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:213)
	at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
	at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
	at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
	at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
	at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
	at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
	at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
	at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:594)
	at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:280)
	at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
	at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
	at org.greenrobot.greendao.database.DatabaseOpenHelper.getWritableDb(DatabaseOpenHelper.java:59)
	at com.songcw.customer.application.SongCheApp.initGreenDao(SongCheApp.java:90)
	at com.songcw.customer.application.SongCheApp.onCreate(SongCheApp.java:67)
	at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1014)
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4995)
	at android.app.ActivityThread.access$1800(ActivityThread.java:170)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1555)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:179)
	at android.app.ActivityThread.main(ActivityThread.java:5769)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)

parcool avatar Mar 11 '19 09:03 parcool

@parcool 我也遇到同样问题,你怎么解决的

xyStephenZhou avatar Jun 24 '19 02:06 xyStephenZhou

@parcool 你的解决了吗

xingjianlong avatar Jun 28 '19 08:06 xingjianlong

我感觉是编译器的问题,有时候我手机里卸载了,然后运行就报错,有时又没问题------------------ 原始邮件 ------------------ 发件人: "XingXiaoye"[email protected] 发送时间: 2019年6月28日(星期五) 下午4:24 收件人: "greenrobot/greenDAO"[email protected]; 抄送: "zhoudaxia1990"[email protected];"Comment"[email protected]; 主题: Re: [greenrobot/greenDAO] Caused by:android.database.sqlite.SQLiteException: table "DRAFT_ENTITY" already exists(Sqlite code 1 SQLITE_ERROR), (OS error - 11:Try again) (#952)

@parcool 你的解决了吗

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

xyStephenZhou avatar Jun 28 '19 08:06 xyStephenZhou

No,I forgot it. as you known there is no anwser from developer of this project.

parcool avatar Jun 29 '19 06:06 parcool

some one can help?

sven-brobonds avatar Oct 12 '19 04:10 sven-brobonds

The errors database is locked (code 5): , while compiling: PRAGMA journal_mode and table &quot;DRAFT_ENTITY&quot; already exists (Sqlite code 1 SQLITE_ERROR), (OS error - 11:Try again) indicate that the database is opened multiple times, which leads to those issues. Maybe check that you open the database only once (singleton, synchronized) and only using a single process.

greenrobot-team avatar Oct 14 '19 08:10 greenrobot-team

Only Xiaomi's Android 9 was tested with the problem

booyoungchen avatar Nov 02 '21 09:11 booyoungchen

SQLite before 3.7.0 cannot identify database files with WAL enabled because Write-ahead Log (WAL) is enabled in Android 9.0. My solution is as follows:

DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "xxx.db", null); helper.setWriteAheadLoggingEnabled(false);//Add this line to disable WAL mode SQLiteDatabase db = helper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession();

I hope the above methods can also solve your problem, wish you good luck!

booyoungchen avatar Nov 03 '21 03:11 booyoungchen