数据库拷贝


当我们在开发应用的时候,有的时候需要给将一些文件随这应用的启动拷贝到内存卡或者系统应用中

最常见的就是数据库的拷贝

思路 :1、读取到数据的输入流

      2、获取终点地址

             3、将输入流转换为输出流,写到应用中

[java]  view plain copy
  1. public class DBuitls {  
  2.     private static final String TAG = "DBuitls";  
  3.     public static final String DB_NAME = "city.db";  
  4.     public static final int DB_VERSION = 1;  
  5.   
  6.     private DBuitls() {  
  7.         super();  
  8.     }  
  9.   
  10.     private static boolean extractDatabase(Context context, String name) {  
  11.         boolean retVal = false;  
  12.         // 获得数据存储的位置 ----/data/data/com.metek.copy/databases/city.db  
  13.         File db = context.getDatabasePath(name);  
  14.         if (!db.exists()) {  
  15.             File directory = db.getParentFile();  
  16.             directory.mkdirs();  
  17.             if (directory.exists()) {  
  18.                 try {  
  19.                     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(db));  
  20.                     InputStream in = context.getResources().openRawResource(R.raw.city);  
  21.                     byte[] buf = new byte[1024 * 8];  
  22.                     int length = 0;  
  23.                     while (-1 != (length = in.read(buf))) {  
  24.                         bos.write(buf, 0, length);  
  25.                     }  
  26.                     in.close();  
  27.                     bos.close();  
  28.                     retVal = true;  
  29.                 } catch (FileNotFoundException ex) {  
  30.                     Log.e(TAG, "Can not access db file.", ex);  
  31.                 } catch (IOException ex) {  
  32.                     Log.e(TAG, "Access db error.", ex);  
  33.                 }  
  34.             } else {  
  35.                 Log.e(TAG, "Can not make db directory.");  
  36.             }  
  37.         } else {  
  38.             retVal = true;  
  39.         }  
  40.         if (!retVal) {  
  41.             Log.e(TAG, "extractDatabase error");  
  42.             if (db.exists()) {  
  43.                 db.delete();  
  44.             }  
  45.         }  
  46.         return retVal;  
  47.     }  
  48.   
  49.     private static class PresetDbHelper extends SQLiteOpenHelper {  
  50.         public PresetDbHelper(Context context, String name) {  
  51.             super(context, name, null100);  
  52.             DBuitls.extractDatabase(context, name);  
  53.         }  
  54.   
  55.         public void onCreate(SQLiteDatabase db) {  
  56.             Log.e(TAG, "This should never been called.");  
  57.         }  
  58.   
  59.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  60.             Log.e(TAG, "This should never been called.");  
  61.         }  
  62.     }  
  63.   
  64.     public static int getDBcount(Context context) {  
  65.         PresetDbHelper dbOpenHelper = new PresetDbHelper(context, DB_NAME);  
  66.         SQLiteDatabase database = dbOpenHelper.getWritableDatabase();  
  67.         String sql = "select count(*) from city";  
  68.         Cursor cursor = database.rawQuery(sql, null);  
  69.         int result = 0;  
  70.         if (cursor != null) {  
  71.             if (cursor.moveToNext()) {  
  72.                 result = Integer.parseInt(cursor.getString(0));  
  73.             }  
  74.             cursor.close();  
  75.         }  
  76.         database.close();  
  77.         return result;  
  78.     }  
  79.   
  80. }  
[java]  view plain copy
  1. <pre name="code" class="java">  
[java]  view plain copy
  1. public class MainActivity extends Activity {  
  2.     private static final String TAG = null;  
  3.   
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.         int count = DBuitls.getDBcount(this);  
  8.         TextView text = (TextView) findViewById(R.id.count);  
  9.         Log.i(TAG, "查询到数据库发长度是 :" + count);  
  10.         text.setText("查询到数据库的长度是:" + count);  
  11.     }  
  12.   
  13. }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值