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
- 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 the database. The database is a variable of the SQLiteDatabase type defined in the Main class.
- Database = openDatabase ();
- // The following code loads related components and sets corresponding events
- BtnSelectWord = (Button) findViewById (R. id. btnSelectWord );
- ActvWord = (AutoCompleteTextView) findViewById (R. id. actvWord );
- BtnSelectWord. setOnClickListener (this );
- ActvWord. addTextChangedListener (this );
- }
- Public void onClick (View view)
- {
- // SQL statement for searching words
- String SQL = "select chinese from t_words where english =? ";
- Cursor cursor = database. rawQuery (SQL, new String []
- {ActvWord. getText (). toString ()});
- String result = "this word is not found .";
- // If a word is searched, its Chinese information is displayed.
- If (cursor. getCount ()> 0)
- {
- // The moveToFirst method must be used to move the record pointer to the position of 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)
- . SetPositiveButton ("off", null). show ();
- }
- Private SQLiteDatabase openDatabase (){
- Try {
- // Obtain the absolute path of 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 the directory does not exist in the/sdcard/dictionary directory
- // Dictionary. db file, copy this file from the res \ raw directory
- // Directory of the SD card (/sdcard/dictionary)
- If (! (New File (databaseFilename). exists ()){
- // Obtain 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 the dictionary. db File
- 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 ){
- }
- }