Android directly reads the SQLite database in the project

Source: Internet
Author: User

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
  1. Package com.easymorse;
  2. Import Java.io.File;
  3. Import Java.io.FileOutputStream;
  4. Import Java.io.InputStream;
  5. Import android.app.Activity;
  6. Import Android.app.AlertDialog;
  7. Import Android.database.Cursor;
  8. Import Android.database.sqlite.SQLiteDatabase;
  9. Import Android.os.Bundle;
  10. Import android.text.Editable;
  11. Import Android.text.TextWatcher;
  12. Import Android.util.Log;
  13. Import Android.view.View;
  14. Import Android.view.View.OnClickListener;
  15. Import Android.widget.AutoCompleteTextView;
  16. Import Android.widget.Button;
  17. Public class Dictionary extends Activity implements Onclicklistener, textwatcher{
  18. Private final String Database_path = android.os.Environment
  19. . getExternalStorageDirectory (). GetAbsolutePath ()
  20. + "/dictionary";
  21. Private final String database_filename = "DICTIONARY.DB3";
  22. Sqlitedatabase database;
  23. Button Btnselectword;
  24. Autocompletetextview Actvword;
  25. @Override
  26. public void OnCreate (Bundle savedinstancestate) {
  27. super.oncreate (savedinstancestate);
  28. Setcontentview (R.layout.main);
  29. //Open databases, database is a variable of type sqlitedatabase defined in the main class
  30. Database = OpenDatabase ();
  31. The following code loads the relevant component and sets the appropriate event
  32. Btnselectword = (Button) Findviewbyid (R.id.btnselectword);
  33. Actvword = (Autocompletetextview) Findviewbyid (R.id.actvword);
  34. Btnselectword.setonclicklistener (this);
  35. Actvword.addtextchangedlistener (this);
  36. }
  37. public void OnClick (view view)
  38. {
  39. //Find words in SQL statements
  40. String sql = "Select Chinese from t_words where english=?";
  41. cursor cursor = database.rawquery (SQL, new string[]
  42. {Actvword.gettext (). toString ()});
  43. String result = "The word was not found.";
  44. //If you find words, display their Chinese information
  45. if (Cursor.getcount () > 0)
  46. {
  47. //must use the Movetofirst method to move the record pointer to the position of the 1th record
  48. Cursor.movetofirst ();
  49. result = Cursor.getstring (Cursor.getcolumnindex ("Chinese"));
  50. LOG.I ("Tran", "Success" +result);
  51. }
  52. //Display Query Results dialog box
  53. New Alertdialog.builder (this). Settitle ("Query Results"). Setmessage (Result)
  54. . Setpositivebutton ("off", null). Show ();
  55. }
  56. private Sqlitedatabase OpenDatabase () {
  57. try {
  58. //Get the absolute path to the dictionary.db file
  59. String databasefilename = Database_path + "/" + database_filename;
  60. File dir = new file (Database_path);
  61. //If the/sdcard/dictionary directory exists, create this directory
  62. if (!dir.exists ())
  63. Dir.mkdir ();
  64. //If it does not exist in the/sdcard/dictionary directory
  65. ////dictionary.db file, copy this file from the Res\raw directory to
  66. //SD card directory (/sdcard/dictionary)
  67. if (! ( new File (DatabaseFileName)). Exists ()) {
  68. //Get the InputStream object that encapsulates the Dictionary.db file
  69. InputStream is = Getresources (). Openrawresource (
  70. R.raw.dictionary);
  71. FileOutputStream fos = new FileOutputStream (DatabaseFileName);
  72. byte[] buffer = new byte[8192];
  73. int count = 0;
  74. //Start copying dictionary.db files
  75. While ((count = is.read (buffer)) > 0) {
  76. Fos.write (buffer, 0, Count);
  77. }
  78. Fos.close ();
  79. Is.close ();
  80. }
  81. //Open the Dictionary.db file in the/sdcard/dictionary directory
  82. Sqlitedatabase Database = Sqlitedatabase.openorcreatedatabase (
  83. DatabaseFileName, null);
  84. return database;
  85. } catch (Exception e) {
  86. }
  87. return null;
  88. }
  89. @Override
  90. public void aftertextchanged (Editable s) {
  91. }
  92. @Override
  93. public void beforetextchanged (charsequence s, int start, int count,
  94. int after) {   
  95. }
  96. @Override
  97. public void ontextchanged (charsequence s, int start, int before, int count) {
  98. }
  99. }

Android directly reads the SQLite database in the project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.