android 如何实现附加现有数据库?而不是每次安装的时候都创建

就是先将数据库存到raw上,然后在第一次安装的时候复制到sdcard上,以后就直接打开数据库了。
一下是代码:

	 private Context context; 
	 private String rootDirectory = "/data/data/CrazyNovelV1_0.com/data/";
	 private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
     + "/crazynovel";
	 private final String DATABASE_FILENAME = "crazynovel.db";

//复制小于1M的数据库程序	    
	    public SQLiteDatabase openDatabase()   
	    {   
	        try  
	        {   
	            // 获得dictionary.db文件的绝对路径   
	            String databaseFilename = DATABASE_PATH+ "/" + DATABASE_FILENAME;   
	            File dir = new File(DATABASE_PATH);   
	            // 如果/sdcard/dictionary目录不中存在,创建这个目录   
	            if (!dir.exists())   
	                dir.mkdir();   
	            // 如果在/sdcard/dictionary目录中不存在   
	            // dictionary.db文件,则从res\raw目录中复制这个文件到   
	            // SD卡的目录(/sdcard/dictionary)   
	            if (!(new File(databaseFilename)).exists())   
	            {   
	                // 获得封装dictionary.db文件的InputStream对象   
	                InputStream is = context.getResources().openRawResource(R.raw.crazynovel);   
	                FileOutputStream fos = new FileOutputStream(databaseFilename);   
	                byte[] buffer = new byte[7168];   
	                int count = 0;   
	                // 开始复制dictionary.db文件   
	                while ((count = is.read(buffer)) > 0)   
	                {   
	                    fos.write(buffer, 0, count);   
	                }   
	                fos.close();   
	                is.close();   
	            }   
	            // 打开/sdcard/dictionary目录中的dictionary.db文件   
	            SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);   
	            return database;   
	        }   
	        catch (Exception e)   
	        {   
	        }   
	    }  


小于1M的可以用上面的方法复制,但是如果文件大于1M则不行,需要分割后再合并,最终拷贝到sdcard上。

以下是解决方案:

private Context context; 
     private String rootDirectory = "/data/data/CrazyNovelV1_0.com/data/";
     private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
     + "/crazynovel";
     private final String DATABASE_FILENAME = "crazynovel.db";

//复制小于1M的数据库程序        
        public SQLiteDatabase openDatabase()   
        {   
            try  
            {   
                // 获得dictionary.db文件的绝对路径   
                String databaseFilename = DATABASE_PATH+ "/" + DATABASE_FILENAME;   
                File dir = new File(DATABASE_PATH);   
                // 如果/sdcard/dictionary目录不中存在,创建这个目录   
                if (!dir.exists())   
                    dir.mkdir();   
                // 如果在/sdcard/dictionary目录中不存在   
                // dictionary.db文件,则从res\raw目录中复制这个文件到   
                // SD卡的目录(/sdcard/dictionary)   
             if (!(new File(databaseFilename)).exists())   
		            {   
		            	//复制文件
		            	copydb(R.raw.crazynovel1,DATABASE_PATH+ "/crazynovel1",944128);
		            	copydb(R.raw.crazynovel2,DATABASE_PATH+ "/crazynovel2",944128);
		            	copydb(R.raw.crazynovel3,DATABASE_PATH+ "/crazynovel3",944128);
		            	copydb(R.raw.crazynovel4,DATABASE_PATH+ "/crazynovel4",944128);
		            	copydb(R.raw.crazynovel5,DATABASE_PATH+ "/crazynovel5",944128);
		            	copydb(R.raw.crazynovel6,DATABASE_PATH+ "/crazynovel6",944128);
		            	copydb(R.raw.crazynovel7,DATABASE_PATH+ "/crazynovel7",944128);
		            	copydb(R.raw.crazynovel8,DATABASE_PATH+ "/crazynovel8",944128);
		            	copydb(R.raw.crazynovel9,DATABASE_PATH+ "/crazynovel9",944128);
		            	copydb(R.raw.crazynovel10,DATABASE_PATH+ "/crazynovel10",944128);
		            	copydb(R.raw.crazynovel11,DATABASE_PATH+ "/crazynovel11",944128);
		            	copydb(R.raw.crazynovel12,DATABASE_PATH+ "/crazynovel12",669696);
		            	


		            	//合并文件
		            	File[] files = new File[12];
		            	files[0] = new File(DATABASE_PATH+ "/crazynovel1");
		            	files[1] = new File(DATABASE_PATH+ "/crazynovel2");
		            	files[2] = new File(DATABASE_PATH+ "/crazynovel3");
		            	files[3] = new File(DATABASE_PATH+ "/crazynovel4");
		            	files[4] = new File(DATABASE_PATH+ "/crazynovel5");
		            	files[5] = new File(DATABASE_PATH+ "/crazynovel6");
		            	files[6] = new File(DATABASE_PATH+ "/crazynovel7");
		            	files[7] = new File(DATABASE_PATH+ "/crazynovel8");
		            	files[8] = new File(DATABASE_PATH+ "/crazynovel9");
		            	files[9] = new File(DATABASE_PATH+ "/crazynovel10");
		            	files[10] = new File(DATABASE_PATH+ "/crazynovel11");
		            	files[11] = new File(DATABASE_PATH+ "/crazynovel12");
		            	

		            	FileOutputStream fos = new FileOutputStream(databaseFilename);   
		            	CreateFromRawDbFiles(files,fos);		            }   
            catch (Exception e)   
            {   
            }   
        }  
//复制文件
	  private void copydb(int dbint,String databaseFilename,int FileSize)
	  {
		  try
		  {
	          // 获得封装dictionary.db文件的InputStream对象   
			  InputStream is = context.getResources().openRawResource(dbint);   
	          FileOutputStream fos = new FileOutputStream(databaseFilename);   
	          byte[] buffer = new byte[FileSize];   
	          int count = 0;   
	          // 开始复制dictionary.db文件   
	          while ((count = is.read(buffer)) > 0)   
	          {   
	              fos.write(buffer, 0, count);   
	          }   
	          fos.close();   
	          is.close();
		  }
		  catch(Exception e)   
		  {}
	  }
	    
	    
	  //合并并拷贝数据
	  private void CreateFromRawDbFiles(File[] filelist,FileOutputStream Fos)
	  {
		try
	  	{
		  for (File file : filelist)
		  {
		  InputStream inputFile = new FileInputStream(file);
		  int TotalLength = 0;
		  try 
		  {
			  TotalLength = inputFile.available();
		  } 
		  catch (IOException e)
		  {
		  }
	
		  // Reading and writing the file Method 1 :
		  byte[] buffer = new byte[TotalLength];
		  int len = 0;
		  try
		  {
			  len = inputFile.read(buffer);
		  } 
		  catch (IOException e)
		  {
		  }
		  Fos.write(buffer,0,len);
		  inputFile.close();
		  }
		  Fos.close();
		 }
		  catch(IOException e)
		  {}
	}




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值