Android database Management---upgrade database

Source: Internet
Author: User

Package com.example.databasetest;

Import Android.content.Context;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteDatabase.CursorFactory;
Import Android.database.sqlite.SQLiteOpenHelper;
Import Android.widget.Toast;

/**
* Created by Administrator on 2016/3/4.
*/
public class Databasehelper extends sqliteopenhelper{

public static final String Create_book = "CREATE table book (ID integer primary key autoincrement, author Text,price real,p Ages integer,name text) ";
public static final String create_category= "CREATE table CATEGORY (ID integer primary key autoincrement,category_name text , Category_code integer) ";
Private Context Mcontext;

Public Databasehelper (Context context, String name, Cursorfactory factory, int version) {
Super (context, name, Factory, version);
Mcontext = context;
}

@Override
public void OnCreate (Sqlitedatabase db) {
Db.execsql (Create_book);
Db.execsql (create_category);
Toast.maketext (Mcontext, "created successfully", Toast.length_short). Show ();
}

@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
Db.execsql ("drop table if exists book");
Db.execsql ("drop table if exists Category");
OnCreate (DB);
}


}



Package com.example.datebasetest;

Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;

public class Mainactivity extends Appcompatactivity {

Private Button btn;
Private Databasehelper DBHelper;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

BTN = (Button) Findviewbyid (r.id.create_database);
DBHelper = new Databasehelper (This, "bookstore", null,2);

Btn.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Dbhelper.getwritabledatabase ();
}
});
}
}

Android database Management---upgrade database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.