我一直在关注this教程,为我的Android应用程序执行SQLite的CRUD操作。 这些是我的代码
ScheduleSQLiteHelper.java
package com.skery.letspark.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.skery.letspark.entities.Schedule;
import java.util.LinkedList;
import java.util.List;
public class ScheduleSQLiteHelper extends SQLiteOpenHelper {
// Database version
private static final int DATABASE_VERSION = 1;
// Database name =
private static final String DATABASE_NAME = "LetsParkDB";
// Schedule table name
private static final String TABLE_SCHEDULE = "Schedule";
// Schedule table column names
private static final String SCHEDULE_ID = "scheduleID";
private static final String SCHEDULE_TIME = "scheduleTime";
private static final String SCHEDULE_DATE = "scheduleDate";
private static final String SCHEDULE_NAME = "scheduleName";
private static final String SCHEDULE_DESC = "scheduleDesc";
private static final String ROUTE_ID = "routeID";
private static final String[] COLUMNS = {SCHEDULE_ID, SCHEDULE_TIME, SCHEDULE_DATE, SCHEDULE_NAME, SCHEDULE_DESC
, ROUTE_ID};
public ScheduleSQLiteHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
// SQL statement to create the schedule table
String CREATE_SCHEDULE_TABLE = " CREATE TABLE Schedule ( " +
"scheduleID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"scheduleTime TEXT, " +
"scheduleDate TEXT, " +
"scheduleName TEXT, " +
"scheduleDesc TEXT, " +
"routeID INTEGER )";
// create schedule table
db.execSQL(CREATE_SCHEDULE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older schedule table if existed
db.execSQL("DROP TABLE IF EXISTS Schedule");
// Create fresh schedule table
this.onCreate(db);
}
// add schedule
public void addSchedule(Schedule schedule){
// for logging
Log.d("addSchedule", schedule.toString());
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create content values to add key column/value
ContentValues values = new ContentValues();
values.put(SCHEDULE_TIME, schedule.getScheduleTime());
values.put(SCHEDULE_DATE, schedule.getScheduleDate());
values.put(SCHEDULE_NAME, schedule.getScheduleName());
values.put(SCHEDULE_DESC, schedule.getScheduleDescription());
values.put(ROUTE_ID, schedule.getRouteId());
// 3. insert
db.insert(TABLE_SCHEDULE, //table
null, //nullColumnHack
values);
// 4. close
db.close();
}
// get schedule by id
public Schedule getSchedule(int scheduleID){
// 1. get reference to readable DB
SQLiteDatabase db = this.getReadableDatabase();
// 2. build query
Cursor cursor =
db.query(TABLE_SCHEDULE,
COLUMNS,
"scheduleID = ?",
new String[] {String.valueOf(scheduleID)},
null, // group by
null, // having
null, // order by
null); // limit
// 3. navigate through results
if(cursor!= null)
cursor.moveToFirst();
// 4. build schedule object
Schedule schedule = new Schedule();
schedule.setScheduleId(Integer.parseInt(cursor.getString(0)));
schedule.setScheduleTime(cursor.getString(1));
schedule.setScheduleDate(cursor.getString(2));
schedule.setScheduleName(cursor.getString(3));
schedule.setScheduleDescription(cursor.getString(4));
schedule.setRouteId(Integer.parseInt(cursor.getString(5)));
// log
Log.d("getBook(" + scheduleID + ")", schedule.toString());
return schedule;
}
// get all schedules
public List<Schedule> getAllSchedules(){
List<Schedule> schedules = new LinkedList<Schedule>();
// 1. build query
String query = "SELECT * FROM " + TABLE_SCHEDULE;
// 2. get reference to writable db
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// 3. go over each row, build book and add items to object
Schedule schedule = null;
if(cursor.moveToFirst()) {
do{
schedule = new Schedule();
schedule.setScheduleId(Integer.parseInt(cursor.getString(0)));
schedule.setScheduleTime(cursor.getString(1));
schedule.setScheduleDate(cursor.getString(2));
schedule.setScheduleName(cursor.getString(3));
schedule.setScheduleDescription(cursor.getString(4));
schedule.setRouteId(Integer.parseInt(cursor.getString(5)));
// add schedule object to list
schedules.add(schedule);
} while (cursor.moveToNext());
}
Log.d("getAllSchedules()", schedules.toString());
return schedules;
}
// update schedule
public int updateSchedule(Schedule schedule){
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create contentvalues to add
ContentValues values = new ContentValues();
values.put("scheduleTime", schedule.getScheduleTime());
values.put("scheduleDate", schedule.getScheduleDate());
values.put("scheduleName", schedule.getScheduleName());
values.put("scheduleDesc", schedule.getScheduleDescription());
values.put("routeID", schedule.getRouteId());
// 3. update row
int i = db.update(TABLE_SCHEDULE, // table
values, //
SCHEDULE_ID+" = ?",
new String[]{String.valueOf(schedule.getScheduleId())});
// 4. close
db.close();
return i;
}
}
安排实体,Schedule.java
package com.skery.letspark.entities;
public class Schedule {
private int scheduleId;
private String scheduleTime;
private String scheduleDate;
private String scheduleName;
private String scheduleDescription;
private int routeId;
public Schedule(){}
public Schedule(String scheduleTime, String scheduleDate, String scheduleName, String scheduleDescription, int routeId){
super();
this.scheduleTime = scheduleTime;
this.scheduleDate = scheduleDate;
this.scheduleName = scheduleName;
this.scheduleDescription = scheduleDescription;
this.routeId = routeId;
}
public int getScheduleId() {
return scheduleId;
}
public void setScheduleId(int scheduleId) {
this.scheduleId = scheduleId;
}
public String getScheduleTime() {
return scheduleTime;
}
public void setScheduleTime(String scheduleTime) {
this.scheduleTime = scheduleTime;
}
public String getScheduleDate() {
return scheduleDate;
}
public void setScheduleDate(String scheduleDate) {
this.scheduleDate = scheduleDate;
}
public String getScheduleName() {
return scheduleName;
}
public void setScheduleName(String scheduleName) {
this.scheduleName = scheduleName;
}
public String getScheduleDescription() {
return scheduleDescription;
}
public void setScheduleDescription(String scheduleDescription) {
this.scheduleDescription = scheduleDescription;
}
public int getRouteId() {
return routeId;
}
public void setRouteId(int routeId) {
this.routeId = routeId;
}
}
以及我如何在活动中执行它:
btnSaveSchedule.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// schedule items
String scheduleName = etScheduleName.getText().toString();
String scheduleDesc = etScheduleDesc.getText().toString();
String scheduleDate = Day + " " + selectedMonth + " " + Year;
tpScheduleTime.clearFocus();
int hour = tpScheduleTime.getCurrentHour();
int minute = tpScheduleTime.getCurrentMinute();
String scheduleTime = hour + ":" + minute;
//int routeID = Integer.parseInt(tvAppliedRoute.getText().toString());
int routeID = 12345;
ScheduleSQLiteHelper db = new ScheduleSQLiteHelper(PlannerNewScheduleActivity.this);
db.addSchedule(new Schedule(scheduleTime, scheduleDate, scheduleName, scheduleDesc,routeID));
}
});
但是,我的log cat中出现了一个java null指针异常。我尝试将db.open()
添加到我的代码中,但它显示语法错误。任何澄清都会非常有用,谢谢!