2

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

我的活动中有微调器和 EditTexts。在那个活动中,它已经显示了android.database.sqlite.SQLiteCantOpenDatabaseException: unable 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)

我可以将数据库中的数据填充到微调器,但是,无论我在哪里按下“保存”按钮,小部件中的值都没有保存到数据库中。

数据库处理程序.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) {

    }

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

4

2 回答 2

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 );");

}
于 2013-07-03T09:24:23.980 回答
1

你有这一行:

this.getReadableDatabase();

你可以用这个方法写。

将其更改为:

this.getWritableDatabase();
于 2013-07-03T09:16:18.773 回答