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) {
}
}
android直接讀取資料庫檔案
相關文章
- 使用openpyxl庫讀取Excel檔案資料Excel
- Java讀取properties檔案連線資料庫Java資料庫
- UnixWare 7.1.3直接讀取*.iso檔案(轉)
- 使用yaml檔案讀取資料YAML
- EasyExcel庫來讀取指定Excel檔案中的資料Excel
- HDFS讀檔案過程分析:讀取檔案的Block資料BloC
- vb向資料庫中讀取單個圖片檔案資料庫
- Hibernate 讀取檔案到資料庫的一個bug資料庫
- 快取依賴(檔案、資料庫)快取資料庫
- 獲取資料庫bak檔案資訊資料庫
- perl小指令碼——ftp上傳檔案、讀取資料庫表中資料指令碼FTP資料庫
- Android讀取配置檔案的方法Android
- Android儲存讀取txt檔案Android
- 關於讀取資料庫配置資原始檔問題資料庫
- golang讀取檔案的json資料流,並解析到struct,儲存到資料庫GolangJSONStruct資料庫
- 利用反射讀取資料庫資料反射資料庫
- sqlserver讀取oracle資料庫資料SQLServerOracle資料庫
- 通過讀取properties檔案動態生成對資料庫的連線資料庫
- Pandas之EXCEL資料讀取/儲存/檔案分割/檔案合併Excel
- 【萬里征程——Windows App開發】檔案&資料——讀取檔案/資料夾名WindowsAPP
- 建立資料庫檔案-日誌檔案-次要資料庫檔案資料庫
- C#讀取資料夾特定檔案的方法C#
- gin框架,讀取檔案的json資料流,並解析到struct,儲存到資料庫框架JSONStruct資料庫
- Django 直接使用資料庫連線和遊標讀寫資料庫Django資料庫
- eazyexcel 讀取excel資料插入資料庫Excel資料庫
- jquery簡單ajax示例_讀取json檔案資料jQueryJSON
- Java 讀取檔案Java
- tiff檔案讀取
- 直接複製資料檔案實現linux平臺資料庫複製到windows平臺資料庫Linux資料庫Windows
- 把影像檔案上傳到資料庫,並從資料庫讀出 (轉)資料庫
- Android--手持PDA讀取SD卡中檔案AndroidSD卡
- python讀取兩個excel資料檔案輸出整理好以後的excel資料檔案PythonExcel
- 資料庫檔案和檔案組資料庫
- c/c++、matlab讀取資料夾下的檔案C++Matlab
- php讀取excel檔案資料的匯入和匯出PHPExcel
- openfiledialog 使用 讀取txt檔案 StreamReader 檢驗資料
- 讀取每一行檔案資料的方法bufferedreader
- python讀取檔案——python讀取和儲存mat檔案Python