无法打开sqlite数据库文件

时间:2013-07-03 09:06:03

标签: android sqlite

有人可以帮助我解决错误不断出现的原因。

我的活动中有微调器和EditTexts。在该活动中,它已经显示 android.database.sqlite.SQLiteCantOpenDatabaseException:无法打开数据库文件

以下是我的 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) {

    }

你能帮忙解决这个错误吗?

2 个答案:

答案 0 :(得分:3)

正如dasdasd所说,使用

this.getWritableDatabase();
而不是 this.getReadableDatabase();

然后,在onCreate中创建数据库。首次创建数据库时会调用OnCreate。这是表的创建和表的初始填充的地方。

例如

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS " + tableName + "(" + id
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + startDate
            + " LONG , " + url + " TEXT, " + title + " TEXT, " + endDate
            + " LONG, " + sentToCloud + " INTEGER DEFAULT 0 );");

}

答案 1 :(得分:1)

你有这一行:

this.getReadableDatabase();

你可以用这种方法写作。

将其更改为:

this.getWritableDatabase();