Read the database files directly in Android

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, database is a variable of type SQLiteDatabase defined in the Main class. 

database = openDatabase(); 

// The following code loading the related components, and set the corresponding event 

btnSelectWord = (Button) findViewById(R.id.btnSelectWord); 

actvWord = (AutoCompleteTextView) findViewById(R.id.actvWord); 

btnSelectWord.setOnClickListener(this); 

actvWord.addTextChangedListener(this); 

} 

public void onClick(View view) 

{ 

//  The SQL statement to find words 

String sql = "select chinese from t_words where english=?";   

Cursor cursor = database.rawQuery(sql, new String[] 

{ actvWord.getText().toString() }); 

String result = "The word is not found."; 

//  If you find the word, to show its Chinese information 

if (cursor.getCount() > 0) 

{ 

//  You must use the moveToFirst method to record the pointer moves to the first recording position 

cursor.moveToFirst(); 

result = cursor.getString(cursor.getColumnIndex("chinese")); 

Log.i("tran", "success"+result); 

} 

//  Show query result dialog 

new AlertDialog.Builder(this).setTitle("Query results").setMessage(result) 

.setPositiveButton("Close", null).show(); 

} 

private SQLiteDatabase openDatabase() { 

try { 

// The absolute path to the dictionary.db file access 

String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME; 

File dir = new File(DATABASE_PATH); 

// If in the /sdcard/dictionary directory, create this directory 

if (!dir.exists()) 

dir.mkdir(); 

// If it does not exist in the /sdcard/dictionary directory 

// Dictionary.db files from the res\raw directory, copy this file to the 

// SD card catalog(/sdcard/dictionary) 

if (!(new File(databaseFilename)).exists()) { 

// Get the package dictionary.db file of the InputStream object 

InputStream is = getResources().openRawResource( 

R.raw.dictionary); 

FileOutputStream fos = new FileOutputStream(databaseFilename); 

byte[] buffer = new byte[8192]; 

int count = 0; 

// To copy 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) { 

} 

} 

Posted by Pauline at November 16, 2013 - 1:06 PM