1. 把原数据库包括在项目源码的 res/raw 目录下,然后建立一个DBManager类,代码如下:
package com.android.ImportDatabase
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.os.Environment
import android.util.Log
public class DBManager {
private final int BUFFER_SIZE = 400000
public static final String DB_NAME = "countries.db"//保存的数据库文件名
public static final String PACKAGE_NAME = "com.android.ImportDatabase"
public static final String DB_PATH = "/data"
+ Environment.getDataDirectory().getAbsolutePath() + "/"
+ PACKAGE_NAME //在手机里存放数据库的位置
private SQLiteDatabase database
private Context context
DBManager(Context context) {
this.context = context
}
public void openDatabase() {
this.database = this.openDatabase(DB_PATH + "/" + DB_NAME)
}
private SQLiteDatabase openDatabase(String dbfile) {
try {
if (!(new File(dbfile).exists())) {//判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
InputStream is = this.context.getResources().openRawResource(
R.raw.countries)//欲导入的数据库
FileOutputStream fos = new FileOutputStream(dbfile)
byte[] buffer = new byte[BUFFER_SIZE]
int count = 0
while ((count = is.read(buffer)) >0) {
fos.write(buffer, 0, count)
}
fos.close()
is.close()
}
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
null)
return db
} catch (FileNotFoundException e) {
Log.e("Database", "File not found")
e.printStackTrace()
} catch (IOException e) {
Log.e("Database", "IO exception")
e.printStackTrace()
}
return null
}
//do something else here<br>
public void closeDatabase() {
this.database.close()
}
}
然后在程序的首个Activity中示例化一个DBManager对象,然后对其执行openDatabase方法就可以完成导入了,可以把一些要对数据库进行的 *** 作写在DBManager类里,然后通过DBManager类的对象调用;也可以在完成导入之后通过一个SQliteDatabase类的对象打开数据库,并执行 *** 作。
我的做法是 在程序的首个Activity中导入数据库:
package com.android.ImportDatabase
import android.app.Activity
import android.content.Intent
import android.os.Bundle
public class RootView extends Activity {
public DBManager dbHelper
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
dbHelper = new DBManager(this)
dbHelper.openDatabase()
dbHelper.closeDatabase()
}
}
此时在DDMS中可以查看到,外部数据库已经成功导入
*** 作方法是用FileInputStream读取原数据库,再用 FileOutputStream把读取到的东西写入到那个目录。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 packagecom.android.ImportDatabaseimportjava.io.Fileimportjava.io.FileNotFoundExceptionimportjava.io.FileOutputStreamimportjava.io.IOExceptionimportjava.io.InputStreamimportandroid.content.Contextimportandroid.database.sqlite.SQLiteDatabaseimportandroid.os.Environmentimportandroid.util.LogpublicclassDBManager { privatefinalintBUFFER_SIZE =400000publicstaticfinalString DB_NAME ="countries.db"//保存的数据库文件名 publicstaticfinalString PACKAGE_NAME ="com.android.ImportDatabase"publicstaticfinalString DB_PATH ="/data" + Environment.getDataDirectory().getAbsolutePath() +"/" + PACKAGE_NAME//在手机里存放数据库的位置 privateSQLiteDatabase databaseprivateContext contextDBManager(Context context) { this.context = context} publicvoidopenDatabase() { 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 =newbyte[BUFFER_SIZE]intcount =0while((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} ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 packagecom.android.ImportDatabaseimportjava.util.ArrayListimportandroid.app.Activityimportandroid.database.Cursorimportandroid.database.sqlite.SQLiteDatabaseimportandroid.os.BundlepublicclassTaxiActivityextendsActivity { privateSQLiteDatabase databaseArrayList 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 = namecity.city_id = idtaxicity.add(city)}while(cur.moveToNext())} returntaxicity}else{ returnnull} } } 查看输出的结果:我基本同意这个理解。同时我认为数据库的IO密集型和CPU密集型是相对概念。一个查询对一个CUP很多很快的服务器而言,可能是IO密集型,对一个装备高速磁盘阵列的服务器而言可能变成CPU密集型。
同时坐等高手。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)