Read database content under android

Source: Internet
Author: User

I will create another project on Android in the last few days. I need to read the SQLite database file into the program, and the result is more than N days... I am not strong enough... Later, I found an example on the Internet to read the. DB file into the SD card and then read it from the SD card.

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. vie W. 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 btnse Lectword; autocompletetextview actvword; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // open the database. database is a variable of the sqlitedatabase type defined in the main class. Database = opendatabase (); // the following code loads the relevant components, the corresponding event btnselectword = (button) findviewbyid (R. id. btnselectword); actvword = (autocompletetextview) findviewbyid (R. id. actvword); btnsele Ctword. setonclicklistener (this); actvword. addtextchangedlistener (this);} public void onclick (view) {// SQL statement for searching words string SQL = "select Chinese from t_words where English =? "; Cursor = database. rawquery (SQL, new string [] {actvword. gettext (). tostring ()}); string result = "this word is not found. "; // if you are searching for a word, display its Chinese information if (cursor. getcount ()> 0) {// you must use the movetofirst method to move the record pointer to the position of the first 1st records, cursor. movetofirst (); Result = cursor. getstring (cursor. getcolumnindex ("Chinese"); log. I ("Tran", "success" + result);} // display the query result dialog box new alertdialog. builder (this ). settitle ("query result "). setmessage (result ). s Etpositivebutton ("off", null ). show ();} private sqlitedatabase opendatabase () {try {// get dictionary. absolute path of the 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 the/sdcard/dictionary directory does not exist // dictionary. DB file, copy the file from the res \ raw directory to the // SD card directory (/sdcard/dictionary) if (! (New file (databasefilename )). exists () {// obtain the encapsulated dictionary. inputstream object of the DB file inputstream is = getresources (). openrawresource (R. raw. dictionary); fileoutputstream Fos = new fileoutputstream (databasefilename); byte [] buffer = new byte [8192]; int COUNT = 0; // start to copy dictionary. DB file while (COUNT = is. read (buffer)> 0) {FOS. write (buffer, 0, count);} FOS. close (); is. close ();} // open the dictionary in the/sdcard/dictionary directory. DB file sqlitedatabase database = sqlitedatabase. openorcreatedatabase (databasefilename, null); Return database;} catch (exception e) {} return NULL;} the initial experience of the JMS server activemq and @ 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) {}}

This is an example from the Internet. Then I figured it out.

But next again encountered a garbled problem, checked, is because my project is GBK, And the android database default for UTF-8, so it is enough to convert the binary data to the UTF-8.

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.