一、基础
二、索引
三、视图
四、触发器
五、例子
SQL语句
CREATE TABLE android_metadata (locale TEXT);
CREATE TABLE student(student_id integer primary key autoincrement, name varchar(20), age integer, gender bit, phone varchar(20));
CREATE TABLE score(score_id integer primary key autoincrement, name varchar(20), chinese real, math real, english real, average real);
CREATE INDEX student_name_index on student(name);
CREATE INDEX score_name_index on score(name);
CREATE VIEW student_info_view as select name, age, phone from student;
CREATE TRIGGER score_insert_trigger after insert on score for each row begin update score set average=(chinese + math + english)/3; end;
CREATE TRIGGER score_update_trigger after update on score for each row begin update score set average=(chinese + math + english)/3; end;
代码(使用AndroidStudio开发)
Student:
public class Student {
private int student_id;
private String name;
private int age;
private int gender;
private String phone;
public Student(String name, int age, int gender, String phone) {
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
}
public void setStudent_id(int student_id) {
this.student_id = student_id;
}
public int getStudent_id() {
return student_id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setGender(int gender) {
this.gender = gender;
}
public int getGender() {
return gender;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("student_id: ")
.append(student_id)
.append(", name: ")
.append(name)
.append(", age: ")
.append(age)
.append(", gender: ")
.append(gender)
.append(", phone: ")
.append(phone);
return builder.toString();
}
}
Score:
public class Score {
private int score_id;
private String name;
private float chinese;
private float math;
private float english;
private float average;
public Score(String name, float chinese, float math, float english) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public void setScore_id(int score_id) {
this.score_id = score_id;
}
public int getScore_id() {
return score_id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setChinese(float chinese) {
this.chinese = chinese;
}
public float getChinese() {
return chinese;
}
public void setEnglish(float english) {
this.english = english;
}
public float getEnglish() {
return english;
}
public void setMath(float math) {
this.math = math;
}
public float getMath() {
return math;
}
public void setAverage(float average) {
this.average = average;
}
public float getAverage() {
return average;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("score_id: ")
.append(score_id)
.append(", name: ")
.append(name)
.append(", chinese: ")
.append(chinese)
.append(", math: ")
.append(math)
.append(", english: ")
.append(english)
.append(", average: ")
.append(average);
return builder.toString();
}
}
MySQLiteOpenHelper:
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static final String TAG = "MySQLiteOpenHelper";
//定义数据库文件名及版本号
private static final String DB_NAME = "student.db";
private static final int DB_VERSION = 3;
private MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//对外提供构造函数
public MySQLiteOpenHelper(Context context) {
//调用该类中的私有构造函数
this(context, DB_NAME, null, DB_VERSION);
}
//当第一次创建数据库的时候回调方法
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "zwm, onCreate");
//创建数据库表的语句
String sql = "create table student(student_id integer primary key autoincrement, name varchar(20), age integer, gender bit, phone varchar(20))";
String sql2 = "create table score(score_id integer primary key autoincrement, name varchar(20), chinese real, math real, english real, average real)";
db.execSQL(sql);
db.execSQL(sql2);
//创建数据库索引
createDBIndex(db);
//创建数据库视图
createDBView(db);
//创建数据库触发器
createDBTrigger(db);
}
//当数据库升级的时候回调方法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, "zwm, onUpgrade, oldVersion: " + oldVersion + ", newVersion: " + newVersion);
if(oldVersion < 3) {
db.execSQL("alter table score add average real");
//创建数据库触发器
createDBTrigger(db);
}
}
//当数据库被打开的时候回调方法
@Override
public void onOpen(SQLiteDatabase db) {
Log.d(TAG, "zwm, onOpen");
}
//创建数据库索引
private void createDBIndex(SQLiteDatabase db) {
Log.d(TAG, "zwm, createDBIndex");
db.execSQL("create index student_name_index on student(name)");
db.execSQL("create index score_name_index on score(name)");
}
//创建数据库视图
private void createDBView(SQLiteDatabase db) {
Log.d(TAG, "zwm, createDBView");
db.execSQL("create view student_info_view as select name, age, phone from student");
}
//创建数据库触发器
private void createDBTrigger(SQLiteDatabase db) {
Log.d(TAG, "zwm, createDBTrigger");
db.execSQL("create trigger score_insert_trigger after insert on score for each row begin update score set average=(chinese + math + english)/3; end");
db.execSQL("create trigger score_update_trigger after update on score for each row begin update score set average=(chinese + math + english)/3; end");
}
}
DBManager:
public class DBManager {
private static final String TAG = "DBManager";
private volatile static DBManager sInstance;
private MySQLiteOpenHelper mHelper;
private SQLiteDatabase mDB;
private DBManager(Context context) {
mHelper = new MySQLiteOpenHelper(context);
mDB = mHelper.getWritableDatabase();
}
public static DBManager getInstance(Context context) {
if(sInstance == null) {
synchronized (DBManager.class) {
if(sInstance == null) {
sInstance = new DBManager(context);
}
}
}
Log.d(TAG, "zwm, getInstance: " + sInstance);
return sInstance;
}
public void insetStudent(Student student) {
Log.d(TAG, "zwm, insetStudent: " + student);
if(mDB == null) {
return;
}
mDB.execSQL("insert into student(name, age, gender, phone) values(?, ?, ?, ?)",
new String[]{student.getName(), String.valueOf(student.getAge()), String.valueOf(student.getGender()), student.getPhone()});
}
public void deleteAllStudent() {
Log.d(TAG, "zwm, deleteAllStudent");
if(mDB == null) {
return;
}
mDB.execSQL("delete from student");
}
public void deleteStudent(String name) {
Log.d(TAG, "zwm, deleteStudent, name: " + name);
if(mDB == null) {
return;
}
mDB.execSQL("delete from student where name=?", new String[]{name});
}
public void updateStudent(Student student) {
Log.d(TAG, "zwm, updateStudent: " + student);
if(mDB == null) {
return;
}
mDB.execSQL("update student set age=? , gender=?, phone=? where name=?",
new String[]{String.valueOf(student.getAge()), String.valueOf(student.getGender()), student.getPhone(), student.getName()});
}
public List<Student> queryStudent() {
Log.d(TAG, "zwm, queryStudent");
if(mDB == null) {
return null;
}
Cursor cursor = mDB.rawQuery("select * from student", null);
List<Student> list = null;
if (cursor.moveToFirst()) {
list = new ArrayList<>();
do {
int student_id = cursor.getInt(cursor.getColumnIndex("student_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
int gender = cursor.getInt(cursor.getColumnIndex("gender"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
Student student = new Student(name, age, gender, phone);
student.setStudent_id(student_id);
list.add(student);
} while (cursor.moveToNext());
}
return list;
}
public List<Student> queryStudent(String name) {
Log.d(TAG, "zwm, queryStudent, name: " + name);
if(mDB == null) {
return null;
}
Cursor cursor = mDB.rawQuery("select * from student where name=?", new String[]{name});
List<Student> list = null;
if (cursor.moveToFirst()) {
list = new ArrayList<>();
do {
int student_id = cursor.getInt(cursor.getColumnIndex("student_id"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
int gender = cursor.getInt(cursor.getColumnIndex("gender"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
Student student = new Student(name, age, gender, phone);
student.setStudent_id(student_id);
list.add(student);
} while (cursor.moveToNext());
}
return list;
}
public void insetScore(Score score) {
Log.d(TAG, "zwm, insetScore: " + score);
if(mDB == null) {
return;
}
mDB.execSQL("insert into score(name, chinese, math, english) values(?, ?, ?, ?)",
new String[]{score.getName(), String.valueOf(score.getChinese()), String.valueOf(score.getMath()), String.valueOf(score.getEnglish())});
}
public void deleteAllScore() {
Log.d(TAG, "zwm, deleteAllScore");
if(mDB == null) {
return;
}
mDB.execSQL("delete from score");
}
public void deleteScore(String name) {
Log.d(TAG, "zwm, deleteScore, name: " + name);
if(mDB == null) {
return;
}
mDB.execSQL("delete from score where name=?", new String[]{name});
}
public void updateScore(Score score) {
Log.d(TAG, "zwm, updateScore: " + score);
if(mDB == null) {
return;
}
mDB.execSQL("update score set chinese=? , math=?, english=? where name=?",
new String[]{String.valueOf(score.getChinese()), String.valueOf(score.getMath()), String.valueOf(score.getEnglish()), score.getName()});
}
public List<Score> queryScore() {
Log.d(TAG, "zwm, queryScore");
if(mDB == null) {
return null;
}
Cursor cursor = mDB.rawQuery("select * from score", null);
List<Score> list = null;
if (cursor.moveToFirst()) {
list = new ArrayList<>();
do {
int score_id = cursor.getInt(cursor.getColumnIndex("score_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
float chinese = cursor.getFloat(cursor.getColumnIndex("chinese"));
float math = cursor.getFloat(cursor.getColumnIndex("math"));
float english = cursor.getFloat(cursor.getColumnIndex("english"));
float average = cursor.getFloat(cursor.getColumnIndex("average"));
Score score = new Score(name, chinese, math, english);
score.setScore_id(score_id);
score.setAverage(average);
list.add(score);
} while (cursor.moveToNext());
}
return list;
}
public List<Score> queryScore(String name) {
Log.d(TAG, "zwm, queryScore, name: " + name);
if(mDB == null) {
return null;
}
Cursor cursor = mDB.rawQuery("select * from score where name=?", new String[]{name});
List<Score> list = null;
if (cursor.moveToFirst()) {
list = new ArrayList<>();
do {
int score_id = cursor.getInt(cursor.getColumnIndex("score_id"));
float chinese = cursor.getFloat(cursor.getColumnIndex("chinese"));
float math = cursor.getFloat(cursor.getColumnIndex("math"));
float english = cursor.getFloat(cursor.getColumnIndex("english"));
float average = cursor.getFloat(cursor.getColumnIndex("average"));
Score score = new Score(name, chinese, math, english);
score.setScore_id(score_id);
score.setAverage(average);
list.add(score);
} while (cursor.moveToNext());
}
return list;
}
public Cursor queryStudentInfoView() {
if(mDB == null) {
return null;
}
Cursor cursor = mDB.rawQuery("select * from student_info_view", null);
return cursor;
}
public void closeDB() {
Log.d(TAG, "zwm, closeDB");
if(mDB != null) {
mDB.close();
sInstance = null;
}
}
}
MainActivity:
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private DBManager dbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbManager = DBManager.getInstance(getApplicationContext());
//student数据表测试
Student xiaoming = new Student("xiaoming", 29, 1, "15017538888");
Student xiaohuan = new Student("xiaohuan", 26, 0, "13751868888");
Student xiaohan = new Student("xiaohan", 2, 0, "16899998888");
dbManager.insetStudent(xiaoming);
dbManager.insetStudent(xiaohuan);
dbManager.insetStudent(xiaohan);
List<Student> list = dbManager.queryStudent();
for(Student student : list) {
Log.d(TAG, "zwm, student: " + student);
}
List<Student> list2 = dbManager.queryStudent("xiaohan");
for(Student student : list2) {
Log.d(TAG, "zwm, student: " + student);
}
dbManager.deleteStudent("xiaoming");
xiaohan.setAge(3);
dbManager.updateStudent(xiaohan);
list = dbManager.queryStudent();
for(Student student : list) {
Log.d(TAG, "zwm, student: " + student);
}
//score数据表测试
Score xiaomingScore = new Score("xiaoming", 88, 100, 99);
Score xiaohuanScore = new Score("xiaohuan", 90, 98, 96);
Score xiaohanScore = new Score("xiaohan", 100, 100, 98);
dbManager.insetScore(xiaomingScore);
dbManager.insetScore(xiaohuanScore);
dbManager.insetScore(xiaohanScore);
List<Score> listScore = dbManager.queryScore();
for(Score score : listScore) {
Log.d(TAG, "zwm, score: " + score);
}
List<Score> listScore2 = dbManager.queryScore("xiaohan");
for(Score score : listScore2) {
Log.d(TAG, "zwm, score: " + score);
}
dbManager.deleteScore("xiaoming");
xiaohanScore.setEnglish(100);
dbManager.updateScore(xiaohanScore);
listScore = dbManager.queryScore();
for(Score score : listScore) {
Log.d(TAG, "zwm, score: " + score);
}
//student_info_view数据视图测试
Cursor cursor = dbManager.queryStudentInfoView();
Log.d(TAG, "zwm, query student_info_view");
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
Log.d(TAG, "zwm, name: " + name + ", age: " + age + ", phone: " + phone);
} while (cursor.moveToNext());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(dbManager != null) {
dbManager.deleteAllStudent();
dbManager.deleteAllScore();
dbManager.closeDB();
}
}
}
输出Log:
2019-03-07 16:56:49.824 D/MySQLiteOpenHelper: zwm, onCreate
2019-03-07 16:56:49.826 D/MySQLiteOpenHelper: zwm, createDBIndex
2019-03-07 16:56:49.827 D/MySQLiteOpenHelper: zwm, createDBView
2019-03-07 16:56:49.827 D/MySQLiteOpenHelper: zwm, createDBTrigger
2019-03-07 16:56:49.835 D/MySQLiteOpenHelper: zwm, onOpen
2019-03-07 16:56:49.835 D/DBManager: zwm, getInstance: com.tomorrow.test2019_2.DBManager@22e0b50
2019-03-07 16:56:49.836 D/DBManager: zwm, insetStudent: student_id: 0, name: xiaoming, age: 29, gender: 1, phone: 15017538888
2019-03-07 16:56:49.841 D/DBManager: zwm, insetStudent: student_id: 0, name: xiaohuan, age: 26, gender: 0, phone: 13751868888
2019-03-07 16:56:49.847 D/DBManager: zwm, insetStudent: student_id: 0, name: xiaohan, age: 2, gender: 0, phone: 16899998888
2019-03-07 16:56:49.853 D/DBManager: zwm, queryStudent
2019-03-07 16:56:49.855 D/MainActivity: zwm, student: student_id: 1, name: xiaoming, age: 29, gender: 1, phone: 15017538888
2019-03-07 16:56:49.855 D/MainActivity: zwm, student: student_id: 2, name: xiaohuan, age: 26, gender: 0, phone: 13751868888
2019-03-07 16:56:49.856 D/MainActivity: zwm, student: student_id: 3, name: xiaohan, age: 2, gender: 0, phone: 16899998888
2019-03-07 16:56:49.856 D/DBManager: zwm, queryStudent, name: xiaohan
2019-03-07 16:56:49.857 D/MainActivity: zwm, student: student_id: 3, name: xiaohan, age: 2, gender: 0, phone: 16899998888
2019-03-07 16:56:49.857 D/DBManager: zwm, deleteStudent, name: xiaoming
2019-03-07 16:56:49.864 D/DBManager: zwm, updateStudent: student_id: 0, name: xiaohan, age: 3, gender: 0, phone: 16899998888
2019-03-07 16:56:49.866 D/DBManager: zwm, queryStudent
2019-03-07 16:56:49.867 D/MainActivity: zwm, student: student_id: 2, name: xiaohuan, age: 26, gender: 0, phone: 13751868888
2019-03-07 16:56:49.867 D/MainActivity: zwm, student: student_id: 3, name: xiaohan, age: 3, gender: 0, phone: 16899998888
2019-03-07 16:56:49.868 D/DBManager: zwm, insetScore: score_id: 0, name: xiaoming, chinese: 88.0, math: 100.0, english: 99.0, average: 0.0
2019-03-07 16:56:49.874 D/DBManager: zwm, insetScore: score_id: 0, name: xiaohuan, chinese: 90.0, math: 98.0, english: 96.0, average: 0.0
2019-03-07 16:56:49.881 D/DBManager: zwm, insetScore: score_id: 0, name: xiaohan, chinese: 100.0, math: 100.0, english: 98.0, average: 0.0
2019-03-07 16:56:49.902 D/DBManager: zwm, queryScore
2019-03-07 16:56:49.906 D/MainActivity: zwm, score: score_id: 1, name: xiaoming, chinese: 88.0, math: 100.0, english: 99.0, average: 95.666664
2019-03-07 16:56:49.907 D/MainActivity: zwm, score: score_id: 2, name: xiaohuan, chinese: 90.0, math: 98.0, english: 96.0, average: 94.666664
2019-03-07 16:56:49.907 D/MainActivity: zwm, score: score_id: 3, name: xiaohan, chinese: 100.0, math: 100.0, english: 98.0, average: 99.333336
2019-03-07 16:56:49.907 D/DBManager: zwm, queryScore, name: xiaohan
2019-03-07 16:56:49.910 D/MainActivity: zwm, score: score_id: 3, name: xiaohan, chinese: 100.0, math: 100.0, english: 98.0, average: 99.333336
2019-03-07 16:56:49.910 D/DBManager: zwm, deleteScore, name: xiaoming
2019-03-07 16:56:49.922 D/DBManager: zwm, updateScore: score_id: 0, name: xiaohan, chinese: 100.0, math: 100.0, english: 100.0, average: 0.0
2019-03-07 16:56:49.925 D/DBManager: zwm, queryScore
2019-03-07 16:56:49.926 D/MainActivity: zwm, score: score_id: 2, name: xiaohuan, chinese: 90.0, math: 98.0, english: 96.0, average: 94.666664
2019-03-07 16:56:49.926 D/MainActivity: zwm, score: score_id: 3, name: xiaohan, chinese: 100.0, math: 100.0, english: 100.0, average: 100.0
2019-03-07 16:56:49.927 D/MainActivity: zwm, query student_info_view
2019-03-07 16:56:49.927 D/MainActivity: zwm, name: xiaohuan, age: 26, phone: 13751868888
2019-03-07 16:56:49.928 D/MainActivity: zwm, name: xiaohan, age: 3, phone: 16899998888