android读取mysql数据库文件_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;@Overridepublic 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;}@Overridepublic void afterTextChanged(Editable s) {}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值