mercredi 15 juillet 2015

Copy Table from file on SD card to App database table

I have created database with one table.I uploaded and downloaded that database file to and from "SD card/Dropbox" successfully.I am storing that file upon downloading in app internal storage.Now when i am trying to copy table from that file to app database table its showing error that "No such table" in internal storage file.Please help does it does not recognize internal storage file as a database file.

        File data = Environment.getDataDirectory();
        db= new DatabaseHandler(getActivity().getApplicationContext());
        String currentDBPath = "//data//"+ "loginscreen.example.com.girviapp" +"//databases//"+DATABASE_NAME;
        Context context = getActivity().getApplicationContext();
        String con=context.getDatabasePath(currentDBPath).getAbsolutePath();
        File currentDB = new File(data, currentDBPath);
        ContextWrapper contextWrapper = new ContextWrapper(getActivity().getApplicationContext());
        File directory = contextWrapper.getDir("GirviDrop", Context.MODE_PRIVATE);
        File myInternalFile = new File(directory ,"GirviDrop");
        if (myInternalFile.exists())
        { 
            db.delete_allsource_table();
            db.create_allsource_table();
            db.copy_to_allsource_table(currentDB.getAbsolutePath(),myInternalFile.getAbsolutePath());
        }

Function for Copy:

 public void copy_to_allsource_table(String dbpath,String backpath)
    {
        SQLiteDatabase db1 = this.getWritableDatabase();
        db1.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READWRITE);
        String attachquery="ATTACH '"+ dbpath +"' AS XXX";
        db1.execSQL(attachquery);

        SQLiteDatabase db2 = this.getWritableDatabase();
        db1.openDatabase(backpath, null, SQLiteDatabase.OPEN_READWRITE);
        String attachquery2="ATTACH '"+ backpath +"' AS YYY";
        db1.execSQL(attachquery2);
        String countQuery1 ="INSERT INTO 'XXX'.'"+ TABLE_CONTACTSDUMMY +"' SELECT * FROM 'YYY'.'"+ TABLE_CONTACTS +"'";

        Cursor cursor1 = db1.rawQuery(countQuery1, null);
    }

Error: "No Such Table YYY.TABLE_CONTACTS"

Aucun commentaire:

Enregistrer un commentaire