Android如何导入已有的外部数据库(在raw下自己导入db文件)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android如何导入已有的外部数据库(在raw下自己导入db文件)相关的知识,希望对你有一定的参考价值。
参考技术A 操作方法是用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.ImportDatabase; 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; 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 =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; ? 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.ImportDatabase; importjava.util.ArrayList; importandroid.app.Activity; importandroid.database.Cursor; importandroid.database.sqlite.SQLiteDatabase; importandroid.os.Bundle; publicclassTaxiActivityextendsActivity privateSQLiteDatabase database; ArrayList<CityClass> 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<CityClass> 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<CityClass> taxicity =newArrayList<CityClass>(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如何导入已有的外部数据库(在raw下自己导入db文件)的主要内容,如果未能解决你的问题,请参考以下文章
android导入外部db文件,出现Unparsed aapt error(s)! Check the console for output