今天做项目的时候为了测试我把模拟器里的项目删除了,然后重新运行项目报
09-26 06:47:23.822: ERROR/AndroidRuntime(781): java.lang.RuntimeException: Unable to start receiver com.noteWidget.Note: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 1: /data/data/com.noteWidget/databases/note_db
rollback ,和数据库不能打开异常
我在网上求助,多亏好友郑方提醒rollback然后我同事又帮我看了下,这时我才想起上次我在创建表的方法里把数据库给关了
public class CourseDAO extends SQLiteOpenHelper{
private final static String DATABASE_NAME="course_db";
private final static int DATABASE_VERSION=1;
private final static String TABLE_NAME1="course_table";
public final static String COURSE_ID="_id";
public final static String COURSE_NAME="course_name";
public final static String COURSE_TEACHER="course_teacher";
public final static String COURSE_DATE="course_time";
public final static String COURSE_ADDRESS="course_address";
public final static String COURSE_NUMBER="course_number";
private SQLiteDatabase db;
public String CREATE_TABLE_SQL ="CREATE TABLE "+ TABLE_NAME1 +"("+ COURSE_ID
+" INTEGER primary key autoincrement," + " "
+ COURSE_NAME + " text ," + " "+ COURSE_TEACHER + " text," + " "+ COURSE_DATE + " text" +
"," + " "+ COURSE_ADDRESS + " text," + " "+ COURSE_NUMBER + " text)";
public CourseDAO(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
String courseTable = "CREATE TABLE "+ TABLE_NAME1 +"("+ COURSE_ID
+" INTEGER primary key autoincrement," + " "
+ COURSE_NAME + " text ," + " "+ COURSE_TEACHER + " text," + " "+ COURSE_DATE + " text" +
"," + " "+ COURSE_ADDRESS + " text," + " "+ COURSE_NUMBER + " text)";
db.execSQL(courseTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS course_table");
onCreate(db);
}
/*根据星期查询*/
public Cursor queryByWeek(String week)
{
SQLiteDatabase dbDatabase = getReadableDatabase();
Cursor cursor =dbDatabase.query(TABLE_NAME1,
new String[]{COURSE_ID,
COURSE_NAME,
COURSE_TEACHER,
COURSE_DATE,
COURSE_ADDRESS,
COURSE_NUMBER},
"course_table.course_time="+"'"+week+"'", null, null, null, "course_table.course_number asc");
return cursor;
}
/*根据Id查询*/
public Cursor queryByInfoIdCursor(int infoId)
{
SQLiteDatabase dbDatabase = getReadableDatabase();
Cursor cursor =dbDatabase.query(TABLE_NAME1,new String[]{COURSE_ID,COURSE_TEACHER,COURSE_ADDRESS}, "course_table._id="+infoId, null, null, null, null);
return cursor;
}
/*根据Id查询*/
public Cursor queryByInfoId(int infoId)
{
SQLiteDatabase dbDatabase = getReadableDatabase();
Cursor cursor =dbDatabase.query(TABLE_NAME1,new String[]{COURSE_NAME,COURSE_NUMBER,COURSE_TEACHER,COURSE_ADDRESS}, "course_table._id="+infoId, null, null, null, null);
return cursor;
}
/*根据上课时间和上课节次查询*/
public Cursor queryByTimeAndNumber(String time,String number)
{
SQLiteDatabase db = getWritableDatabase();
String sql="select p._id,p.course_name" +
" from course_table as p where p.course_time="+"'"+time+"'" +"and p.course_number="+"'"+number+"'" ;
Cursor cursor=db.rawQuery(sql, null);
return cursor;
}
/*插入课程信息*/
public long course_insert(String name,String teacher,String date,String address,String number){
SQLiteDatabase dbDatabase=getWritableDatabase();
ContentValues cValues=new ContentValues();
cValues.put(COURSE_NAME, name);
cValues.put(COURSE_TEACHER,teacher);
cValues.put(COURSE_DATE,date);
cValues.put(COURSE_ADDRESS,address);
cValues.put(COURSE_NUMBER,number);
long row=dbDatabase.insert(TABLE_NAME1, null, cValues);
//sub_insert((int)row, subGroup);
return row;
}
/*更改课程信息*/
public long course_update(int id,String name,String teacher,String date,String address,String number){
SQLiteDatabase dbDatabase=getWritableDatabase();
ContentValues cValues=new ContentValues();
cValues.put(COURSE_NAME, name);
cValues.put(COURSE_TEACHER,teacher);
cValues.put(COURSE_DATE,date);
cValues.put(COURSE_ADDRESS,address);
cValues.put(COURSE_NUMBER,number);
String where = COURSE_ID+"= ?";
String[] whereValue={Integer.toString(id)};
long row=dbDatabase.update(TABLE_NAME1, cValues,where ,whereValue);
//sub_insert((int)row, subGroup);
return row;
}
//删除课程信息
public void deleteCourse(int id)
{
SQLiteDatabase db = getWritableDatabase();
String where = COURSE_ID+ "= ?";
String[] whereValue={Integer.toString(id)};
db.delete(TABLE_NAME1, where, whereValue);
}