package com.yu.testSQL;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.content.Context;public class ProvinceDB {

Context mContext;
public ProvinceDB(Context context){
this.mContext=context;
}
private final static String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath() + "/testsql";
private final static String DATABASE_FILENAME = "testsql.db"; public void openDB(){
try{
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
if (!dir.exists())
//exists  存在的
dir.mkdir();
//判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
if(!(new File(databaseFilename)).exists())
{
//欲导入的数据库
InputStream is = mContext.getResources()
.openRawResource(R.raw.testsql);
//每次都这里报错,说是找不到文件还是什么的!明明在raw文件夹下面有那个数据库文件,真心郁闷
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
while((count=is.read(buffer))>0){
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
}catch(Exception e){
e.printStackTrace();
}

}}
android数据库

解决方案 »

  1.   

    InputStream is = mContext.getResources()
    .openRawResource(R.raw.testsql);一个是流 一个是文件 你是否要转化一下?
      

  2.   

     if(!(new File(databaseFilename)).exists())    //能执行里面的代码,这里都说明了这个文件不存在了赛
            {
                //欲导入的数据库
                InputStream is = mContext.getResources()
    .openRawResource(R.raw.testsql);
    //还执行下面一句,怎么想的
                FileOutputStream fos = new FileOutputStream(databaseFilename);
      

  3.   

          呵呵,其实这个是可以的!我忘记了一个低级的错误!就是要想在SD卡中写入数据必须得在XML文件中写权限!