将表格从SD卡上的文件复制到App数据库表

时间:2015-07-15 20:03:25

标签: sql sqlite android-sqlite

我用一个表创建了数据库。我已成功上传并下载该数据库文件到“SD卡/ Dropbox”。我在app内部存储下载时存储该文件。现在我正在尝试从中复制表格该文件到app数据库表的不插入记录。

        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.db");
        if (myInternalFile.exists())
        { 
            db.delete_allsource_table();
            db.create_allsource_table();
            db.copy_to_allsource_table(currentDB.getAbsolutePath(),myInternalFile.getAbsolutePath());
        }

复制功能:

 public int copy_to_allsource_table(String dbpath,String backpath)
    {
        SQLiteDatabase db1 = this.getWritableDatabase();
        db1.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READWRITE);
        String attachquery="ATTACH '"+ dbpath +"' AS AAA";
        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 'AAA'.'"+ TABLE_CONTACTSDUMMY +"' SELECT * FROM 'YYY'.'"+ TABLE_CONTACTS +"'";

        Cursor cursor1 = db1.rawQuery(countQuery1, null);
       // String countQuery11 =" SELECT * FROM 'YYY'.'"+ TABLE_CONTACTS +"'";
       // Cursor cursor11 = db1.rawQuery(countQuery11, null);

        return cursor1.getCount();
    }

结果:No Record Inserted

但是'YYY.TABLE_CONTACTS'中存在记录。当我通过取消注释注释行来运行代码时返回no。记录。

0 个答案:

没有答案