美文网首页Android开发我爱编程
Android_6.数据存储_3_SQLite存储之升级数据库

Android_6.数据存储_3_SQLite存储之升级数据库

作者: zhang_yongfeng | 来源:发表于2018-05-29 16:46 被阅读2次

    本文是以Android Studio为开发工具,<<第一行代码Anroid第2版>> 为学习指导书籍的学习记录

    6.4.2 升级数据库

    使用MyDatabaseHelper中的onUpgrade()方法,对数据库进行升级操作

    要实现的内容:
    在DatabaseTest项目(上一篇内容)中已经有一张Book表,现在再添加一张表进数据库

    修改上节中MyDatabaseHelper类如下:

    package com.zyf.android.databasetest;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.widget.Toast;
    
    public class MyDatabaseHelper extends SQLiteOpenHelper {
    
        //创建表sql语句
        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_CATEGOTY = "create table Category("
                + "id integer primary key autoincrement,"
                + "category_name text,"
                + "category_code integer)";
    
        private Context mContext;
    
        public MyDatabaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,
                                int version){
            super(context,name,factory,version);
            mContext =context;
        }
        //抽象方法
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_BOOK);
            db.execSQL(CREATE_CATEGOTY);//创建数据库表
            Toast.makeText(mContext,"create succeeded", Toast.LENGTH_SHORT).show();
        }
    
        //抽象方法(升级方法)
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop table if exists Book");//如果已经存在的Book表,就删掉重新创建
            db.execSQL("drop table if exists Category");
            onCreate(db);
        }
    }
    
    Snip20180529_5.png

    修改MainActivity类如下;

    package com.zyf.android.databasetest;
    
    import android.content.ContentValues;
    import android.database.sqlite.SQLiteDatabase;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
        private MyDatabaseHelper dbHelper;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,3);
            Button createDatabase = (Button) findViewById(R.id.create_database);
            createDatabase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dbHelper.getWritableDatabase();
                }
            });
       }
    }
    
    Snip20180529_6.png

    运行代码,点击按钮,用adb 和sqlite3 查看数据库中的表

    Snip20180529_2.png

    写在最后
    本示例源码下载百度云地址

    相关文章

      网友评论

        本文标题:Android_6.数据存储_3_SQLite存储之升级数据库

        本文链接:https://www.haomeiwen.com/subject/tlyajftx.html