Android sqlite数据库导出/导入工作但不显示数据

时间:2013-08-16 20:47:48

标签: android database sqlite import export

我正在努力让我的sqlite数据库导出到sdcard作为备份,然后将信息导回到我的应用程序。我已经搜索了我可以尝试的所有主题,并且无需提出任何问题就可以解决这个问题,但现在我已经用完了有用的信息。我似乎非常非常接近完成这个...这就是我的位置:我正在关注这个基本的例子http://androidgenuine.com/?tag=importexport-android-database。一切似乎工作得很好,我可以看到我用我的备份文件创建的文件夹,如果我打开文件文本,我可以看到它从我的数据库中保存我的信息。然后我将保存的文件导回到我的应用程序,一切似乎都顺利。问题是我从我的备份加载的新信息没有显示在我的应用程序中,它仍然显示与导入之前相同的数据。奇怪的是,如果我在导入数据库后导出我的数据库导出文件是应该显示的正确数据,所以当我导入它时,我显然正在获取正确的数据但是没有显示,而是显示任何旧数据我在导入之前有过。再次,它工作,但似乎没有刷新我的应用程序正在读取的数据。谢谢你对此的任何帮助,我很难过。

这是我的导入

    myOutput = new FileOutputStream("/data/data/my_project/databases/empdb.db");


            // Set the folder on the SDcard
            File directory = new File("/sdcard/CarMaintenanceBackup");
            // Set the input file stream up:

   InputStream myInputs = new FileInputStream(directory.getPath()+ "/empdb.backup");



            // Transfer bytes from the input file to the output file
            byte[] buffer = new byte[1024];
            int length;

            while ((length = myInputs.read(buffer))>0)
            {
                myOutput.write(buffer, 0, length);

            }


            // Close and clear the streams
            myOutput.flush();


            myOutput.close();

            myInputs.close();   



        } catch (FileNotFoundException e) {
   Toast.makeText(mymenu.this, "Import Unsuccesfull!", Toast.LENGTH_LONG).show();


            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {   Toast.makeText(mymenu.this, "Import Unsuccesfull!", 
   Toast.LENGTH_LONG).show();


            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Toast.makeText(mymenu.this, "Import Done Succesfully!", Toast.LENGTH_LONG).show();

编辑* 我的OpenHelper

    private static class OpenHelper extends SQLiteOpenHelper {

    OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }






    public OpenHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CARINFO_TABLE);

        db.execSQL(HISTORY_TABLE);

        db.execSQL(MPG_TABLE);

        db.execSQL(MODS_TABLE);

        db.execSQL(PICK_TABLE);

        //UPDATE//

        db.execSQL(VEHICLENAME_TABLE);
        db.execSQL(ODOMETER_TABLE);
        db.execSQL(COUNTER_TABLE);
        db.execSQL(SETTIME5_TABLE);
        db.execSQL(USORMETRIC_TABLE);



        }





    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        // TODO Auto-generated method stub
        if (arg2 > arg1) {
            db.execSQL(PICK_TABLE);

            db.execSQL("ALTER TABLE Employees ADD COLUMN _datetask INTEGER DEFAULT 0");
        }
    }
}

0 个答案:

没有答案