视乎在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查看效果图: