sqlite自动更新数据库
来源:互联网 发布:阿里云logo 编辑:程序博客网 时间:2024/06/06 05:00
写一个类继承自 SQLiteOpenHelper
系统会自动添加构造方法、 onCreate方法、onUpgrade方法
当数据库里面数据或者表结构有所改动时,咱们需要升级数据库
这个时候,版本加1.在update里面做相应修改。
需要注意的是,如果需要测试update,每次开始测试,version 值增大,如果和上次的相同,就不会促发update方法了
下面贴上代码
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.widget.Toast;
//第一版的时候,update里面的代码为空,因为不需要更新,之后版本更新,就得在update里面写代码了
public class DataHelp extends SQLiteOpenHelper {
public static String name = "sxjj.db3";
public static int version = 3;
Context context = null;
public DataHelp(Context context, String name, CursorFactory factory,
int version) {
super(context, name, null, version);
this.context = context;
}
/**
* 创建表
*/
public void onCreate(SQLiteDatabase db) {
// 更改表结构之前的
// String sql_tansInfo =
// "create table if not exists transInfo(id integer primary key autoincrement, name nvarchar(50), tel nvarchar(20), content text,type int )";
// 更改表结构之后的,添加了两列,marker 和address
String sql_tansInfo = "create table if not exists transInfo(id integer primary key autoincrement, name nvarchar(50), tel nvarchar(20), content text,type int,address text,marker text )";
db.execSQL(sql_tansInfo);
}
/**
* 更新表
*/
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// 第一次运行,版本加1的时候,触发这个方法了,下一次就不触发了,所以每次测试需要版本号加1,正式发布不需要
// 1创建新表
String sql_create_tansInfo = "create table if not exists transInfo_new(id integer primary key autoincrement, name nvarchar(50), tel nvarchar(20), content text,type int,address text,marker text )";
db.execSQL(sql_create_tansInfo);
// 2 删除旧表
String sql_drop_oldTable = "drop table transInfo";
db.execSQL(sql_drop_oldTable);
// 3表新表的名字改成旧表的名字
String sql_rename = "ALTER TABLE transInfo_new RENAME TO transInfo";
db.execSQL(sql_rename);
// 提示更新了,正式发布可去掉这一句提示
Toast.makeText(context, "更新---------", 1).show();
// 注:如果只是增加一列,其实不需要创建新表,
// 执行 String sql="ALTER TABLE transInfo ADD COLUMN address text;";
// 就可以了,上述的方法是一个通用的方法
}
}
- sqlite自动更新数据库
- IP数据库的自动更新
- Android 数据库自动更新
- php自动更新远程数据库
- SQLite---SQLite数据库简介
- Sqlite 数据库
- SQLite数据库
- SQlite 数据库
- SQLite数据库
- SQLite数据库
- SQLite数据库
- SQLite数据库
- SQLite数据库
- SQlite数据库
- sqlite数据库
- SQLite 数据库
- SQLite数据库
- SQLite数据库
- delphi xe5 android 开发实现手机打电话和发短信
- HDU 1358 Period (KMP)
- 生成iptables
- R books
- Intent和PendingIntent的区别
- sqlite自动更新数据库
- 使用keepalived 实现redis主从高可用
- git上传文件到visualstudio online
- Linux下ppp拨号
- NYOJ题目21-三个水杯
- Android 高效加载大图片
- 书单:根据《用户体验要素》整理的PM书单
- HtmlParser初探--使用Nodefilter和Visitor进行网页分析
- TProfiler部署文档