android sqlite database is locked (code 5): , while compiling:....

    xiaoxiao2023-10-12  172

    安卓开发时用到了sqlite数据库, 到网上复制了点例子,结果反复运行的时候出现异常:database is locked (code 5): , while compiling:....

    到网上查都是一堆的代码,看着就头晕

    最后解决:

    执行sql后关闭事务,再关闭数据库即可。

    部分代码:

    private ProfileDBHelper profileDBHelper; public ProfileDAO(Context context){ this.context = context; profileDBHelper = new ProfileDBHelper(context); } public void save(String brand, String type){ SQLiteDatabase db = profileDBHelper.getWritableDatabase(); db.beginTransaction(); String sql = "insert into " + ProfileDBHelper.TABLE_NAME + "(id, brand, type)" + " values(null, '" + brand + "','" + type + "')"; db.execSQL(sql); db.setTransactionSuccessful(); db.endTransaction(); // db.close(); // }

     

    最新回复(0)