Sqlite automatically updates the database and sqlite automatically updates the database.

Source: Internet
Author: User

Sqlite automatically updates the database and sqlite automatically updates the database.

Write a class inherited from SQLiteOpenHelper

The system automatically adds the constructor, onCreate, and onUpgrade methods.

When the data or table structure in the database is changed, we need to upgrade the database.

In this case, add version 1. Modify the version in update.

It should be noted that if you need to test update, each time you start the test, the version value increases. If it is the same as the previous one, the update method will not be promoted.

Paste the code below

First, the original table structure, corresponding code


Import android. content. Context;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteOpenHelper;
Import android. database. sqlite. SQLiteDatabase. CursorFactory;


Public class DataHelp extends SQLiteOpenHelper {


Public static String name = "sxjj. db3 ";
Public static int version = 1;


Public DataHelp (Context context, String name, CursorFactory factory,
Int version ){
Super (context, name, null, version );
}






Public void onCreate (SQLiteDatabase db ){


String SQL _tansInfo = "create table if not exists transInfo (id integer primary key autoincrement, name nvarchar (50), tel nvarchar (20), content text, type int )";
Db.exe cSQL (SQL _tansInfo );





}


Public void onUpgrade (SQLiteDatabase arg0, int arg1, int arg2 ){


}






}



Current table structure, corresponding code

Import com. ylj. sxbmzx. view. QueryTransInfoActivity;


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


Public class DataHelp extends SQLiteOpenHelper {


Public static String name = "sxjj. db3 ";
Public static int version = 3;
Context context = null;


Public DataHelp (Context context, String name, CursorFactory factory,
Int version ){

Super (context, name, null, version );
This. context = context;
}






Public void onCreate (SQLiteDatabase db ){
// String SQL _tansInfo = "create table if not exists transInfo (id integer primary key autoincrement, name nvarchar (50), tel nvarchar (20), content text, type int, address text, marker text )";




String SQL _tansInfo = "create table if not exists transInfo (id integer primary key autoincrement, name nvarchar (50), tel nvarchar (20), content text, type int, address text, marker text )";
Db.exe cSQL (SQL _tansInfo );





}


Public void onUpgrade (SQLiteDatabase db, int arg1, int arg2 ){
// This method is triggered when version 1 is added during the first running. This method will not be triggered next time.


// String SQL _tansInfo = "create table if not exists transInfo1 (id integer primary key autoincrement, name nvarchar (50), tel nvarchar (20), content text, marker text, type int, address text )";

// Db.exe cSQL (SQL _tansInfo );
// String SQL _tansInfo_insert = "INSERT INTO transInfo1 (id integer primary key autoincrement, name nvarchar (50), tel nvarchar (20), content text, marker text, type int, address text) ";



// String SQL _delete = "delete from transInfo ";
// Db.exe cSQL (SQL _delete );
// String SQL = "ALTER TABLE transInfo ADD COLUMN address text ;";
// Db.exe cSQL (SQL );
//
// String sql2 = "alter table transInfo add column marker text ;";
// Db.exe cSQL (sql2 );


Toast. makeText (context, "Update ---------", 1). show ();
// It is estimated that the alter table statement is actually executed. The address is null.
// Do not know whether the original data is retained after alter table, but the new field is empty and no data is retained after testing.
// After the program is changed, the new fields must be checked out during query. Therefore, re-insert the data.

// The current table actually needs to add two columns, so I have to insert the column twice. You can also delete the original table and recreate the table.

// Add two columns. This is not suitable for later versions.


String SQL _create_tansInfo = "create table if not exists transInfo_new (id integer primary key autoincrement, name nvarchar (50), tel nvarchar (20), content text, type int, address text, marker text )";
// No insert is required.
Db.exe cSQL (SQL _create_tansInfo );
String SQL _drop_oldTable = "drop table transInfo ";
Db.exe cSQL (SQL _drop_oldTable );
String SQL _rename = "ALTER TABLE transInfo_new RENAME TO transInfo ";
Db.exe cSQL (SQL _rename );
 


}






}



Android development and sqlite database updates

String. valueOf (date) Try again

How to independently update the sqlite database version

Each time you use new SQLiteOpenHelper, you will wear a version. This method will be called when the version you pass is larger than the current 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.