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

数据库类:

importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importandroid.content.Context;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.os.Environment;
importandroid.util.Log;

publicclassDBManager {
    privatefinalintBUFFER_SIZE =400000;
    publicstaticfinalString DB_NAME ="countries.db";//保存的数据库文件名
    publicstaticfinalString PACKAGE_NAME ="com.android.ImportDatabase";
    publicstaticfinalString DB_PATH ="/data"
            + Environment.getDataDirectory().getAbsolutePath() +"/"
            + PACKAGE_NAME; //在手机里存放数据库的位置
 
    privateSQLiteDatabase database;
    privateContext context;
 
    DBManager(Context context) {
        this.context = context;
}
 
    Public void openDatabase() {
        this.database =this.openDatabase(DB_PATH +"/"+ DB_NAME);
    }
 
    privateSQLiteDatabase openDatabase(String dbfile) {
        try{
            if(!(newFile(dbfile).exists())) {  //判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
                InputStream is =this.context.getResources().openRawResource(
                        R.raw.countries);//欲导入的数据库
                FileOutputStream fos =newFileOutputStream(dbfile);
                byte[] buffer =new byte[is.available()];
                intcount =0;
                while((count = is.read(buffer)) >0) {
                    fos.write(buffer,0, count);
                }
                fos.close();
                is.close();
            }
 SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
                    null);
            returndb;
        }catch(FileNotFoundException e) {
            Log.e("Database","File not found");
            e.printStackTrace();
        }catch(IOException e) {
            Log.e("Database","IO exception");
            e.printStackTrace();
        }
        returnnull;
    }
Public void closeDatabase() {
        this.database.close();
}
在主窗体上进行调用

Public class RootView extends Activity {
 
    publicDBManager dbHelper;
   @Override
    Public void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        dbHelper =newDBManager(this);
        dbHelper.openDatabase();
        dbHelper.closeDatabase();
 
    }
此时在DDMS中可以查看到,外部数据库已经成功导入

在需要使用数据库的类里

Public class TaxiActivity extends Activity {
 
    privateS QLiteDatabase database;
        ArrayList CITY;
@Override
    publicvoidonCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH +"/"+ DBManager.DB_NAME,null);
 CITY = getCity();
 // do something with CITY
 
        database.close();
}
privateArrayList getCity() {
         
        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);
         
        if(cur !=null) {
            intNUM_CITY = cur.getCount();
            ArrayList taxicity =newArrayList(NUM_CITY);
            if(cur.moveToFirst()) {
                do{
                    String name = cur.getString(cur.getColumnIndex("name"));
                    intid = cur.getInt(cur.getColumnIndex("id_city"));
                    CityClass city =newCityClass("",0);
                    System.out.println(name); //额外添加一句,把select到的信息输出到Logcat
                    city.city_name = name;
                    city.city_id = id;
                    taxicity.add(city);
                }while(cur.moveToNext());
            }
            returntaxicity;
        }else{
            returnnull;
        }
    }
}





你可能感兴趣的:(Android)