一. SQLiteOpenHolper 用来创建数据库,数据表,更新版本
用法:
二.SQLiteDatabase
创建类继承SQLiteOpenHelper
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context,"day7",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//学生表数据
db.execSQL("create table student(id integer primary key autoincrement,name varchar(20),sex varchar(10),birth integer,department varchar(20),address varchar(20))");
//成绩表数据
db.execSQL("create table score(id integer primary key autoincrement,stu_id integer,c_name varchar(20),grade integer)");
//学生表添加数据
db.execSQL("insert into student values(901,'张老大','男',1985,'计算机系','北京市海淀区')");
db.execSQL("insert into student values(902,'张老二','男',1986,'中文系','北京市昌平区')");
db.execSQL("insert into student values(903,'张三','女',1990,'中文系','湖南省永州市')");
db.execSQL("insert into student values(904,'李四','男',1990,'英语系','辽宁省阜新市')");
db.execSQL("insert into student values(905,'王五','女',1991,'英语系','福建省厦门市')");
db.execSQL("insert into student values(906,'王六','男',1988,'计算机系','湖南省衡阳市')");
//成绩表添加数据
db.execSQL("insert into score values(1,901,'计算机',98)");
db.execSQL("insert into score values(2,901,'英语',80)");
db.execSQL("insert into score values(3,901,'计算机',65)");
db.execSQL("insert into score values(4,901,'中文',88)");
db.execSQL("insert into score values(5,901,'中文',95)");
db.execSQL("insert into score values(6,901,'计算机',70)");
db.execSQL("insert into score values(7,901,'英语',92)");
db.execSQL("insert into score values(8,901,'英语',94)");
db.execSQL("insert into score values(9,901,'计算机',90)");
db.execSQL("insert into score values(10,901,'英语',85)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
获得对象
MyHelper myHelper = new MyHelper(MainActivity.this);
db = myHelper.getWritableDatabase();
增删改查
/**
* 查询显示
*/
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = db.rawQuery("select * from student", null);
while (cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
int birth = cursor.getInt(cursor.getColumnIndex("birth"));
String department = cursor.getString(cursor.getColumnIndex("department"));
String address = cursor.getString(cursor.getColumnIndex("address"));
Student student = new Student(id, name, sex, birth, department, address);
sb.append(student.toString()+"\n");
}
tv.setText(sb.toString());
}
});
/**
* 增加
*/
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.execSQL("insert into student values(null,'add','男',1985,'计算机系','北京市海淀区')");
}
});
/**
* 删除
*/
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.execSQL("delete from student where student.name = ?",new String[]{"add"});
}
});
/**
* 修改
*/
set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.execSQL("update student set name = '赵六' where student.name = ?",new String[]{"王六"});
}
});