将数据库复制到scard
来源:互联网 发布:淘宝店铺开通花呗 编辑:程序博客网 时间:2024/05/16 01:54
视乎在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.javapublic 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
- 将数据库复制到scard
- 将sqlite数据库复制到用户目录
- 将一个数据库中的表复制到另一个数据库
- SqlServer将数据库中的表复制到另一个数据库
- SqlServer将数据库中的表复制到另一个数据库
- SQL Server将数据库中的表复制到另一个数据库
- SqlServer将数据库中的表复制到另一个数据库
- SqlServer将数据库中的表复制到另一个数据库
- 如何将一个数据库的表复制到另一个数据库
- SqlServer将数据库中的表复制到另一个数据库
- sqlite3怎样将A数据库表复制到另一个数据库
- 使用 Bulk Copy 将大量数据复制到数据库
- 使用 Bulk Copy 将大量数据复制到数据库
- 使用 Bulk Copy 将大量数据复制到数据库
- Android将项目中的数据库复制到SD
- 复制数据库到手机
- 将一个数据库的所有表复制到另一个数据库里
- 使用LINQ to SQL将数据从一个数据库复制到另一个数据库
- mysql——触发器
- 【Mybatis】深入浅出Mybatis(五)——入门程序(增删改)
- 最大连续数列的和
- iframe自适应高度和宽度 全css实现无javascript
- 【官方教程】使用Quick-Cocos2d-x搭建一个横版过关游戏(二)
- 将数据库复制到scard
- LeetCode
- dubbo协议下的单一长连接与多线程并发如何协同工作
- 集合类
- EventBus使用记录(一)
- 数组去重
- 5.Javascript语法-循环语句for循环
- 17
- 安卓manifest文件中配置activity时总是出错