错误日志
11-21 05:23:49.654 3793-3793/com.example.administrator.androiddemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.administrator.androiddemo, PID: 3793
android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:360)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:254)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at org.litepal.tablemanager.Connector.getWritableDatabase(Connector.java:58)
at org.litepal.tablemanager.Connector.getDatabase(Connector.java:81)
at org.litepal.LitePal.getDatabase(LitePal.java:62)
at com.example.administrator.androiddemo.MainActivity$1.onClick(MainActivity.java:21)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
原因
由于后面对sqllite数据库修改了一次,使得数据库的版本号变成了2。
但是现在我又要创建相同名字的数据库,但是定义的数据库版本是1,所以产生错误了。
解决
修改sqlite数据库的版本。
<?xml version="1.0" encoding="utf-8" ?>
<litepal>
<!--数据库名-->
<dbname value="BookStore"/>
<!--数据库版本号-->
<version value="3"/>
<!--映射模型-->
<list>
<mapping class="com.example.administrator.androiddemo.Book"/>
</list>
</litepal>