这是否会code更换新文件或不存在的数据库文件? [英] Will this code replace the existing database file with new file or not?

查看:96
本文介绍了这是否会code更换新文件或不存在的数据库文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个梅索德,请告诉我,如果在文件中的SD卡已经存在那么它是否会取代或不复制拷贝我的数据库文件到SD车?

 公共布尔copyDbToSDCard(){
        布尔成功= FALSE;
        字符串SDCardPath = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        最终字符串DBPATH = SDCardPath +/ BD /;
        最后弦乐DBNAME =Mydb3.db;
        this.getReadableDatabase();
        文件目录=新的文件(DBPATH);
        如果(!directory.exists())
            directory.mkdir();
        关闭();        尝试{
            InputStream的mInput =新的FileInputStream(DB_PATH + DB_NAME);
            的OutputStream mOutput =新的FileOutputStream(DBPATH + DBNAME);
            字节[]缓冲区=新的字节[1024];
            INT长;
            而((长度= mInput.read(缓冲液))大于0){
                mOutput.write(缓冲液,0,长度);
            }
            mOutput.flush();
            mOutput.close();
            mInput.close();
            成功= TRUE;
        }赶上(例外五){
            Toast.makeText(myContext,
                    copyDbToSDCard错误:+ e.getMessage()
                    Toast.LENGTH_SHORT).show();
            e.fillInStackTrace();
        }
        返回成功;
    }


解决方案

您可以检查是数据库可用?

像这样

  CHECKDB()
{
尝试{
      SQLiteDatabase DBE = SQLiteDatabase.openDatabase(selectedFilePath,NULL,0);
        Log.d(opendb,存在);
        dbe.close();
         // DB退出然后删除
        档案文件=新的文件(selectedFilePath);
        布尔删除= file.delete(); < ---这将帮助您删除数据库    }
    赶上(例外五)
    {
         // DB不退出code复制数据库
    }
 }

I'm copying my db file to sd car using this methode please tell me if file at sdcard is already existing then whether it will replace or will not copy?

public boolean copyDbToSDCard() {
        boolean success = false;
        String SDCardPath = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        final String DBPATH = SDCardPath + "/BD/";
        final String DBNAME = "Mydb3.db";
        this.getReadableDatabase();
        File directory = new File(DBPATH);
        if (!directory.exists())
            directory.mkdir();
        close();

        try {
            InputStream mInput = new FileInputStream(DB_PATH + DB_NAME);
            OutputStream mOutput = new FileOutputStream(DBPATH + DBNAME);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = mInput.read(buffer)) > 0) {
                mOutput.write(buffer, 0, length);
            }
            mOutput.flush();
            mOutput.close();
            mInput.close();
            success = true;
        } catch (Exception e) {
            Toast.makeText(myContext,
                    "copyDbToSDCard Error : " + e.getMessage(),
                    Toast.LENGTH_SHORT).show();
            e.fillInStackTrace();
        }
        return success;
    }

解决方案

You can check is database available or not ?

like this

checkDB()
{
try{
      SQLiteDatabase   dbe = SQLiteDatabase.openDatabase("selectedFilePath", null,0);
        Log.d("opendb","EXIST");
        dbe.close();
         // DB exits then delete
        File file = new File(selectedFilePath);
        boolean deleted = file.delete(); <--- this will help you to delete DB

    }
    catch(Exception e)
    {
         // DB not exits code to copy database
    }
 }

这篇关于这是否会code更换新文件或不存在的数据库文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆