android直接讀取資料庫檔案

huidaoli發表於2013-07-19

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); 
        // 開啟資料庫,database是在Main類中定義的一個SQLiteDatabase型別的變數 
        database = openDatabase(); 
        // 下面的程式碼裝載了相關元件,並設定了相應的事件 
        btnSelectWord = (Button) findViewById(R.id.btnSelectWord); 
        actvWord = (AutoCompleteTextView) findViewById(R.id.actvWord); 
        btnSelectWord.setOnClickListener(this); 
        actvWord.addTextChangedListener(this); 
    } 
    public void onClick(View view) 
    { 
        //  查詢單詞的SQL語句 
        String sql = "select chinese from t_words where english=?";   
        Cursor cursor = database.rawQuery(sql, new String[] 
        { actvWord.getText().toString() }); 
        String result = "未找到該單詞."; 
        //  如果查詢單詞,顯示其中文資訊 
        if (cursor.getCount() > 0) 
        { 
            //  必須使用moveToFirst方法將記錄指標移動到第1條記錄的位置 
            cursor.moveToFirst(); 
            result = cursor.getString(cursor.getColumnIndex("chinese")); 
            Log.i("tran", "success"+result); 
        } 
        //  顯示查詢結果對話方塊 
        new AlertDialog.Builder(this).setTitle("查詢結果").setMessage(result) 
                .setPositiveButton("關閉", null).show(); 

    } 

    private SQLiteDatabase openDatabase() { 
        try { 
            // 獲得dictionary.db檔案的絕對路徑 
            String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME; 
            File dir = new File(DATABASE_PATH); 
            // 如果/sdcard/dictionary目錄中存在,建立這個目錄 
            if (!dir.exists()) 
                dir.mkdir(); 
            // 如果在/sdcard/dictionary目錄中不存在 
            // dictionary.db檔案,則從res\raw目錄中複製這個檔案到 
            // SD卡的目錄(/sdcard/dictionary) 
            if (!(new File(databaseFilename)).exists()) { 
                // 獲得封裝dictionary.db檔案的InputStream物件 
                InputStream is = getResources().openRawResource( 
                        R.raw.dictionary); 
                FileOutputStream fos = new FileOutputStream(databaseFilename); 
                byte[] buffer = new byte[8192]; 
                int count = 0; 
                // 開始複製dictionary.db檔案 
                while ((count = is.read(buffer)) > 0) { 
                    fos.write(buffer, 0, count); 
                } 

                fos.close(); 
                is.close(); 
            } 
            // 開啟/sdcard/dictionary目錄中的dictionary.db檔案 
            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) { 
    } 

} 

相關文章