最近项目中要实现android读取sqlite数据库文件,在这里先做一个英汉字典的例子。主要是输入英语到数据库中查询相应的汉语意思,将其答案输出。数据库采用sqlite3.

如图:

实现过程完全是按照参考文章中所述。其中要说明的是,程序在第一次启动的时候,会把数据库安装到内存卡上面,从而可以读却数据库。

相关的代码:

  1. package com.easymorse;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.InputStream;
  5. import android.app.Activity;
  6. import android.app.AlertDialog;
  7. import android.database.Cursor;
  8. import android.database.sqlite.SQLiteDatabase;
  9. import android.os.Bundle;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.AutoCompleteTextView;
  16. import android.widget.Button;
  17. public class Dictionary extends Activity  implements OnClickListener, TextWatcher{
  18. private final String DATABASE_PATH = android.os.Environment
  19. .getExternalStorageDirectory().getAbsolutePath()
  20. + "/dictionary";
  21. private final String DATABASE_FILENAME = "dictionary.db3";
  22. SQLiteDatabase database;
  23. Button btnSelectWord;
  24. AutoCompleteTextView actvWord;
  25. @Override
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.main);
  29. // 打开数据库,database是在Main类中定义的一个SQLiteDatabase类型的变量
  30. database = openDatabase();
  31. // 下面的代码装载了相关组件,并设置了相应的事件
  32. btnSelectWord = (Button) findViewById(R.id.btnSelectWord);
  33. actvWord = (AutoCompleteTextView) findViewById(R.id.actvWord);
  34. btnSelectWord.setOnClickListener(this);
  35. actvWord.addTextChangedListener(this);
  36. }
  37. public void onClick(View view)
  38. {
  39. //  查找单词的SQL语句
  40. String sql = "select chinese from t_words where english=?";
  41. Cursor cursor = database.rawQuery(sql, new String[]
  42. { actvWord.getText().toString() });
  43. String result = "未找到该单词.";
  44. //  如果查找单词,显示其中文信息
  45. if (cursor.getCount() > 0)
  46. {
  47. //  必须使用moveToFirst方法将记录指针移动到第1条记录的位置
  48. cursor.moveToFirst();
  49. result = cursor.getString(cursor.getColumnIndex("chinese"));
  50. Log.i("tran", "success"+result);
  51. }
  52. //  显示查询结果对话框
  53. new AlertDialog.Builder(this).setTitle("查询结果").setMessage(result)
  54. .setPositiveButton("关闭", null).show();
  55. }
  56. private SQLiteDatabase openDatabase() {
  57. try {
  58. // 获得dictionary.db文件的绝对路径
  59. String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
  60. File dir = new File(DATABASE_PATH);
  61. // 如果/sdcard/dictionary目录中存在,创建这个目录
  62. if (!dir.exists())
  63. dir.mkdir();
  64. // 如果在/sdcard/dictionary目录中不存在
  65. // dictionary.db文件,则从res\raw目录中复制这个文件到
  66. // SD卡的目录(/sdcard/dictionary)
  67. if (!(new File(databaseFilename)).exists()) {
  68. // 获得封装dictionary.db文件的InputStream对象
  69. InputStream is = getResources().openRawResource(
  70. R.raw.dictionary);
  71. FileOutputStream fos = new FileOutputStream(databaseFilename);
  72. byte[] buffer = new byte[8192];
  73. int count = 0;
  74. // 开始复制dictionary.db文件
  75. while ((count = is.read(buffer)) > 0) {
  76. fos.write(buffer, 0, count);
  77. }
  78. fos.close();
  79. is.close();
  80. }
  81. // 打开/sdcard/dictionary目录中的dictionary.db文件
  82. SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
  83. databaseFilename, null);
  84. return database;
  85. } catch (Exception e) {
  86. }
  87. return null;
  88. }
  89. @Override
  90. public void afterTextChanged(Editable s) {
  91. }
  92. @Override
  93. public void beforeTextChanged(CharSequence s, int start, int count,
  94. int after) {
  95. }
  96. @Override
  97. public void onTextChanged(CharSequence s, int start, int before, int count) {
  98. }
  99. }

android直接读取项目中的sqlite数据库的更多相关文章

  1. 在项目中使用SQLite数据库小结

    ------------------------------------------------------------------------推荐: - VS2012 使用 1.0.84 版的库 - ...

  2. 在 Android 应用程序中使用 SQLite 数据库以及怎么用

    part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...

  3. android中与SQLite数据库相关的类

    为什么要在应用程序中使用数据库?数据库最主要的用途就是作为数据的存储容器,另外,由于可以很方便的将应用程序中的数据结构(比如C语言中的结构体)转化成数据库的表,这样我们就可以通过操作数据库来替代写一堆 ...

  4. 在Android 开发中使用 SQLite 数据库笔记

    SQLite 介绍   SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PH ...

  5. Android虚拟机中的sqlite数据库文件

    Android虚拟机中的sqlite数据库文件 ①

  6. Android+Jquery Mobile学习系列(5)-SQLite数据库

    SQLite是轻量级的.嵌入式的.关系型数据库,目前已经在iPhone.Android等手机系统中使用,SQLite可移植性好,很容易使用,很小,高效而且可靠. 因为Android已经集成了SQLit ...

  7. 2014-08-01 ASP.NET中对SQLite数据库的操作——ADO.NET

    今天是在吾索实习的第18天.我主要学习了如何在ASP.NET中对SQLite数据库的操作,其基本操作如下: 添加引用System.Data.SQLite.dll(PS:在网页里面任意找到适合的.NET ...

  8. 在Xamarin.iOS项目中使用预设数据库

    在Xamarin.iOS项目中使用预设数据库 当开发者准备好一个预设数据库文件后,就可以将这个数据库文件添加到创建的项目中了.本节将分别在Xamarin.iOS和Xamarin.Android项目中使 ...

  9. Go语言中使用SQLite数据库

    Go语言中使用SQLite数据库 1.驱动 Go支持sqlite的驱动也比较多,但是好多都是不支持database/sql接口的 https://github.com/mattn/go-sqlite3 ...

随机推荐

  1. C++ 竞赛常用头文件

    C.传统 C++ #include <assert.h> 设定插入点 #include <ctype.h> 字符处理 #include <errno.h> 定义错误 ...

  2. 反射 Reflect Modifier 修饰符工具类

    在查看反射相关的Class.Field .Constructor 等类时,看到他们都有这样一个方法:getModifiers():返回此类或接口以整数编码的 Java 语言修饰符.如需要知道返回的值所 ...

  3. 如何配置Ubuntu 16.04 GRUB 2引导加载程序

    正如你所知,GRUB 2 是大多数 Linux 操作系统的默认引导加载程序.GRUB 是 GRand Unified Bootloader 的缩写,它是 Linux 启动时首先要加载的一个程序,此后它 ...

  4. nodejs pm2的简单应用

    一.简介 pm2是一个带有负载均衡功能的应用进程管理器,类似有Supervisor,forever,详细参数见官网:http://pm2.keymetrics.io 二.安装 Linux Binari ...

  5. cognos report上钻下钻报表处理方法(1)

    cognos  report开发中追溯行为,也可以称为上钻下钻行为大致遇到了两种情况 第一种:根据当前报表样式在维度范围内上钻下钻. 第二种:给追溯行为指定报表,传递参数. 可能还有其他情况,这里就不 ...

  6. [Algorithm] How to use Max Heap to maintain K smallest items

    Let's say we are given an array: [,,,,,,] We want to get K = 3 smallest items from the array and usi ...

  7. 免费电子书:The Guide to Minimum Viable Products

    本地下载 来自uxPin的免费电子书

  8. c的链接详解

    多目标文件的链接 stack.c #include <stdio.h> #define STACKSIZE 1000 typedef struct stack { int data[STA ...

  9. 算法笔记_189:历届试题 横向打印二叉树(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 二叉树可以用于排序.其原理很简单:对于一个排序二叉树添加新节点时,先与根节点比较,若小则交给左子树继续处理,否则交给右子树. 当遇到空子树 ...

  10. Junit和Spring

    @ContextConfiguration 用来指定加载的Spring配置文件的位置,会加载默认配置文件 例如下例会加载:classpath:/com/example/MyTest-context.x ...