将数据库复制到scard

视乎在android4.4以后没有root的手机不能查看data/data目录,所以有时候创建了数据库但是不能很清晰的查看里面的信息。为了更清晰的查看数据库信息可以选择模拟器,或者将数据库复制到sdcard然后导出到电脑使用sqlitespy查看

下面来个demo

.

1.DBHelper.java
/**
 * Created by tiankonglanlande on 2017/3/8.
 */

public class DBHelper extends SQLiteOpenHelper {
    private static final String TAG = "DBHelper";


    public static final String DB_NAME ="copy.db" ;
    public static final String TB_USER ="user_tb" ;
    private String TB_USER_USERNAME="username";
    private String TB_USER_AGE="age";

    public DBHelper(Context context) {
        super(context,DB_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.e(TAG, "onCreate: ");
        String sql="create table "+TB_USER+"( _id Integer primary key autoincrement," +
                TB_USER_USERNAME+" text," +
                TB_USER_AGE+" Integer" +
                ")";
        db.execSQL(sql);

        // 插入测试数据
        insertData(db);
    }

    private void insertData(SQLiteDatabase db) {
        for (int i=0;i<50;i++){
            ContentValues values=new ContentValues();
            values.put("username","用户"+i);
            values.put("age",10+i);
            long count=db.insert(TB_USER,null,values);
            if (count>0){
                Log.e(TAG, "insertData: 插入第"+i+"条数据");
            }
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql="drop table if exists "+TB_USER;
        db.execSQL(sql);
        onCreate(db);
    }
}

2.CopyDBBiz.java
/**
 * Created by tiankonglanlande on 2017/3/8.
 * 将本应用的数据库复制到sdcard
 */
public class CopyDBBiz {
    private final Context context;
    public static String DataBaseName= DBHelper.DB_NAME;//需要复制到sdcard的数据库名称


    public CopyDBBiz(Context context){
        this.context=context;
        File oldFile=context.getDatabasePath(DataBaseName);
        String oldPath=oldFile.getPath();
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            String newPath= Environment.getExternalStorageDirectory().getPath()+File.separator+DataBaseName;
            copy(oldPath,newPath);
        }else{
            try {
                throw new Exception("please make sure permision and SdCard exist !");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 复制操作
     * @param oldPath
     * @param newPath
     */
    private void copy(String oldPath, String newPath) {
        InputStream in=null;
        OutputStream os=null;
        try {
            //判断文件是否存在
            File oldfile=new File(oldPath);
            File newfile=new File(newPath);
            if (!newfile.exists()){
                newfile.createNewFile();
            }
            if (oldfile!=null){
                in=new FileInputStream(oldPath);
                byte buffer[]=new byte[1024];
                int read=-1;
                os=new FileOutputStream(newPath);
                while((read=in.read(buffer))!=-1){
                    os.write(buffer,0,read);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (in!=null) in.close();
                if (os!=null) os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3.MainActivity.java
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DBHelper helper=new DBHelper(this);
        helper.getWritableDatabase();

        CopyDBBiz copyDBBiz=new CopyDBBiz(this);
    }
}
MainActivity.java创建helper以及调用getWritableDatabase方法是为了促使数据库创建。因为SqliteOpenHelper的onCreate是在
getWritableDatabase,或getReadableDatabase的情况下去调用的
 
 
 
 
 
 
 
 
 
 


4.添加权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

5.导出到电脑使用sqliteSpy查看效果图:



 
 
 
 
 
 
 
 
 
 
 
 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值