前面学了一些sqlite数据库的基本使用方法,下面实现一个简答的数据库应用程序。
程序介绍
本程序模范一个简单的课程管理系统,老师可以添加删除学生,添加删除课程,将学生添加到课程中,将学生从课程中删除,查询课程学生,查询学生所修课程,根据条件查询学生或课程。
程序结构
数据库设计
程序中涉及的对象有学生,课程,学生和课程组成的班级。用三个表来表示这三个对象。
描述 学生的信息如下
- 学号
- 姓名
- 专业
- 注册状态
- 性别
student表如下.
列 | 类型 |
---|---|
student_id | integer |
name | text |
major | text |
state | text |
gender | text |
grade | integer |
描述课程的信息如下
- 课程编号
- 课程名字
课程类的表如下
列 | 类型 |
---|---|
course_id | integer |
course_name | text |
描述班级的信息如下
- 课程编号
- 学生学号
班级表如下
列 | 类型 |
---|---|
course_id | integer |
student_id | integer |
构建建表语句
students建表语句如下
create table students(
student_id integer primary key autoincrement,
name text,
major text
state text,
grade,integer
gender text
)
courese的建表语句如下
create table courses(
course_id integer primary key autoincrement,
course_name text
)
classes的建表语句如下
create table classes(
class_id integer primary key autoincrement,
student_id integer,
course_id integer
)
数据库开发
在Android studio 中新建一个安卓工程,在java目录下新家一个package,包名为database,将数据库相关的类放在这个包里面。
为方便数据库管理,每个表都用一个类来表示它们的结构。
下面代码表示表students的结构
/java/database/StudentTable.java
package database;
/**
*表示学生的表
*/
public class StudentTable {
//学生编号
public static final String ID = "_id";
//学生的姓名
public static final String NAME = "name";
//学生注册状态
public static final String STATE = "state";
//学生所在年级
public static final String GRADE = "grade";
//学的性别
public static final String GENDER = "gender";
//学生所修专业
public static final String MAJOR = "major";
//表名
public static final String TABLE_NAME = "students";
//创建学生表的语句
public static final String CREATE_STUDENT =
"create table " + StudentTable.TABLE_NAME + "("
+ ID + " integer primary key ,"
+ NAME + " text,"
+ MAJOR + " text,"
+ STATE + " text,"
+ GRADE + " integer,"
+ GENDER + " text)";
//删除学生表的语句
public static final String DROP_STUDENT =
"drop table if exists " + TABLE_NAME;
}
表示课程表结构的类
/java/database/CourseTable.java
package database;
/**
* 表示课程的表
*/
public class CouresTable {
//课程编号
public static final String ID = "_id";
//课程的名字
public static final String NAME = "course_name";
//表名
public static final String TABLE_NAME = "courses";
//创建课程表的sql语句
public static final String CREATE_COURESE =
"create table " + TABLE_NAME + "("
+ ID + " integer primary key autoincrement, "
+ NAME + " text)";
//删除课程表的语句
public static final String DROP_COURSE =
"drop table if exists " + TABLE_NAME;
}
表示班级表结构的类
/java/database/ClassTable
package database;
/**
* 表示班级的表
*/
public class ClassTable {
public static final String ID = "_id";
//