Android SQLite 升级数据库,在原有数据库的基础上添加一列

2018-02-04 23:46:16

1.原本以为很简单的问题,直接在原来创建数据库的语句中加上需要添加的列new_column,但是运行时发现,应用crash。原因是,原有数据库文件已经存在的情况下并不会重新创建数据库,也就是说此时数据库中并没有new_column列,这个时候,运行query()来查询数据库如果包括new_column列,就会导致应用crash。


2.其实,SQLite提供了数据库升级的方法 onUpgrade(),只需要在创建SQLiteOpenHelper对象时,传人比原版本号更大的版本号,便会触发该方法,在这个方法中可以实现迁移原有数据及创建新数据库Table。\


3.在某些情况下,数据库版本会出现降低的情况:如用户安装了版本更低的应用,但是应用数据没有改变。这种情况下,如果设计数据库时没有override onDowngrade()方法,会导致应用崩溃。所以,在设计数据库时,最好能override onDowngrade()方法,防止这种情况下应用崩溃的发生


  • 2017-11-06 01:00:17

    撤销git add

    如何撤销git add,不小心执行了git add . 操作,但是又不能提交所有的文件,因为对应不同的分支,现在怎么样可以将git add 撤销回来

  • 2017-11-10 00:06:15

    CORS: credentials mode is 'include'

    XMLHttpRequest cannot load http://localhost/Foo.API/token. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:5000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.