升级SQLite数据库

来源:互联网 发布:团购的软件 编辑:程序博客网 时间:2024/06/08 08:52

一、步骤

1.在之前的基础上添加一张Category表;在onCreate()方法中执行建CREATE_CATEGORY表语句。

2.然后在onUpgrade()中执行两条drop语句,发现数据库表存在,就将已经存在的表格删除;再在onCreate()中重新创建表格。

3.要让onUpgrade()方法得到执行,在SQLiteOpenHelper的构造方法里面的构造函数的第四个参数为当前数据库的版本号,之前我们传入的值为1,现在改成一个比1大的数。


二、代码

1.MyDatabaseHelper.java

package com.zhc.sqliteopenhelper;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;import android.widget.Toast;public class MyDatabaseHelper extends SQLiteOpenHelper {// 数据库中创建一张表的与语句;这里创建一张Book表。public static final String CREATE_BOOK = "create table Book ("+ "id integer primary key autoincrement," + "author text,"+ "price real," + "pages integer," + "name text)";// 添加一张Category表public static final String CREATE_CATEGORY = "create table Category ("+ "id integer primary key autoincrement," + "category_name text,"+ "category_code integer)";private Context mContext;/** *  * @param context *            上下文 * @param name *            数据库名 * @param factory *            允许我们在查询数据的时候返回一个自定义的Cursor,一般都是传入null * @param version *            当前数据库的版本号,可用于对数据库进行升级操作 *  *            数据库文件会存放在/data/data/<package name>/databases/目录下 */public MyDatabaseHelper(Context context, String name,CursorFactory factory, int version) {super(context, name, factory, version);mContext = context;}/** * 此方法中实现创建数据库的逻辑 */@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL(CREATE_BOOK);// 执行建CREATE_BOOK表语句db.execSQL(CREATE_CATEGORY);// 执行建CREATE_CATEGORY表语句Toast.makeText(mContext, "Create Book and Category succeeded", Toast.LENGTH_LONG).show();}/** * 此方法中实现升级数据库的逻辑 */@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL("drop table if exists Book");db.execSQL("drop table if exists Category");onCreate(db);}}

2.MainActivity.java

package com.zhc.sqliteopenhelper;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.app.Activity;public class MainActivity extends Activity {private MyDatabaseHelper dbHelper;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);Button createDatabase = (Button) findViewById(R.id.create_database);createDatabase.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {dbHelper.getWritableDatabase();//创建或打开一个现有的数据库(数据库存在直接打开,否则创建一个新数据库)}});}}

3.activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/create_database"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Create database" /></LinearLayout>
三、结果


0 0
原创粉丝点击