dimanche 19 juillet 2015

Sqllite Insert Query Multiple Database

I had written following code to insert records into table from the table of other database file.But unable even after execution of sql statement it shows that there are no records in table.

     public int copy_to_all_source_table(String dbpath,String backpath)
        {
            SQLiteDatabase db1 = this.getWritableDatabase();

               //Opening App database(i.e. dbpath) and attaching it as "OLD" 

            db1.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READWRITE);
            String attach_old="ATTACH '"+ dbpath +"' AS OLD";
            db1.execSQL(attach_old);



       //Opening New File which is Student.db(i.e. dbpath) and attaching it as "NEW"        

            db1.openDatabase(backpath, null, SQLiteDatabase.OPEN_READWRITE);
            String attach_new="ATTACH '"+ backpath +"' AS NEW";
            db1.execSQL(attach_new);

            // Getting count of records in table of "NEW" 

            String new_query =" SELECT * FROM 'NEW'.'"+ TABLE_CONTACTS +"'";
            Cursor new_data = db1.rawQuery(new_query, null);          
            Integer new_count= new_data.getCount();


              //INSERTING ALL RECORDS FROM TABLE OF NEW TO TABLE OF OLD

            String insert_query ="INSERT INTO 'OLD'.'"+ TABLE_CONTACTS +"' SELECT * FROM 'NEW'.'"+ TABLE_CONTACTS +"'";
            Cursor success_insert = db1.rawQuery(insert_query, null);

             // Getting count of records in table of "NEW"

            String after_insert_old_query =" SELECT * FROM 'OLD'.'"+ TABLE_CONTACTS +"'";
            Cursor old_data = db1.rawQuery(after_insert_old_query, null);             
            Integer old_count= old_data.getCount();
        }

RESULT:

       new_count=11
       old_count=0

    Means record not inserted.

Aucun commentaire:

Enregistrer un commentaire