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資料庫
- spark直接讀取本地檔案系統的檔案Spark
- 讀取資料夾檔案
- 使用yaml檔案讀取資料YAML
- EasyExcel庫來讀取指定Excel檔案中的資料Excel
- Android讀取配置檔案的方法Android
- golang讀取檔案的json資料流,並解析到struct,儲存到資料庫GolangJSONStruct資料庫
- gin框架,讀取檔案的json資料流,並解析到struct,儲存到資料庫框架JSONStruct資料庫
- Pandas之EXCEL資料讀取/儲存/檔案分割/檔案合併Excel
- sqlserver讀取oracle資料庫資料SQLServerOracle資料庫
- 利用反射讀取資料庫資料反射資料庫
- Asp.net Core 和類庫讀取配置檔案資訊ASP.NET
- Django 直接使用資料庫連線和遊標讀寫資料庫Django資料庫
- 阿里Android開發規範:檔案與資料庫阿里Android資料庫
- 任意檔案讀取
- Java 讀取檔案Java
- eazyexcel 讀取excel資料插入資料庫Excel資料庫
- c/c++、matlab讀取資料夾下的檔案C++Matlab
- python讀取兩個excel資料檔案輸出整理好以後的excel資料檔案PythonExcel
- python中xlrd庫如何實現檔案讀取?Python
- XamarinSQLite教程在Xamarin.Android專案中提取資料庫檔案SQLiteAndroid資料庫
- XamarinSQLite教程在Xamarin.Android專案中定位資料庫檔案SQLiteAndroid資料庫
- 爬取LeetCode資料,生成README檔案,美化GitHub倉庫LeetCodeGithub
- ASP.NET Core 中的檔案提供程式 遍歷資料夾讀取檔案資訊ASP.NET
- python中按照資料夾中檔案的排列順序讀取檔案內容Python
- 說說在 Python 中,如何讀取檔案中的資料Python
- php讀取excel檔案資料的匯入和匯出PHPExcel
- [python] 資料夾所有檔案讀取,正則化,json使用PythonJSON
- ArcGIS Pro SDK 002 對資料檔案的讀取和操作
- 【Python標準庫:fileinput】優雅的讀取檔案Python
- JEEVMS倉庫管理系統任意檔案讀取漏洞
- go配置檔案讀取Go
- python讀取大檔案Python
- springboot讀取配置檔案Spring Boot
- 用友任意檔案讀取
- viper 讀取配置檔案
- matlab讀取npy檔案Matlab
- python小白檔案讀取Python