使用我自己的sqlite数据库时出现问题

时间:2011-01-13 13:59:43

标签: android database sqlite

我使用此代码将我的sqlite数据库从“Assets”目录复制到Android-phone“/ data / data / my_packname / databases /”目录。

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

    }

       ...
}

但是在行

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

我有 IOException

谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试此链接的答案代码。我对原始代码所做的更改进行了评论。因此,您可以更轻松地找到自己错误的内容。

Database not copying from assets