为什么"未能chmod"会发生?

时间:2014-05-08 09:46:15

标签: android database sqlite

我将sqlite数据库复制到assets文件夹的代码是这样的。但出了点问题。 `私有语境;         private String db_path;

/*public static final String CREATE_TABLE="CREATE TABLE " + 
            TABLE_NAME +"  ("+
    KEY_ID+" INTEGER PRIMATY KEY AUTOINCREMENT, "+
    KEY_USERNAME+" INTEGER NOT NULL, "+
    KEY_PASSWORD+" VARCHAR NOT NULL, "+
    KEY_HOMEADDRESS+" VARCHAR, "+
    KEY_LICENSENO+" VARCHAR );";*/

我的DbHelper课程是

public DbHelper(Context context){
    super(context,"DATABASE_NAME",null,DATABASE_VERSION);
    this.DATABASE_NAME=DATABASE_NAME;
    this.context=context;
    db_path="/data/data/"+context.getPackageName()+"/databases/";
}

//create an empty database on the system to rewrite
public void crearDataBase() throws IOException{
    boolean dbExists=checkDataBase();
    if(dbExists){

    }else{
        this.getReadableDatabase();
        try{
            copyDataBase();
        }catch(IOException e){
            throw new Error("Error copying databse");
        }
    }
}

private boolean checkDataBase(){
    SQLiteDatabase checkDB=null;
    try{
        String myPath=db_path+DATABASE_NAME;
        checkDB=SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){

    }
    if(checkDB!=null){
        checkDB.close();
    }
    return checkDB!=null?true:false;
}

//copy database from local asset-folder to the system folder
private void copyDataBase() throws IOException{
    //open local database as the input stream
    InputStream myInput=context.getAssets().open(DATABASE_NAME);

    //path to the just created empty database
    String outFileName=db_path+DATABASE_NAME;

    //open the emoty database as the output stream
    OutputStream myOutput=new FileOutputStream(outFileName);

    //transfer bytes from the input file to the output file
    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=db_path+DATABASE_NAME;
    myDataBase=SQLiteDatabase.openDatabase(mypath, null,SQLiteDatabase.OPEN_READONLY); 

}*/


    @Override
    public void onCreate(SQLiteDatabase db) {
        //db.execSQL(CREATE_TABLE);
    }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    //delete table
    db.execSQL(DROP_TABLE);
    onCreate(db);

    }`  

并且log cat是这样的:

05-08 04:24:31.644: D/dalvikvm(7015): GC_FOR_ALLOC freed 124K, 6% free 3242K/3440K, paused 40ms, total 42ms
05-08 04:24:32.134: D/gralloc_goldfish(7015): Emulator without GPU emulation detected.
05-08 04:25:42.644: I/Choreographer(7015): Skipped 62 frames!  The application may be doing too much work on its main thread.
05-08 04:25:45.244: W/FileUtils(7015): Failed to chmod(/data/data/com.example.login/databases/DATABASE_NAME): libcore.io.ErrnoException: chmod failed: EPERM (Operation not permitted)

应用程序可以在模拟器上运行,但不能在手机上运行 另外,我的手机没有SD卡,外部sqlite数据库还可以吗?

1 个答案:

答案 0 :(得分:0)

1)排队

super(context,"DATABASE_NAME",null,DATABASE_VERSION);

我认为应该是

super(context,DATABASE_NAME,null,DATABASE_VERSION);

2)DATABASE_NAME的确值与资产中的db名称相同吗?

3)DATABASE_NAME应包含扩展名。像“mydb.db”这样的东西。