The android database operations we usually see are generally to create an empty database at the beginning of the program, and then perform related operations. What if we need to use an existing database?
We all know that databases in the android system should be stored in/data/com. *. * (package name)/directory, so we need to import the existing database to that directory. The operation is to use FileInputStream to read the original database, and then use FileOutputStream to write the read content to that directory.
Operation Method: 1. Include the original database in the res/raw directory of the project source code and create a DBManager class. The Code is as follows:
View plain
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"; // saved database file name
Public static final String PACKAGE_NAME = "com. android. ImportDatabase ";
Public static final String DB_PATH = "/data"
+ Environment. getDataDirectory (). getAbsolutePath () + "/"
+ PACKAGE_NAME; // location where the database is stored on the mobile phone
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 () {// checks whether the database File exists. If it does not exist, import the database. Otherwise, open the database directly.
InputStream is = this. context. getResources (). openRawResource (
R. raw. countries); // database to be imported
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;
}
Then, in the first Activity of the program, an example of a DBManager object is provided, and then the openDatabase method can be executed to complete the import. You can write some operations to the database in the DBManager class, then it is called through the DBManager class object. You can also open the database and perform operations through a SQliteDatabase class object after the import is completed.
My approach is to import the database in the first Activity of the program:
View plain
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 ();
}
}
At this time, we can see in DDMS that the external database has been successfully imported
In the class where you need to use the database:
Package com. android. ImportDatabase;
Import java. util. ArrayList;
Import android. app. Activity;
Import android. database. Cursor;
Import android. database. sqlite. SQLiteDatabase;
Import android. OS. Bundle;
Public class TaxiActivity extends Activity {
Private SQLiteDatabase database;
ArrayList <CityClass> CITY;
@ Override
Public void onCreate (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 ();
}
Private ArrayList <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 ){
Int NUM_CITY = cur. getCount ();
ArrayList <CityClass> taxicity = new ArrayList <CityClass> (NUM_CITY );
If (cur. moveToFirst ()){
Do {
String name = cur. getString (cur. getColumnIndex ("name "));
Int id = cur. getInt (cur. getColumnIndex ("id_city "));
CityClass city = new CityClass ("", 0 );
System. out. println (name); // Add an additional statement to output the select statement to Logcat.
City. city_name = name;
City. city_id = id;
Taxicity. add (city );
} While (cur. moveToNext ());
}
Return taxicity;
} Else {
Return null;
}
}
}
View the output result:
From wenyizhenglove's column