android直接读取项目中的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) {
- }
- }
android直接读取项目中的sqlite数据库的更多相关文章
- 在项目中使用SQLite数据库小结
------------------------------------------------------------------------推荐: - VS2012 使用 1.0.84 版的库 - ...
- 在 Android 应用程序中使用 SQLite 数据库以及怎么用
part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...
- android中与SQLite数据库相关的类
为什么要在应用程序中使用数据库?数据库最主要的用途就是作为数据的存储容器,另外,由于可以很方便的将应用程序中的数据结构(比如C语言中的结构体)转化成数据库的表,这样我们就可以通过操作数据库来替代写一堆 ...
- 在Android 开发中使用 SQLite 数据库笔记
SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PH ...
- Android虚拟机中的sqlite数据库文件
Android虚拟机中的sqlite数据库文件 ①
- Android+Jquery Mobile学习系列(5)-SQLite数据库
SQLite是轻量级的.嵌入式的.关系型数据库,目前已经在iPhone.Android等手机系统中使用,SQLite可移植性好,很容易使用,很小,高效而且可靠. 因为Android已经集成了SQLit ...
- 2014-08-01 ASP.NET中对SQLite数据库的操作——ADO.NET
今天是在吾索实习的第18天.我主要学习了如何在ASP.NET中对SQLite数据库的操作,其基本操作如下: 添加引用System.Data.SQLite.dll(PS:在网页里面任意找到适合的.NET ...
- 在Xamarin.iOS项目中使用预设数据库
在Xamarin.iOS项目中使用预设数据库 当开发者准备好一个预设数据库文件后,就可以将这个数据库文件添加到创建的项目中了.本节将分别在Xamarin.iOS和Xamarin.Android项目中使 ...
- Go语言中使用SQLite数据库
Go语言中使用SQLite数据库 1.驱动 Go支持sqlite的驱动也比较多,但是好多都是不支持database/sql接口的 https://github.com/mattn/go-sqlite3 ...
随机推荐
- C++ 竞赛常用头文件
C.传统 C++ #include <assert.h> 设定插入点 #include <ctype.h> 字符处理 #include <errno.h> 定义错误 ...
- 反射 Reflect Modifier 修饰符工具类
在查看反射相关的Class.Field .Constructor 等类时,看到他们都有这样一个方法:getModifiers():返回此类或接口以整数编码的 Java 语言修饰符.如需要知道返回的值所 ...
- 如何配置Ubuntu 16.04 GRUB 2引导加载程序
正如你所知,GRUB 2 是大多数 Linux 操作系统的默认引导加载程序.GRUB 是 GRand Unified Bootloader 的缩写,它是 Linux 启动时首先要加载的一个程序,此后它 ...
- nodejs pm2的简单应用
一.简介 pm2是一个带有负载均衡功能的应用进程管理器,类似有Supervisor,forever,详细参数见官网:http://pm2.keymetrics.io 二.安装 Linux Binari ...
- cognos report上钻下钻报表处理方法(1)
cognos report开发中追溯行为,也可以称为上钻下钻行为大致遇到了两种情况 第一种:根据当前报表样式在维度范围内上钻下钻. 第二种:给追溯行为指定报表,传递参数. 可能还有其他情况,这里就不 ...
- [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 ...
- 免费电子书:The Guide to Minimum Viable Products
本地下载 来自uxPin的免费电子书
- c的链接详解
多目标文件的链接 stack.c #include <stdio.h> #define STACKSIZE 1000 typedef struct stack { int data[STA ...
- 算法笔记_189:历届试题 横向打印二叉树(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 二叉树可以用于排序.其原理很简单:对于一个排序二叉树添加新节点时,先与根节点比较,若小则交给左子树继续处理,否则交给右子树. 当遇到空子树 ...
- Junit和Spring
@ContextConfiguration 用来指定加载的Spring配置文件的位置,会加载默认配置文件 例如下例会加载:classpath:/com/example/MyTest-context.x ...