2011-01-13 13 views
0

このコードを使用して、sqliteデータベースを "Assets"ディレクトリから "/ data/data/my_packname/databases /"ディレクトリにコピーします。自分のsqliteデータベースを使用したときの問題

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を持っています!

誰でも私が間違っていることを教えてもらえますか?

+0

あなたの書式設定を修正し、あなたのデータベース名が何であるかを示すことができるかどうかを確認してください。また、ブレークポイントを入れて、あなたのmyimputがiylooksが持っていないようなファイルを開いているかどうか確認してください。おそらく誤ったスペルやパス。 – Emile

+0

返信ありがとうございます。私はパスとデータベース名を確認しましたが、大丈夫です。 – Alex

+0

Emile、myInputがどのように開いているかわかりますか? – Alex

答えて

0

このリンクの回答コードをお試しください。私が元のコードに行った変更はコメントされています。あなたが間違っていることを見つけるのが簡単になります。

Database not copying from assets