In recent projects to implement Android reading SQLite database files, here first to do an English-Chinese dictionary example. The main is to input English into the database query the corresponding Chinese meaning, the answer output. The database uses Sqlite3.
The implementation process is exactly as described in the reference article. It is to be noted that the first time the program starts, the database will be installed on the memory card, so as to read the database.
The relevant code:
Java code
- Package com.easymorse;
- Import Java.io.File;
- Import Java.io.FileOutputStream;
- Import Java.io.InputStream;
- Import android.app.Activity;
- Import Android.app.AlertDialog;
- Import Android.database.Cursor;
- Import Android.database.sqlite.SQLiteDatabase;
- Import Android.os.Bundle;
- Import android.text.Editable;
- Import Android.text.TextWatcher;
- Import Android.util.Log;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.AutoCompleteTextView;
- Import Android.widget.Button;
- Public class Dictionary extends Activity implements Onclicklistener, textwatcher{
- Private final String Database_path = android.os.Environment
- . getExternalStorageDirectory (). GetAbsolutePath ()
- + "/dictionary";
- Private final String database_filename = "DICTIONARY.DB3";
- Sqlitedatabase database;
- Button Btnselectword;
- Autocompletetextview Actvword;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- //Open databases, database is a variable of type sqlitedatabase defined in the main class
- Database = OpenDatabase ();
- The following code loads the relevant component and sets the appropriate event
- Btnselectword = (Button) Findviewbyid (R.id.btnselectword);
- Actvword = (Autocompletetextview) Findviewbyid (R.id.actvword);
- Btnselectword.setonclicklistener (this);
- Actvword.addtextchangedlistener (this);
- }
- public void OnClick (view view)
- {
- //Find words in SQL statements
- String sql = "Select Chinese from t_words where english=?";
- cursor cursor = database.rawquery (SQL, new string[]
- {Actvword.gettext (). toString ()});
- String result = "The word was not found.";
- //If you find words, display their Chinese information
- if (Cursor.getcount () > 0)
- {
- //must use the Movetofirst method to move the record pointer to the position of the 1th record
- Cursor.movetofirst ();
- result = Cursor.getstring (Cursor.getcolumnindex ("Chinese"));
- LOG.I ("Tran", "Success" +result);
- }
- //Display Query Results dialog box
- New Alertdialog.builder (this). Settitle ("Query Results"). Setmessage (Result)
- . Setpositivebutton ("off", null). Show ();
- }
- private Sqlitedatabase OpenDatabase () {
- try {
- //Get the absolute path to the dictionary.db file
- String databasefilename = Database_path + "/" + database_filename;
- File dir = new file (Database_path);
- //If the/sdcard/dictionary directory exists, create this directory
- if (!dir.exists ())
- Dir.mkdir ();
- //If it does not exist in the/sdcard/dictionary directory
- ////dictionary.db file, copy this file from the Res\raw directory to
- //SD card directory (/sdcard/dictionary)
- if (! ( new File (DatabaseFileName)). Exists ()) {
- //Get the InputStream object that encapsulates the Dictionary.db file
- InputStream is = Getresources (). Openrawresource (
- R.raw.dictionary);
- FileOutputStream fos = new FileOutputStream (DatabaseFileName);
- byte[] buffer = new byte[8192];
- int count = 0;
- //Start copying dictionary.db files
- While ((count = is.read (buffer)) > 0) {
- Fos.write (buffer, 0, Count);
- }
- Fos.close ();
- Is.close ();
- }
- //Open the Dictionary.db file in the/sdcard/dictionary directory
- Sqlitedatabase Database = Sqlitedatabase.openorcreatedatabase (
- DatabaseFileName, null);
- return database;
- } catch (Exception e) {
- }
- return null;
- }
- @Override
- public void aftertextchanged (Editable s) {
- }
- @Override
- public void beforetextchanged (charsequence s, int start, int count,
- int after) {
- }
- @Override
- public void ontextchanged (charsequence s, int start, int before, int count) {
- }
- }
Android directly reads the SQLite database in the project