エラーが表示され続ける理由を教えてください。
アクティビティにスピナーと EditTexts があります。そのアクティビティでは、すでにandroid.database.sqlite.SQLiteCantOpenDatabaseException: Cannot to open database file が表示されています。
これが私のLogcatの言うことです:
07-03 16:43:03.156: E/SqliteDatabaseCpp(13449): sqlite3_open_v2("/data/data/ph.com.unilab.ireport/databases/lamp_db.sqlite", &handle, 1, NULL) failed
07-03 16:43:03.164: E/SQLiteDatabase(13449): Failed to open the database. closing it.
07-03 16:43:03.164: E/SQLiteDatabase(13449): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1123)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1074)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1050)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at ph.com.unilab.ireport.DatabaseHandler.checkDataBase(DatabaseHandler.java:81)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at ph.com.unilab.ireport.DatabaseHandler.createDB(DatabaseHandler.java:49)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at ph.com.unilab.ireport.S_9th_ISubmit.onCreate(S_9th_ISubmit.java:56)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.app.Activity.performCreate(Activity.java:4465)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.app.ActivityThread.access$600(ActivityThread.java:128)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.os.Looper.loop(Looper.java:137)
07-03 16:43:03.164: E/SQLiteDatabase(13449): at android.app.ActivityThread.main(ActivityThread.java:4514)
データベースからスピナーにデータを入力することはできますが、[保存] ボタンを押しても、ウィジェットの値がデータベースに保存されませんでした。
databaseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
static final String TAG = "LAMP";
public SQLiteDatabase db;
private final Context myContext;
private static DatabaseHandler dbHandler;
public DatabaseHandler(Context context) {
super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
this.myContext = context;
}
public static DatabaseHandler getInstance(Context context) {
if(dbHandler == null) {
dbHandler = new DatabaseHandler(context);
}
return dbHandler;
}
public void createDB() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist) {
//do nothing - database already exist
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath =Constants.DB_PATH + Constants.DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(Constants.DB_NAME);
// Path to the just created empty db
String outFileName = Constants.DB_PATH + Constants.DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = Constants.DB_PATH + Constants.DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(db != null)
db.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
このエラーの解決を手伝ってもらえますか?