使用安卓读取sqlite数据库方法记录
最近要实现android读取sqlite数据库文件,在这里先做一个英汉字典的例子。主要是输入英语到数据库中查询相应的汉语意思,将其答案输出。数据库采用sqlite3.
如图:
![]()
实现过程完全是按照参考文章中所述。其中要说明的是,程序在第一次启动的时候,会把数据库安装到内存卡上面,从而可以读却数据库。
相关的代码:
- package com.easymorse;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- 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) {
- }
- }
使用安卓读取sqlite数据库方法记录的更多相关文章
- php读取sqlite数据库入门实例
php读取sqlite数据库的例子,php编程中操作sqlite入门实例.原文参考:http://www.jbxue.com/article/php/22383.html在使用SQLite前,要确保p ...
- 孤荷凌寒自学python第五十二天初次尝试使用python读取Firebase数据库中记录
孤荷凌寒自学python第五十二天初次尝试使用python读取Firebase数据库中记录 (完整学习过程屏幕记录视频地址在文末) 今天继续研究Firebase数据库,利用google免费提供的这个数 ...
- SQLite 数据库使用记录
SQLite 数据库使用记录 官网 https://www.sqlite.org/index.html 下载地址 https://www.sqlite.org/download.html 参考资料 S ...
- [CSharpTips]C#读取SQLite数据库中文乱码
C#读取SQLite数据库中文乱码 C#在读取C++写入数据的Sqlite数据库中的Text内容时,会出现乱码,因为C++默认编码格式为GB2312,而Sqlite编码格式为UTF-8,存入时不统一就 ...
- C#连接SQLite数据库方法
--结合Enterprise Library连接,操作SQLite 企业库是我们常用的框架之一,可以从http://entlib.codeplex.com/下载Enterprise Library 5 ...
- 安卓平台SQLite数据库基础操作总结
最近学了一些安卓开发,在这里分享一下SQLite数据库的使用相关部分,我使用的工具为Android Studio,后台语言为java: 首先,需要创建一个数据库辅助类DataBaseHelper,用于 ...
- unity读取Sqlite数据库
using UnityEngine; using System.Collections; using Mono.Data.Sqlite; using System.Data; public enum ...
- android直接读取项目中的sqlite数据库
最近项目中要实现android读取sqlite数据库文件,在这里先做一个英汉字典的例子.主要是输入英语到数据库中查询相应的汉语意思,将其答案输出.数据库采用sqlite3. 如图: 实现过程完全是按照 ...
- Android开发-之SQLite数据库
之前我们讲了如何将数据存储在文件中,那么除了这种方式呢,就是我们常见的大家都知道的将数据存储在数据库当中了. 将数据存储在数据库中的优势: 1)存储在数据库中的数据更加方便操作,比如增.删.改.查等 ...
随机推荐
- context.Response.End()的用法和本质
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Web_C ...
- linux下无线网卡的ioctl 接口
var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/ba ...
- [转] C#操作EXCEL,生成图表的全面应用
gailzhao 原文 关于C#操作EXCEL,生成图表的全面应用 近来我在开发一个运用C#生成EXCEL文档的程序,其中要根据数据生成相应的图表,该图表对颜色和格式都有严格的要求,在百度和谷歌中搜索 ...
- DevExpress控件使用小结 z
.TextEditor(barEditItem)取文本 string editValue = barEditItem1.EditValue.ToString(); //错误,返回null string ...
- IOS 屏幕截图 UIScrollview
//截图UIView:截全图 -(UIImage*)captureView:(UIView *)theView{ CGRect rect = theView.frame; if ([theView i ...
- 【QTP】自动化测试:
一.参数化: 1.随机数: 下面两个语句都可以: Window("Flight Reservation").Dialog("Flights Table").Wi ...
- [原创]Android应用 - YE启动器APP(YeLauncherApp)
支持解冻.冻结APP 支持在本APP列表隐藏指定APP 支持检测系统核心APP,防止误点冻结导致手机变砖 支持清理APP缓存 支持强制停止APP Apk文件仅仅47KB 自用.开源 类似APP:A ...
- 测试xss和xsf
xss注入攻击: 123123123 被动注入: 1231231231231231 主动注入: 对不起,你需要登录才能评论 用户名 密码
- 关于display显示 linux
export DISPLAY=ipaddressofyourmachineorpc:0.0 如果要在本来的机器上显示,使用 export DISPLAY=localhost:0
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...