0

Here is my code. I have created a .db file using "sqlite browser" and placed into "assets"folder of my work space. But the '.db ' file is not copied into application path (when I checked in DDMS(data/data/com.example.trans/) databases folder is not created). Also .db file is not copied from assets folder.

Here is my code:

public class ConnectionFactory  extends SQLiteOpenHelper{
    private static String DB_Path = "/data/data/com.example.translationapp/databases/";
    public static  String DB_Name = "Translator.db";
    public SQLiteDatabase myDb;
    File dbFile;
    public Context con;

    public ConnectionFactory(Context context) {
        super(context, DB_Name, null,1);

        // TODO Auto-generated constructor stub
        this.con= context;
    }

    public void OpenDb() throws IOException 
    {
        boolean dbCheck = checkDb();
        if(dbCheck)
        {

        }
        else
        {
            this.getWritableDatabase();
            try
            {
                copyDatabase();
            }
            catch(IOException e)
            {
                throw new Error("Sorry");
            }
        }
    }
    public boolean checkDb() throws IOException
    {
        SQLiteDatabase checkDb =null;
        try
        {
        String myPath = DB_Path+DB_Name;
        checkDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        }
        catch(SQLiteException e)
        {

        }
        if(checkDb!= null)
        {
          checkDb.close();  
        }
        return checkDb !=null ? true:false;
    }
   public void copyDatabase()throws IOException
   {
       try
       {
       File filetext = con.getFileStreamPath(DB_Name);
       boolean exists = filetext.exists();
       if(!exists)
       {
       String outFileName = DB_Path+ DB_Name;

       myDb=con.openOrCreateDatabase(outFileName, Context.MODE_PRIVATE, null );
       OutputStream myOut  = new FileOutputStream(filetext.getAbsolutePath());
       InputStream myin = con.getAssets().open(DB_Name);
       byte[] buffer = new byte[1024];
          int length;
          while((length = myin.read(buffer))>0)
          {
              myOut.write(buffer,0,length);
          }
          myOut.flush();
          myOut.close();
          myin.close();
       }
       }
       catch(IOException e)
       {
           e.printStackTrace();
       }

   }
   public void openDatabase() throws SQLException
   {
      String myPath = DB_Path+DB_Name;
     myDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

   }
   public synchronized void close()
   {
      if(myDb != null)
      {
         myDb.close();
          super.close();
      }
   }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}
APC
  • 144,005
  • 19
  • 170
  • 281
Raviteja
  • 1
  • 1

1 Answers1

1

Instead of hardcoding db path in assets folder, just use this -

private static String DB_Path = "/data/data/"+getPackageName()+"/databases";
  • But when the db file from assets is not copying into DDMS. How can we expect that this query will execute . primarly it is throwing an error that " un able to open databaase"(error = 14) – Raviteja Mar 10 '13 at 12:03
  • @DjHacktorReborn DDMS mode does't come in any android devices :-P –  Mar 10 '13 at 12:17
  • @Andi your answer wont work by mistake i have voted it and cant change unless you edit – DjHacktorReborn Mar 10 '13 at 12:22