注:使用getReadableDatabase和getReadableDatabase方法获取的SQLiteDatabase都可对数据库进行读写操作。
区别在于,当磁盘满时getReadableDatabase打开数据库成功,因为数据库是当前可以读的;使用getWritableDatabase打开数据库失败,因为此时数据库是不可写的
getReadableDatabase:打开可以读的数据库,打开后,可对数据库进行读写操作
getWritableDatabase:打开可以写的数据库,打开后,可对数据库进行读写操作
MySQLiteOpenDataBase.java
package prictise.lxm.prictise;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
/**
* Created by Administrator on 2015/4/7.
*/
public class MySQLiteOpenDataBase extends SQLiteOpenHelper {
final String CREATE_TABLE_WORD = "create table words(_id integer primary key autoincrement,"
+ " words nvarchar(50), detail nvarchar(300))"; //创建单词表语句
Context context;
/**
*
* @param context
* @param name 数据库名
* @param version 数据库版本
*/
public MySQLiteOpenDataBase(Context context, String name, int version) {
super(context, name, null, version, null);
this.context = context;
}
/**
* 在创建数据库时进行建表操作
* @param db
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_WORD);
}
/**
* 更改数据库版本时回调
* @param db
* @param oldVersion
* @param newVersion
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Toast.makeText(context, "更改数据库版本:旧版本-" + oldVersion + ";新版本-" + newVersion, Toast.LENGTH_LONG).show();
}
}
MainActivity
package prictise.lxm.prictise;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* 实现两个功能
* 1.新增单词
* 2.根据条件查找单词
*/
public class MainActivity extends Activity {
final String FILE_NAME = "lxmTest.bin";
MySQLiteOpenDataBase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddWords = (Button)findViewById(R.id.btn_addWords);
Button btnQuery = (Button)findViewById(R.id.btn_query);
//使用相对路径,文件将保持在程序的数据文件夹的databases目录下
db = new MySQLiteOpenDataBase(this,"myWord.db3",1);
//新增单词事件
btnAddWords.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//得到新单词信息
EditText edTxtWords = (EditText)findViewById(R.id.edTxt_words);
EditText edTxtDetail = (EditText)findViewById(R.id.edTxt_detail);
String newWords = edTxtWords.getText().toString();
String newDetail = edTxtDetail.getText().toString();
//执行新增
insertWord(db,newWords,newDetail);
//清空
edTxtWords.setText("");
edTxtDetail.setText("");
}
});
//查询单词事件
btnQuery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//得到查询条件
EditText edTxtQuery = (EditText) findViewById(R.id.edTxt_query);
String queryWhe = edTxtQuery.getText().toString();
//查询
Cursor cursorResult = searchWords(db, queryWhe);
ArrayList<Map<String, Object>> arrayList = cursorToList(cursorResult);
//弹出新的Activity显示结果
Bundle bundle = new Bundle();
bundle.putSerializable(ShowResult.DATA_NAME, arrayList);
Intent intent = new Intent(MainActivity.this, ShowResult.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
/**
* 释放时,关闭连接
*/
protected void onDestroy() {
if(db != null){
db.close();
}
super.onDestroy();
}
/**
* 插入单词
* @param db 数据库
* @param newWords 新的单词
* @param newDetail 新单词的明细
*/
private void insertWord(MySQLiteOpenDataBase db,String newWords,String newDetail){
db.getWritableDatabase().execSQL("insert into words(words,detail) values(?,?)",
new String[]{newWords,newDetail});
}
/**
* 对单词进行后模糊查询
* @param db 数据库
* @param words 进行模糊查询的部分单词
*/
private Cursor searchWords(MySQLiteOpenDataBase db, String words){
return db.getReadableDatabase().rawQuery("select * from words where words like ?", new String[]{words + "%"});
}
/**
* cursor转列表
* @param cursor
* @return
*/
private ArrayList<Map<String,Object>> cursorToList(Cursor cursor){
ArrayList<Map<String,Object>> arrayList = new ArrayList<Map<String, Object>>();
while (cursor.moveToNext()){
//得到当前行数据信息
String words = cursor.getString(cursor.getColumnIndex("words"));
String detail = cursor.getString(cursor.getColumnIndex("detail"));
//
Map<String,Object> map = new HashMap<String, Object>();
map.put("words",words);
map.put("detail",detail);
arrayList.add(map);
}
return arrayList;
}
}