在Sqlite中启用Soundex

时间:2019-05-25 12:31:14

标签: android sqlite android-sqlite soundex

我想将soundex与sqlite一起使用。目标是将同音字编码为相同的表示形式,以便尽管拼写略有不同也可以匹配。

我正在使用以下代码-

class SearchDBManager private constructor(context: Context) : BaseSQLiteOpenHelper(context, SQLITE_FILE_NAME, null, DATABASE_VERSION) {

    override fun onCreate(db: SQLiteDatabase?) {
        db?.execSQL(CREATE_SEARCH_TABLE)
    }

    override fun onUpgrade(db: SQLiteDatabase?, p1: Int, p2: Int) {
    }

    fun insertSearchList(station: HashMap<String, String>) {
        var db: SQLiteDatabase? = null
        try {
            db = writableDatabase
        } catch (e: IllegalStateException) {
            CrashLogger.logException(e)
        } catch (e: SQLException) {
            CrashLogger.logException(e)
        }
        if (db == null) {
            return
        }
        try {


            for ((key, value) in station) {
                val values = ContentValues()
                values.put(COLUMN_CODE, key)
                values.put(COLUMN_NAME, value)
                values.put(COLUMN_SOUNDEX, "soundex("+value+")")

                LogcatLogger.d(TAG, key + " " + value + " " + System.currentTimeMillis())
                insert(TABLE_SEARCH, null, values)

            }



        } catch (e: SQLException) {
            CrashLogger.logException(e)
        }

    }


    companion object {
        val DATABASE_VERSION = 1
        val SQLITE_FILE_NAME = "Search.sqlite"
        private val TAG = "SearchDBManager"
        private val TABLE_SEARCH = "Search"
        private val COLUMN_CODE = "Code"
        private val COLUMN_NAME = "Name"
        private val COLUMN_SOUNDEX = "SSoundex"

        private var sInstance: SearchDBManager? = null
        private val COMMA_SEP = ","

        private val CREATE_SEARCH_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_SEARCH + " (" +
                COLUMN_CODE + " TEXT NOT NULL UNIQUE " + COMMA_SEP +
                COLUMN_NAME + " TEXT " + COMMA_SEP +
                COLUMN_SOUNDEX + " TEXT " + ")"

        val instance: SearchDBManager?
            @Synchronized get() {
                if (sInstance == null) {
                    sInstance = SearchDBManager(XYApplication.getInstance())
                }
                return sInstance
            }
    }


}

以上代码在表中以soundex(<value>)的形式插入值,而我想用它对值进行编码。 How to use Soundex with SQLITE in android 但我不明白如何使用SQLITE_SOUNDEX编译时选项。 如何在sqlite中启用soundex?

0 个答案:

没有答案