Import external database to Android
When we need to use a large amount of data in our software, we will choose to store the data in a database, and then manage the data through the database query and modification operations. In most cases, we only create and use databases in the program, but there are also databases that we only use in the program, not in the program, because such databases often have a large amount of data, if we do not use multithreading or background services when we are creating the service, it will easily lead to congestion and stagnation on the front-end interface. This will often affect the user experience and lead to poor use results. In this case, can we directly create a database and input data, and then import the database into our software folder through a program. This reduces the time required to read data and create databases, and greatly increases the software response speed.
It also explains how to import an External Database Based on an instance developed with the weather forecast function of the previous software. First, let's look at the database storage path through DDMS. Generally, the database storage path is: /data/packagname/databases/(packagname indicates the package name of the project we created. For example, here, my package name is com. liuproject. reminder. Some databases are directly under packagname, and some are under the databases directory. You can open DDMS for a moment .) The databases folder contains two databases:
City (after successful import) is the database to be imported from outside. Copy the created City database file to the assets folder of the project we created, as shown in:
Note that the external data we provide should be consistent with the database we designed in the software, and the attribute value name and data type of each record should be consistent in a timely manner. After the data preparation is complete, we can begin to import the external database. The specific implementation source code is as follows:
Import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import android. content. context; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteDatabase. cursorFactory; import android. database. sqlite. SQLiteOpenHelper; import android. OS. environment; public class ImportDB {private Final int BUFFER_SIZE = 10000; public static final String DB_NAME = "City"; // The saved database file name is public static final String PACKAGE_NAME = "com. liuproject. reminder "; // project package name public static final String DB_PATH ="/data "+ Environment. getDataDirectory (). getAbsolutePath () + "/" + PACKAGE_NAME + "/databases"; // the location where the database is stored on the mobile phone. private Context context Context; ImportDB (context Context) {this. context = context;} public void c OpyDatabase () {String dbfile = DB_PATH + "/" + DB_NAME; try {// execute the Database Import InputStream is = this. context. getResources (). getAssets (). open ("City"); // The database 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 (); // close the output stream is. close (); // close the input stream} catch (FileNotFoundException E) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} using the class above, we can import the external database stored in the assets folder to the file directory we need. Then we start to build the database class we will use in the program, the following code: class CityDB extends SQLiteOpenHelper {// city Id database operation private final String DB_NAME = "CityID "; // database name public CityDB (Context context, String name, CursorFactory factory, int version) {super (context, name, factory, version );} @ Override public void onCreate (SQLiteDatabase db) {String createDB = "create table" + DB_NAME + "(cityid varchar (14) primary key, cityname varchar (20), type int) "; // name of the attribute recorded in the database and type of the attribute value db.exe cSQL (createDB); // create a database} @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {String dropTableSQL = "drop table if exists" + DB_NAME + ""; db.exe cSQL (dropTableSQL); dropTableSQL = "drop table if exists" + DB_NAME + ""; db.exe cSQL (dropTableSQL); onCreate (db);} public void execSQL (String SQL, Object [] args) {// execute SQLiteDatabase db = this. getWritableDatabase (); db.exe cSQL (SQL, args);} public Cursor query (String SQL, String [] args) {// return operation result pointer SQLiteDatabase db = this. getWritableDatabase (); Cursor cursor = db. rawQuery (SQL, args); return cursor ;}}In the subsequent program, we can use new CityDB (this, NAME, null, VERSION); Create a secondary object for database operations. The NAME must be the same as the NAME of the External Database imported between us. In my example, NAME = "City ", VERSION is the VERSION number of the database, and it should be consistent with the previous build. Use the copyDatabase () method of the ImportDB class to import the database where the program is suitable. If the database is large, you can open a thread in the background to perform operations.