DatabaseHelper

pull/12/head
lisiqidaer 4 years ago
commit 7857e30487

@ -0,0 +1,33 @@
package com.showme.database.DatabaseHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class CourseSQLHelper extends SQLiteOpenHelper {
private static final String db_name = "course";//自定义的数据库名;
private static final int version = 2;//版本号
public CourseSQLHelper(Context context) {
super(context, db_name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table courses(" +
"id integer primary key autoincrement," +
"course_name text," +
"teacher text," +
"class_room text," +
"day integer," +
"class_start integer," +
"class_end integer," +
"weeks text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists schedules");
onCreate(db);
}
}

@ -0,0 +1,39 @@
package com.showme.database.DatabaseHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ScheduleSQLHelper extends SQLiteOpenHelper {
private static final String db_name = "MySchedule";//自定义的数据库名;
private static final int version = 98889;//版本号
public ScheduleSQLHelper(Context context) {
super(context, db_name, null, version);
}
// 该方法会自动调用首先系统会检查该程序中是否存在数据库名为MySchedule的数据库
// 如果存在则不会执行该方法,如果不存在则会执行该方法。
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table schedules(" +
"id Integer primary key autoincrement," + //id自增,只支持integer不支持int
"scheduleName varchar(50)," +
"schedulePlace varchar(50)," +
"ifSchedulePlace integer," +
"startTime varchar(50)," +
"ifStartTime integer," +
"endTime varchar(50)," +
"ifEndTime interger," +
"time varchar(30)" +
")";
db.execSQL(sql);
}
//数据库版本更新时执行该方法,如果表已存在则先删除再调用onCreate重新创建
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists schedules");
onCreate(db);
}
}

@ -0,0 +1,10 @@
package com.showme.database.utils;
import android.database.sqlite.SQLiteDatabase;
import com.showme.database.MySQLiteDatabase;
public class CourseSQLUtils {
private static SQLiteDatabase courseSQL = MySQLiteDatabase.courseSQL;
}

@ -0,0 +1,69 @@
package com.showme.database.utils;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.showme.database.MySQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class ScheduleSQLUtils {
private static SQLiteDatabase database = MySQLiteDatabase.scheduleSQL;
public static List<String> queryByDate(String date) {
//columns为null 查询所有列
List<String> res = new ArrayList<>();
String aScheduleName;
Cursor cursor = database.query("schedules",null,"time=?",new String[]{date},null,null,null);
if(cursor.moveToFirst()){
int scheduleCount = 0;
do{
aScheduleName = cursor.getString(cursor.getColumnIndex("scheduleName"));
String aStartTime = cursor.getString(cursor.getColumnIndex("startTime"));
Integer ifStartTime = cursor.getInt(cursor.getColumnIndex("ifStartTime"));
String aEndTime = cursor.getString(cursor.getColumnIndex("endTime"));
Integer ifEndTime = cursor.getInt(cursor.getColumnIndex("ifEndTime"));
String scheduleText = aScheduleName;
if(ifStartTime == 1){
scheduleText += "($时间:"+aStartTime;
if(ifEndTime ==1){
scheduleText += "\n"+aEndTime;
} else {
scheduleText += "\n24:00";
}
} else {
scheduleText += "($时间: 00:00\n24:00";
}
scheduleCount++;
res.add(scheduleText);
}while (cursor.moveToNext());
}
cursor.close();
return res;
}
public static String extractScheduleName(String scheduleText) {
int index = scheduleText.indexOf("($时间:");
if (index != -1) {
return scheduleText.substring(0, index);
}
return null;
}
public static String extractScheduleTime(String scheduleText) {
int index = scheduleText.indexOf("($时间:");
if (index != -1) {
return scheduleText.substring(index+5);
}
return null;
}
public static boolean isScheduleMarked(String date) {
Cursor cursor = database.query("schedules",null,"time=?",new String[]{date},null,null,null);
return cursor.moveToFirst();
}
}
Loading…
Cancel
Save