SQLite를 사용하고 데이터베이스 클래스를 작성하고 데이터베이스 테이블에 값을 삽입하려고했지만 예외가 발생했습니다.sqlite 데이터베이스에서 예외를 삽입하는 동안 해당 테이블을 해결하는 방법
I/Database(958): sqlite returned: error code = 1, msg = no such table: Exercise
그리고 난 고정 된 경우는이 문을 가리키는 로그 :
long d= dbAdapter.SaveExecise(date, exercise_Time, ex_Name, minutes, burned_cals);
왜이 예외가 발생? 내가 읽고 그런 문제의 해결책을 시도했지만 오류가 여전히 어떻게 그것을 해결할 수 있습니까? 내가 운동 테이블의 운동 정보를 삽입 여기
public class DBAdapter {
private static final String DB_NAME = "MYDB";
private static final int DB_VERSION = 1;
private static final String EXERCISE_TABLE_NAME = "Exercise";
private static final String TAG = "DBAdapter";
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
private Context context;
// table Exercise columns name
public static final String KEY_DATE = "Date";
public static final String KEY_TIME = "Time";
public static final String KEY_NAME = "Name";
public static final String KEY_PERIOD = "Period";
public static final String KEY_BURNEDCALS = "Burned_Calories";
private static final String EXERCISE_TABLE_CREATE =
"create tables Exercise (Date text not null , "
+ "Time text not null ,Name text not null,"
+ "Period REAL not null, Burned_Calories REAL not null,"
+ " primary key(Date,Time));";
public DBAdapter(Context ctxt) {
this.context = ctxt;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(EXERCISE_TABLE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version"
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS Exercise");
onCreate(db);
}
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
DBHelper.close();
}
// ---insert Exercise info to the Exercise table---
public long SaveExecise(String date, String time, String name, float period,
float BurnedCalories) {
ContentValues content = new ContentValues();
content.put(KEY_DATE, date);
content.put(KEY_TIME, time);
content.put(KEY_NAME, name);
content.put(KEY_PERIOD, period);
content.put(KEY_BURNEDCALS, BurnedCalories);
return db.insert(EXERCISE_TABLE_NAME, null, content);
}
}
}
과 : 당신은 테이블 대신 테이블의 뒀다
save_exercise_btn = (Button) findViewById(R.id.save_exercise_Btn);
save_exercise_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showSavingDialog();
// save the name,ime,burnedcals of the exercise in the DB
int year, month, day;
year = localCalendar.get(1);
month = localCalendar.get(2) + 1;
day = localCalendar.get(5);
String date = year + "/" + month + "/" + day;
DBAdapter dbAdapter = new DBAdapter(SelectedExerciseInfo.this);
dbAdapter = dbAdapter.open();
long d = dbAdapter.SaveExecise(date, exercise_Time, ex_Name, minutes, burned_cals);
dbAdapter.close();
hideSavingDialog();
}
});
포맷하세요 그것을 게시하기 전에 귀하의 코드! 방금 당신을 위해했기 때문에 우리는 실제로 무엇이 잘못되었을 지 알 수 있습니다. –