2011-01-13 49 views
0

我使用此代码将我的sqlite数据库从“Assets”目录复制到Android手机“/ 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是否打开了它看起来没有的文件。可能拼写错误或路径不正确。 – Emile 2011-01-13 14:18:02

回答

0

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

Database not copying from assets