package com.baihai.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLite extends SQLiteOpenHelper {
public MySQLite(Context context) {
super(context, "baiye2.db", null, 2);
// TODO Auto-generated constructor stub
}
super(context, "baiye2.db", null, 2);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table students(id integer primary key autoincrement , name varchar(20),age varchar(20),phone varchar(20))");
}
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table students(id integer primary key autoincrement , name varchar(20),age varchar(20),phone varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
/**
* 特别注意: 首先更该表名时要rename加上 to 其次添加数据是不能有values关键字;
* */
if (oldVersion <newVersion) {
// rename table t_stu to t_student;
// alter table t_student rename t_stu;
// 将students表更改为临时表
db.execSQL("alter table students rename to students1");
// 创建一个新表students1
db.execSQL("create table students(id integer primary key autoincrement , name varchar(20),age varchar(20),sex varchar(20),phone varchar(20))");
// insert into t_user(name,age) value ('zhangsan',20);
// 查询students1表所有的数据,将数据插入2步骤中所创建的一个新表 students
db.execSQL("insert into students (name,age,phone) select name,age,phone from students1");
// drop table t_user;
// 删除零时表students1
db.execSQL("drop table students1");
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
/**
* 特别注意: 首先更该表名时要rename加上 to 其次添加数据是不能有values关键字;
* */
if (oldVersion <newVersion) {
// rename table t_stu to t_student;
// alter table t_student rename t_stu;
// 将students表更改为临时表
db.execSQL("alter table students rename to students1");
// 创建一个新表students1
db.execSQL("create table students(id integer primary key autoincrement , name varchar(20),age varchar(20),sex varchar(20),phone varchar(20))");
// insert into t_user(name,age) value ('zhangsan',20);
// 查询students1表所有的数据,将数据插入2步骤中所创建的一个新表 students
db.execSQL("insert into students (name,age,phone) select name,age,phone from students1");
// drop table t_user;
// 删除零时表students1
db.execSQL("drop table students1");
}
}
}