2014-05-21 4 views
0

내 데이터베이스에 문제가 있습니다. 응용 프로그램 시작시 데이터베이스가 자산에서 자동으로로드됩니다. 이미 존재하지 않는 경우에만로드해야하며 (응용 프로그램의 처음 시작) 작동하지 않습니다.데이터베이스가 존재하는지 확인하십시오.

public static boolean copyDBFile(Context context) 
{ 
    File dbAbsolutePathFile = new File(dbPath + dbName); 
    File dbPathFile = new File(dbPath); 

    //Here we make sure that the directory path for the database exists 
    if(!dbPathFile.exists()) { 
     dbPathFile.mkdirs(); 

    } 



    File dbNameFile = context.getDatabasePath(dbName); 

    //Here we check whether the database file exists or not. If not, we then create it 
    if(!dbNameFile.exists()) { 
     Log.d("database", "copy"); 
     //dbNameFile = new File(dbName); 
     try { 

      //Here we call the copyDB() method. 
      copyDB(context.getAssets().open(dbName), new FileOutputStream(dbPath + dbName)); 

     } catch (FileNotFoundException e) { 
     } catch (IOException e) { 
     } 

    }  
    return true; 
} 

private static void copyDB(InputStream inputStream, OutputStream outputStream) { 


    byte[] buffer = new byte[1024]; 
    int length; 

    //Here we copy 1K bytes at a time 
    try { 
     while((length=inputStream.read(buffer))>0) { 
      outputStream.write(buffer, 0, length); 
     } 

     inputStream.close(); 
     outputStream.close(); 

    } catch (IOException e) { 

    } 
} 
+0

는 왜 빈'catch' 블록이 있습니까 데이터베이스 가져 오기 위해이 코드

public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); if(! MyDBHelper.isDatabaseExist(Your_Context , YOUR_DATABASE_NAME)) new MyDBHelper(this).ImportDatabase(this); } } public static boolean isDatabaseExist(ContextWrapper context, String dbName) { File dbFile = context.getDatabasePath(dbName); return dbFile.exists(); } 

을 시도해보십시오 여기

내 코드? –

+0

무엇이 작동하지 않습니까? 애플리케이션이 전혀 시작되지 않거나 체크 코드가 작동하지 않습니다. – Saqib

+0

몇 분 전에는 비어 있지 않았지만 내용에 정적 방법에 문제가있었습니다. 먼저 catch 블록을 채우기 전에 수정해야합니다. – honiahaka10

답변

1

public Boolean ImportDatabase(Context c) 
    { 
     InputStream myInput = null; 

     try { 


      myInput = c.getAssets().open("Assets/Database.db"); 


      // Set the output file stream up: 


      SQLiteDatabase db = getReadableDatabase(); 
      OutputStream myOutput = new FileOutputStream(db.getPath()); 
      db.close(); 

      // Transfer bytes from the input file to the output file 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = myInput.read(buffer))>0) 
      { 
       myOutput.write(buffer, 0, length); 
      } 
      // Close and clear the streams 

      myOutput.flush(); 
      myOutput.close(); 
      myInput.close(); 
     } 
     catch (FileNotFoundException e) 
     { 
      Log.e("Restoring Database", "file not found"); 
      e.printStackTrace(); 
      return false; 
     } 
     catch (IOException e) 
     { 
      Log.e("Restoring Database", "IO exception"); 
      e.printStackTrace(); 
      return false; 
     } 
     Log.e("Restoring Database", "Restored"); 
     return true; 
    } 
+0

문법적으로 잘못된 함수 이름을 제외하고 질문의 코드와 다른 점은 무엇입니까? –

+0

은이 메소드를 호출하는 컨텍스트에 따라 다릅니다. @CL. – Andrain

+0

어디에서이 함수를 호출할까요? @ honiahaka10 – Andrain

관련 문제