When we use a lot of data in our software, we choose to store the data in a database and then manage the data through the query modification operations of the database. Most of the time we are only in the program to establish the use of the database, but also have we only use the database in the program, not in the program to establish them, because this database is often a large amount of data, we are built without multi-threaded and background services, it is easy to cause the front interface blocking stagnation, This often affects the user experience and results in poor use. Then we can directly build the database and input data, and then through the program to correctly import the database into our software folder. This reduces the time to read data and establish the database, which can greatly improve the speed of software response.
It is also an example of how to import an external database by combining the development of a weather forecast feature of a software previously done. First we passDDMSTake a look at the database storage path, generally the database storage path is:/data/packagname/databases/(Packagnamerefers to the package name of the project we are building, for example, here my package name isCom.liuproject.reminder. Some databases are directlyPackagnamebelow, some are in thedatabasesdirectory, you can open theDDMSCheck it out. )databasesThere are two databases at the bottom of the folder:
which City(after successful import) is the database we are going to import from externally. We're going to build it. CityThe database files are copied to the project we built.Assetsfolder, as shown in the following:
Note that the external data we provide is consistent with the database we have designed to be used in the software, and the property value names and data types of each record are consistent in time. Once the data is ready, we can start importing the external database. The specific implementation of the following source code:
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"; Saved database file name 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 of the database in the mobile phone private context context; Importdb (Context context) {This.context = context; } public void Copydatabase () {String Dbfile=db_path + "/" + db_name; try {//Perform database import InputStream is = This.context. Getresources (). Getassets (). Open ("City"); Database to import 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 output stream is.close ();//close input stream} catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); We can import the external database stored in the Assets folder into the file directory we need by using the class above. Then start building the database class that we want to use in the program, as follows: 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 (C Ontext, Name,factory, version); } @Override public void OnCreate (Sqlitedatabase db) { String createdb= "CREATE TABLE" +db_name+ "(Cityid varchar primary KEY, CityName varchar (), type int)";//The database records the Property name and property value type Db.execsql (createdb);//CREATE DATABASE} @Override public void Onupgrade (SQLi Tedatabase db, int oldversion, int newversion) {String droptablesql = "DROP TABLE IF EXISTS" + db_n AME + ""; Db.execsql (Droptablesql); Droptablesql = "DROP TABLE IF EXISTS" + db_name + ""; Db.execsql (Droptablesql); OnCreate (DB); } public void Execsql (String sql, object[] args) {//perform operation Sqlitedatabase db = THIS.GETW Ritabledatabase (); Db.execsql (sql, args); } public Cursor query (String sql, string[] args) {//Return operation result pointer sqlitedatabase db = This.getwritab Ledatabase (); cursor cursor = db.rawquery (sql, args); RetuRN cursor; }}
in the subsequent program we will be able to passNew Citydb (this, NAME, NULL, VERSION);to create a database manipulation helper, where theNAMEto match the name of the external database that we imported between us, in my examplename=" City",VERSIONas the version number of the database, try to maintain consistency with the previous build. Use in the appropriate location of the programImportdbClass ofcopydatabase ()method to import the database. If the database is relatively large, it can be manipulated in the background thread.
Android Import External database