多一分心力去注意别人,就少一分心力反省自己。
本讲内容:SQLiteDatabse数据库
一、升级数据库的最佳写法
上一讲我们学习的升级数据库的方式是非常粗暴的,譬如用户更新了这个版本之后会发现以前程序中存储的本地数据全部丢失了。
示例一:创建数据库
建一个名为StoreBook.db的数据库,然后在这个数据库中新建一张book表。表中有id(主键)、作者、价格、页数和书名等列。
运行程序,点击按钮,可以看到生成StoreBook.db数据库和一张book表
下面是MyDatabaseHelper.java文件:
public class MyDatabaseHelper extends SQLiteOpenHelper{
private Context mContext;
public static final String CREATE_BOOK="create table book(id integer primary key autoincrement,author text,price real,pages integer,name text)";
//四个参数的构造函数
public MyDatabaseHelper(Context context, String name,CursorFactory factory, int version) {
super(context, name, factory, version);
mContext=context;
}
//回调函数,第一次创建数据库时才会调用此函数
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_LONG).show();
}
//回调函数,当你构造DBHelper的传递的Version与之前的Version不同时调用此函数
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity implements OnClickListener{
private MyDatabaseHelper dbHelper;
private Button createDatabase;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//指定数据库名为StoreBook.db,版本号为1
dbHelper=new MyDatabaseHelper(this, "StoreBook.db", null, 1);
createDatabase=(Button) findViewById(R.id.create_database);
createDatabase.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.create_database:
dbHelper.getWritableDatabase();//创建数据库
break;
}
}
}
示例二:升级数据库
之后又有了新的需求,需要向数据库中再添加一张Category表
下面是MyDatabaseHelper.java文件:
public class MyDatabaseHelper extends SQLiteOpenHelper{
private Context mContext;
public static final String CREATE_BOOK="create table book(id integer primary key autoincrement,author text,price real,pages integer,name text)";
public static final String CREATE_CATEGORY="create table Category(id integer primary key autoincrement,category_name text,category_code integer)";
//四个参数的构造函数
public MyDatabaseHelper(Context context, String name,CursorFactory factory, int version) {
super(context, name, factory, version);
mContext=context;
}
//回调函数,第一次创建数据库时才会调用此函数
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
}
//回调函数,当你构造DBHelper的传递的Version与之前的Version不同时调用此函数
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
db.execSQL(CREATE_CATEGORY);
default:
}
}
}
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity implements OnClickListener{
private MyDatabaseHelper dbHelper;
private Button createDatabase;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//指定数据库名为StoreBook.db,版本号为1
dbHelper=new MyDatabaseHelper(this, "StoreBook.db", null, 2);
createDatabase=(Button) findViewById(R.id.create_database);
createDatabase.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.create_database:
dbHelper.getWritableDatabase();//创建数据库
break;
}
}
}
示例三:升级数据库
没过多久又有了新的需求,这次要给book表和Category表之间建立关联,需要在book表中添加一个category_id的字段。
下面是MyDatabaseHelper.java文件:
public class MyDatabaseHelper extends SQLiteOpenHelper{
private Context mContext;
public static final String CREATE_BOOK="create table book(id integer primary key autoincrement,author text,price real,pages integer,name text,category_id integer)";
public static final String CREATE_CATEGORY="create table Category(id integer primary key autoincrement,category_name text,category_code integer)";
//四个参数的构造函数
public MyDatabaseHelper(Context context, String name,CursorFactory factory, int version) {
super(context, name, factory, version);
mContext=context;
}
//回调函数,第一次创建数据库时才会调用此函数
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
}
//回调函数,当你构造DBHelper的传递的Version与之前的Version不同时调用此函数
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
db.execSQL(CREATE_CATEGORY);
case 2:
db.execSQL("alter table book add column category_id integer");
default:
}
}
}
注意:switch中第一个case的最后都是没有使用break的,这是为了保证在跨版本升级时,第一次的数据库修改都能被全部执行到。Take your time and enjoy
it 要原码的、路过的、学习过的请留个言,顶个呗~~