如何导入已有的外部数据库

我们平时见到的 android 数据库操作一般都是在程序开始时创建一个空的数据库,然后再进行相关操作。如果我们需要使用一个已有数据的数据库怎么办呢?
  我们都知道 android系统 下数据库应该存放在 /data/data/com.*.*(package name)/ 目录下,所以我们需要做的是把已有的数据库传入那个目录下。操作方法是用FileInputStream读取原数据库,再用FileOutputStream把读取到的东西写入到那个目录。
  操作方法:1. 把原数据库包括在项目源码的 res/raw 目录下(其实随便建立个目录放进去就行了),然后建立一个DBManager类,代码如下:
  01 package com.android.ImportDatabase;
  02
  03 import java.io.File;
  04 import java.io.FileNotFoundException;
  05 import java.io.FileOutputStream;
  06 import java.io.IOException;
  07 import java.io.InputStream;
  08
  09 import android.content.Context;
  10 import android.database.sqlite.SQLiteDatabase;
  11 import android.os.Environment;
  12 import android.util.Log;
  13
  14 public class DBManager {
  15     private final int BUFFER_SIZE = 400000;
  16     public static final String DB_NAME = "countries.db"; //保存的数据库文件名
  17     public static final String PACKAGE_NAME = "com.android.ImportDatabase";
  18     public static final String DB_PATH = "/data"
  19             + Environment.getDataDirectory().getAbsolutePath() + "/"
  20             + PACKAGE_NAME;  //在手机里存放数据库的位置
  21
  22     private SQLiteDatabase database;
  23     private Context context;
  24
  25     DBManager(Context context) {
  26         this.context = context;
  27     }
  28
  29     public void openDatabase() {
  30         this.database = this.openDatabase(DB_PATH + "/" + DB_NAME);
  31     }
  32
  33     private SQLiteDatabase openDatabase(String dbfile) {
  34         try {
  35             if (!(new File(dbfile).exists())) {  //判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
  36                 InputStream is = this.context.getResources().openRawResource(
  37                         R.raw.countries); //欲导入的数据库
  38                 FileOutputStream fos = new FileOutputStream(dbfile);
  39                 byte[] buffer = new byte[BUFFER_SIZE];
  40                 int count = 0;
  41                 while ((count = is.read(buffer)) > 0) {
  42                     fos.write(buffer, 0, count);
  43                 }
  44                 fos.close();
  45                 is.close();
  46             }
  47             SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
  48                     null);
  49             return db;
  50         } catch (FileNotFoundException e) {
  51             Log.e("Database", "File not found");
  52             e.printStackTrace();
  53         } catch (IOException e) {
  54             Log.e("Database", "IO exception");
  55             e.printStackTrace();
  56         }
  57         return null;
  58     }
  1    //do something else here

  1     public void closeDatabase() {
  2         this.database.close();
  3     }
  4 }
  然后在程序的首个Activity中示例化一个DBManager对象,然后对其执行openDatabase方法就可以完成导入了,可以把一些要对数据库进行的操作写在DBManager类里,然后通过DBManager类的对象调用;也可以在完成导入之后通过一个SQliteDatabase类的对象打开数据库,并执行操作。
  我的做法是 在程序的首个Activity中导入数据库:


  1 package com.android.ImportDatabase;
  2
  3 import android.app.Activity;
  4 import android.content.Intent;
  5 import android.os.Bundle;
  6
  7 public class RootView extends Activity {
  8
  9     public DBManager dbHelper;
  view sourceprint?01     @Override
  02     public void onCreate(Bundle savedInstanceState) {
  03
  04         super.onCreate(savedInstanceState);
  05         setContentView(R.layout.main);
  06
  07         dbHelper = new DBManager(this);
  08         dbHelper.openDatabase();
  09         dbHelper.closeDatabase();
  10
  11     }
  12 }
  此时在DDMS中可以查看到,外部数据库已经成功导入
  在需要使用数据库的类里:
  01 package com.android.ImportDatabase;
  02
  03 import java.util.ArrayList;
  04 import android.app.Activity;
  05 import android.database.Cursor;
  06 import android.database.sqlite.SQLiteDatabase;
  07 import android.os.Bundle;
  08
  09 public class TaxiActivity extends Activity {
  10
  11     private SQLiteDatabase database;
  12         ArrayList CITY;
  13
  14     @Override
  15     public void onCreate(Bundle savedInstanceState) {
  16
  17         super.onCreate(savedInstanceState);
  18         setContentView(R.layout.main);
  19
  20         database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
  21
  22         CITY = getCity();
  23
  24         // do something with CITY
  25
  26         database.close();
  27     }
  28
  29     private ArrayList getCity() {
  30
  31         Cursor cur = database.rawQuery("SELECT city.id_city, city.name FROM taxi, city WHERE city.id_city = taxi.id_city GROUP BY city.id_city", null);
  32
  33         if (cur != null) {
  34             int NUM_CITY = cur.getCount();
  35             ArrayList taxicity = new ArrayList(NUM_CITY);
  36             if (cur.moveToFirst()) {
  37                 do {
  38                     String name = cur.getString(cur.getColumnIndex("name"));
  39                     int id = cur.getInt(cur.getColumnIndex("id_city"));
  40                     CityClass city = new CityClass("", 0);
  41                     System.out.println(name);  //额外添加一句,把select到的信息输出到Logcat
  42                     city.city_name = name;
  43                     city.city_id = id;
  44                     taxicity.add(city);
  45                 } while (cur.moveToNext());
  46             }
  47             return taxicity;
  48         } else {
  49             return null;
  50         }
  51     }
  52 }
  查看输出的结果:
  001 01-03 22:32:02.970: INFO/System.out(18231): AGEN
  002 01-03 22:32:02.970: INFO/System.out(18231): AJACCIO
  003 01-03 22:32:02.970: INFO/System.out(18231): ALBI
  004 01-03 22:32:02.980: INFO/System.out(18231): ALEN??ON
  005 01-03 22:32:02.980: INFO/System.out(18231): AMIENS
  006 01-03 22:32:02.980: INFO/System.out(18231): ANGERS
  007 01-03 22:32:02.990: INFO/System.out(18231): ANGOUL??ME
  008 01-03 22:32:02.990: INFO/System.out(18231): ANNECY
  009 01-03 22:32:03.000: INFO/System.out(18231): ARGENTEUIL
  010 01-03 22:32:03.000: INFO/System.out(18231): ARRAS
  011 01-03 22:32:03.000: INFO/System.out(18231): AUCH
  012 01-03 22:32:03.009: INFO/System.out(18231): AURILLAC
  013 01-03 22:32:03.009: INFO/System.out(18231): AUXERRE
  014 01-03 22:32:03.009: INFO/System.out(18231): AVIGNON
  015 01-03 22:32:03.019: INFO/System.out(18231): BASTIA
  016 01-03 22:32:03.019: INFO/System.out(18231): BAYONNE
  017 01-03 22:32:03.019: INFO/System.out(18231): BEAUVAIS
  018 01-03 22:32:03.019: INFO/System.out(18231): BELFORT
  019 01-03 22:32:03.029: INFO/System.out(18231): BESAN??ON
  020 01-03 22:32:03.029: INFO/System.out(18231): BLOIS
  021 01-03 22:32:03.029: INFO/System.out(18231): BOBIGNY
  022 01-03 22:32:03.029: INFO/System.out(18231): BORDEAUX
  023 01-03 22:32:03.029: INFO/System.out(18231): BOURG-EN-BRESSE
  024 01-03 22:32:03.039: INFO/System.out(18231): BOURGES
  025 01-03 22:32:03.039: INFO/System.out(18231): BREST
  026 01-03 22:32:03.039: INFO/System.out(18231): CAEN
  027 01-03 22:32:03.050: INFO/System.out(18231): CAHORS
  028 01-03 22:32:03.050: INFO/System.out(18231): CARCASSONNE
  029 01-03 22:32:03.050: INFO/System.out(18231): CASTRES
  030 01-03 22:32:03.050: INFO/System.out(18231): CH??LONS-EN-CHAMPAGNE
  031 01-03 22:32:03.059: INFO/System.out(18231): CHALON-SUR-SA??NE
  032 01-03 22:32:03.059: INFO/System.out(18231): CHAMB??RY
  033 01-03 22:32:03.059: INFO/System.out(18231): CHARLEVILLE-M??ZI??RES
  034 01-03 22:32:03.069: INFO/System.out(18231): CHARTRES
  035 01-03 22:32:03.080: INFO/System.out(18231): CH??TEAUROUX
  036 01-03 22:32:03.080: INFO/System.out(18231): CHAUMONT
  037 01-03 22:32:03.089: INFO/System.out(18231): CHERBOURG
  038 01-03 22:32:03.089: INFO/System.out(18231): COLMAR
  039 01-03 22:32:03.089: INFO/System.out(18231): CR??TEIL
  040 01-03 22:32:03.089: INFO/System.out(18231): DAX
  041 01-03 22:32:03.089: INFO/System.out(18231): DIGNE
  042 01-03 22:32:03.099: INFO/System.out(18231): DIJON
  043 01-03 22:32:03.099: INFO/System.out(18231): ??PINAL
  044 01-03 22:32:03.099: INFO/System.out(18231): ??VREUX
  045 01-03 22:32:03.099: INFO/System.out(18231): ??VRY
  046 01-03 22:32:03.110: INFO/System.out(18231): FOIX
  047 01-03 22:32:03.110: INFO/System.out(18231): GRENOBLE
  048 01-03 22:32:03.129: INFO/System.out(18231): GU??RET
  049 01-03 22:32:03.129: INFO/System.out(18231): LA ROCHELLE
  050 01-03 22:32:03.129: INFO/System.out(18231): LA ROCHE-SUR-YON
  051 01-03 22:32:03.129: INFO/System.out(18231): LANGON
  052 01-03 22:32:03.129: INFO/System.out(18231): LAON
  053 01-03 22:32:03.139: INFO/System.out(18231): LAVAL
  054 01-03 22:32:03.139: INFO/System.out(18231): LE HAVRE
  055 01-03 22:32:03.139: INFO/System.out(18231): LE MANS
  056 01-03 22:32:03.139: INFO/System.out(18231): LE PUY-EN-VELAY
  057 01-03 22:32:03.159: INFO/System.out(18231): LIBOURNE
  058 01-03 22:32:03.159: INFO/System.out(18231): LILLE
  059 01-03 22:32:03.169: INFO/System.out(18231): LIMOGES
  060 01-03 22:32:03.169: INFO/System.out(18231): LISIEUX
  061 01-03 22:32:03.179: INFO/System.out(18231): LONS-LE-SAUNIER
  062 01-03 22:32:03.179: INFO/System.out(18231): LORIENT
  063 01-03 22:32:03.179: INFO/System.out(18231): LYON
  064 01-03 22:32:03.179: INFO/System.out(18231): M??CON
  065 01-03 22:32:03.189: INFO/System.out(18231): MARSEILLE
  066 01-03 22:32:03.189: INFO/System.out(18231): MEAUX
  067 01-03 22:32:03.189: INFO/System.out(18231): MELUN
  068 01-03 22:32:03.189: INFO/System.out(18231): MENDE
  069 01-03 22:32:03.189: INFO/System.out(18231): METZ
  070 01-03 22:32:03.209: INFO/System.out(18231): MONTAUBAN
  071 01-03 22:32:03.209: INFO/System.out(18231): MONT-DE-MARSAN
  072 01-03 22:32:03.209: INFO/System.out(18231): MONTPELLIER
  073 01-03 22:32:03.209: INFO/System.out(18231): MOULINS
  074 01-03 22:32:03.230: INFO/System.out(18231): NANCY
  075 01-03 22:32:03.230: INFO/System.out(18231): NANTERRE
  076 01-03 22:32:03.239: INFO/System.out(18231): NANTES
  077 01-03 22:32:03.239: INFO/System.out(18231): NARBONNE
  078 01-03 22:32:03.249: INFO/System.out(18231): NEVERS
  079 01-03 22:32:03.249: INFO/System.out(18231): NICE
  080 01-03 22:32:03.249: INFO/System.out(18231): N??MES
  081 01-03 22:32:03.249: INFO/System.out(18231): NIORT
  082 01-03 22:32:03.259: INFO/System.out(18231): ORL??ANS
  083 01-03 22:32:03.259: INFO/System.out(18231): PARIS
  084 01-03 22:32:03.259: INFO/System.out(18231): PAU
  085 01-03 22:32:03.259: INFO/System.out(18231): P??RIGUEUX
  086 01-03 22:32:03.269: INFO/System.out(18231): PERPIGNAN
  087 01-03 22:32:03.269: INFO/System.out(18231): POITIERS
  088 01-03 22:32:03.269: INFO/System.out(18231): PONTOISE
  089 01-03 22:32:03.280: INFO/System.out(18231): PRIVAS
  090 01-03 22:32:03.280: INFO/System.out(18231): QUIMPER
  091 01-03 22:32:03.280: INFO/System.out(18231): REIMS
  092 01-03 22:32:03.289: INFO/System.out(18231): RENNES
  093 01-03 22:32:03.289: INFO/System.out(18231): RODEZ
  094 01-03 22:32:03.289: INFO/System.out(18231): ROUEN
  095 01-03 22:32:03.309: INFO/System.out(18231): SAINT GERMAIN EN LAYE
  096 01-03 22:32:03.309: INFO/System.out(18231): SAINT-BRIEUC
  097 01-03 22:32:03.319: INFO/System.out(18231): SAINT-??TIENNE
  098 01-03 22:32:03.329: INFO/System.out(18231): STRASBOURG
  099 01-03 22:32:03.329: INFO/System.out(18231): TARBES
  100 01-03 22:32:03.329: INFO/System.out(18231): TOULON
  101 01-03 22:32:03.339: INFO/System.out(18231): TOULOUSE
  102 01-03 22:32:03.339: INFO/System.out(18231): TOURS
  103 01-03 22:32:03.339: INFO/System.out(18231): TROYES
  104 01-03 22:32:03.339: INFO/System.out(18231): TULLE
  105 01-03 22:32:03.339: INFO/System.out(18231): VALENCE
  106 01-03 22:32:03.339: INFO/System.out(18231): VANNES
  107 01-03 22:32:03.349: INFO/System.out(18231): VERDUN
  108 01-03 22:32:03.349: INFO/System.out(18231): VERSAILLES
  109 01-03 22:32:03.369: INFO/System.out(18231): VESOUL
  110 01-03 22:32:03.379: INFO/System.out(18231): VIRE
  111 01-03 22:32:04.089: INFO/ActivityManager(68): Displayed com.android.ImportDatabase/.TaxiActivity: +1s548ms

你可能感兴趣的:(如何导入已有的外部数据库)