学習辞書
学習辞書をSQLiteで実装している
そもそも学習辞書をSQLiteで実装すべきなのかよくわからないし、仕様が色々面倒なのだがとりあえず...(2011/12/10)
参考にした情報
Android SQLiteマニュアル
code:SQLDict.java
//
// 学習辞書をSQLiteで実装する
//
package com.pitecan.gyaim;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.content.Context;
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "learndict", null, 1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public void onCreate(SQLiteDatabase db) {
// DBが存在する場合は呼ばれないらしい
db.execSQL(
"create table history("+
" word text not null,"+
" pat text not null,"+
" patind text not null,"+
" date text not null"+
");"
);
}
}
public class SQLDict
{
static SQLiteDatabase db;
public SQLDict(Context context){
DBHelper helper = new DBHelper(context);
db = helper.getWritableDatabase();
}
public static void add(String word, String pat){ // エントリ追加
// 最初に全部消す
db.delete("history", "word = '"+word+"' AND pat = '"+pat+"'", null);
int patind = LocalDict.patInd(pat);
// SQLite3の日付処理
db.execSQL("insert into history(word,pat,patind,date) values ('"+word+"', '"+pat+"', "+patind+", datetime('now', 'localtime'));");
}
public static void limit(int max){ // max個までにDBを制限する
Cursor cursor;
String word, pat;
cursor = db.query("history", new String[] { "word", "pat", "patind", "date" },
null, null, null, null, "date desc");
int count = cursor.getCount();
for(;max < count;max++){
cursor.moveToPosition(max);
word = cursor.getString(0);
pat = cursor.getString(1);
//Message.message("SQLite","delete -> " + word);
db.delete("history", "word = '"+word+"' AND pat = '"+pat+"'", null);
}
cursor.close();
}
public static String[][] search(String pat, boolean exactMode){ // 新しいものから検索
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> wordpats = new ArrayList<String>();
Pattern pattern = (exactMode ? Pattern.compile("^"+pat) : Pattern.compile("^"+pat+".*"));
//Message.message("Gyaim","pattern="+pattern);
Cursor cursor = db.query("history", new String[] { "word", "pat", "date" },
"patind = " + LocalDict.patInd(pat), null, null, null, "date desc");
boolean isEof = cursor.moveToFirst();
while (isEof) {
String word = cursor.getString(0);
String wordpat = cursor.getString(1);
//Message.message("Gyaim",String.format("word:%s wordpat:%s\r\n", word, wordpat));
if(pattern.matcher(wordpat).matches()){
//Message.message("Gyaim/SQLite - match",String.format("word:%s pat:%s\r\n", word, wordpat));
words.add(word);
wordpats.add(wordpat);
}
isEof = cursor.moveToNext();
}
cursor.close();
//Message.message("Gyaim","length = "+words.size());
for(int i=0;i<words.size();i++){
}
return res;
}
}