升级SQLite数据库最佳写法
SQLite简介SQLite是android中内置的一款轻量级的关系型数据库。支持标准的SQL语法,还遵循数据库的ACID事务。 如何创建数据库Android为了让我们能够更加方便的管理数据库,专门提供了一个SQLiteOpenHelper帮助类,借助这个类就可以非常简单的对数据库进行创建和升级。我们可以自己创建一个类去继承它。SQLiteOpenHelper中有两个抽象方法,分别是onCreate()和onUpgrade(),我们必须要在自己定义的类中重写这两个方法,然后分别在这个两个方法中创建、升级数据库的逻辑。 方法参数分析public MyDataBaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version)
{
super(context,name,factory,version);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
}
SQLiteOpenHelper中有两个构造方法,一般使用参数少的构造方法即可。 具体实例-创建自定义类,继承SQLiteOpenHelper类 public class MyDataBaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK="create table book("
+"id integer primary key autoincrement,"
+"author text,"
+"price real,"
+"pages integer,"
+"name text)";
private Context mContext;
public MyDataBaseHelper(Context context,version);
mcontext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
}
@Override
public void onUpgrade(SQLiteDatabase db,int newVersion) {
switch (oldVersion) {
case 1:
db.execSQL(CREATE_CATEGORY);
case 2:
db.execSQL("alter table Book add column category_id integer");
default:
}
}
}
-新建布局文件,在布局文件中添加一个按钮即可。 public class MainActivity extends Activity {
priavte MyDatabaseHelper dbHelper;
@override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new MyDatabaseHelper(this,"BookStore.db",null,1);
Button createDatabase=(Button)findViewById(R.id.create_database);
createDatabase.setOnClickListener(new OnClickListener(){
@override
public void onClick(View v)
{
dbHelper.getWritableDatabase();
}
});
}
}
首先在onCreate()方法中构建一个MyDatabaseHelper对象,并且通过构造函数的参数将数据库名指定为BookStore.db,版本号为1,点击界面上的按钮,调用getWritableDatabase()方法。 如何升级数据库呢?onUpgrade()方法是用于对数据库进行升级的。 public class MyDataBaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK="create table book("
+"id integer primary key autoincrement,"
+"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)";
private Context mContext;
public MyDataBaseHelper(Context context,version);
mContext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
}
@Override
public void onUpgrade(SQLiteDatabase db,int newVersion) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}
}
}
升级数据库的最佳写法上面的升级数据库的方法是不可取的,因为在现实应用中,数据库包含重要的历史数据,如果全部删除,后果不堪设想。 public class MyDataBaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK="create table book("
+"id integer primary key autoincrement,"
+"category_code integer)";
public MyDataBaseHelper(Context context,version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
}
@Override
public void onUpgrade(SQLiteDatabase db,int newVersion) {
switch (oldVersion) {
case 1:
db.execSQL(CREATE_CATEGORY);
case 2:
db.execSQL("alter table Book add column category_id integer");
default:
}
}
}
[1]:参考资料:第一行代码 Android (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |