开发者

Problem with using my own sqlite database

I use this code to copy my sqlite database from "Assets" directory to Android-phone "/data/data/my_packname/databases/" directory.

public class DataBaseHelper extends SQLiteOpenHelper{   

    private SQLiteDatabase myDataBase; 

    private final Context myContext;

    public DataBaseHelper(Context context) {

     super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.myContext = context;
    } 

  ...

    private void copyDataBase() throws IOException{

     //Open your开发者_如何学C local db as the input stream

     AssetManager am = this.myContext.getAssets();

     InputStream myInput = am.open(DATABASE_NAME);


     // Path to the just created empty db
     String outFileName = DATABASE_PATH + DATABASE_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();

    }

       ...
}

But in line

 while ((length = myInput.read(buffer)) > 0){

i have IOException!

Can anyone tell me what i'm doing wrong?


Try the code of answer of this link. the changes which I have done to the original code are commented.So it'll be easier for you to find what you are doing wrong.

Database not copying from assets

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜