上記のエラーが発生する理由を知っている人はいますか?
コードは次のとおりです。
これは、すべてのデータベース情報を保持するクラスです。
package com.petroc.nationaldiploma;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_GRADES = "grades";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_GRADE = "grade";
public static final String COLUMN_MODULE = "module";
private static final String DATABASE_NAME = "grades.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_GRADES + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_GRADE
+ " text not null" + COLUMN_MODULE + " text not null" + ");";
/*
* private static final String DATABASE_CREATE = "create table " +
* TABLE_GRADES + "(" + COLUMN_ID + " integer primary key autoincrement, " +
* COLUMN_GRADE + " text not null);";
*/
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_GRADES);
onCreate(db);
}
}
これは、データベースの作成と操作に使用されるメソッドを作成するクラスです
package com.petroc.nationaldiploma;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class GradesDataSource {
// Database fields
private SQLiteDatabase database;
private final MySQLiteHelper dbHelper;
private final String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_GRADE, MySQLiteHelper.COLUMN_MODULE };
/*private final String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_GRADE };*/
public GradesDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Grade createGrade(String grade, String module) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_GRADE, grade);
values.put(MySQLiteHelper.COLUMN_MODULE, module);
long insertId = database.insert(MySQLiteHelper.TABLE_GRADES, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_GRADES, allColumns,
MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null,
null);
cursor.moveToFirst();
Grade newGrade = cursorToGrade(cursor);
cursor.close();
return newGrade;
}
/*
* public Grade createGrade(String grade) { ContentValues values = new
* ContentValues(); values.put(MySQLiteHelper.COLUMN_GRADE, grade); long
* insertId = database.insert(MySQLiteHelper.TABLE_GRADES, null, values);
* Cursor cursor = database.query(MySQLiteHelper.TABLE_GRADES, allColumns,
* MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null, null);
* cursor.moveToFirst(); Grade newGrade = cursorToGrade(cursor);
* cursor.close(); return newGrade; }
*
* public void deleteGrade(Grade grade) { long id = grade.getId();
* System.out.println("Grade deleted with id: " + id);
* database.delete(MySQLiteHelper.TABLE_GRADES, MySQLiteHelper.COLUMN_ID +
* " = " + id, null); }
*/
public List<Grade> getAllGrades() {
List<Grade> grades = new ArrayList<Grade>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_GRADES, allColumns,
null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Grade grade = cursorToGrade(cursor);
grades.add(grade);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return grades;
}
private Grade cursorToGrade(Cursor cursor) {
Grade grade = new Grade();
grade.setId(cursor.getLong(0));
grade.setGrade(cursor.getString(1));
return grade;
}
}