Android directly reads the sqlite database in the project, androidsqlite

Source: Internet
Author: User

Android directly reads the sqlite database in the project, androidsqlite

To read sqlite database files for android in a recent project, here is an example of an English-Chinese dictionary. It is mainly used to input English to the database to query the corresponding Chinese meaning and output the answer. The database uses sqlite3.

The implementation process is as described in the reference document. It should be noted that when the program is started for the first time, it will install the database on the memory card to read the database.

 

Related 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 the database. The database is a variable of the SQLiteDatabase type defined in the Main class.
  30. Database = openDatabase ();
  31. // The following code loads related components and sets corresponding events
  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. // SQL statement for searching words
  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 = "this word is not found .";
  44. // If a word is searched, its Chinese information is displayed.
  45. If (cursor. getCount ()> 0)
  46. {
  47. // The moveToFirst method must be used to move the record pointer to the position of 1st records
  48. Cursor. moveToFirst ();
  49. Result = cursor. getString (cursor. getColumnIndex ("chinese "));
  50. Log. I ("tran", "success" + result );
  51. }
  52. // Display the query result dialog box
  53. New AlertDialog. Builder (this). setTitle ("query result"). setMessage (result)
  54. . SetPositiveButton ("off", null). show ();
  55. }
  56. Private SQLiteDatabase openDatabase (){
  57. Try {
  58. // Obtain the absolute path of 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 the directory does not exist in the/sdcard/dictionary directory
  65. // Dictionary. db file, copy this file from the res \ raw directory
  66. // Directory of the SD card (/sdcard/dictionary)
  67. If (! (New File (databaseFilename). exists ()){
  68. // Obtain 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 the dictionary. db File
  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. }

Related Article

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.