T9输入法在SQL Server2005 中的实现

2024-03-28 17:58

本文主要是介绍T9输入法在SQL Server2005 中的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Create   table  NumberToChar  -- --------用来保存数字和字母之间映射的表
(
    
number    int ,
    GetText 
varchar ( 10 )
)
-- 字典表
select   *   from  Dictionary
-- 联系人表
select   *   from  Contact
-- 创建键盘表---------------------------------------------------------------------------------
create   table  Keys
(
 alpha 
char ( 1 primary   key ,
 digit 
char ( 1 ) ,
)
insert   into  Keys(alpha, digit)  values ( ' a ' , ' 2 ' );
insert   into  Keys(alpha, digit)  values ( ' b ' , ' 2 ' );
insert   into  Keys(alpha, digit)  values ( ' c ' , ' 2 ' );
insert   into  Keys(alpha, digit)  values ( ' d ' , ' 3 ' );
insert   into  Keys(alpha, digit)  values ( ' e ' , ' 3 ' );
insert   into  Keys(alpha, digit)  values ( ' f ' , ' 3 ' );
insert   into  Keys(alpha, digit)  values ( ' g ' , ' 4 ' );
insert   into  Keys(alpha, digit)  values ( ' h ' , ' 4 ' );
insert   into  Keys(alpha, digit)  values ( ' i ' , ' 4 ' );
insert   into  Keys(alpha, digit)  values ( ' j ' , ' 5 ' );
insert   into  Keys(alpha, digit)  values ( ' k ' , ' 5 ' );
insert   into  Keys(alpha, digit)  values ( ' l ' , ' 5 ' );
insert   into  Keys(alpha, digit)  values ( ' m ' , ' 6 ' );
insert   into  Keys(alpha, digit)  values ( ' n ' , ' 6 ' );
insert   into  Keys(alpha, digit)  values ( ' o ' , ' 6 ' );
insert   into  Keys(alpha, digit)  values ( ' p ' , ' 7 ' );
insert   into  Keys(alpha, digit)  values ( ' q ' , ' 7 ' );
insert   into  Keys(alpha, digit)  values ( ' r ' , ' 7 ' );
insert   into  Keys(alpha, digit)  values ( ' s ' , ' 7 ' );
insert   into  Keys(alpha, digit)  values ( ' t ' , ' 8 ' );
insert   into  Keys(alpha, digit)  values ( ' u ' , ' 8 ' );
insert   into  Keys(alpha, digit)  values ( ' v ' , ' 8 ' );
insert   into  Keys(alpha, digit)  values ( ' w ' , ' 9 ' );
insert   into  Keys(alpha, digit)  values ( ' x ' , ' 9 ' );
insert   into  Keys(alpha, digit)  values ( ' y ' , ' 9 ' );
insert   into  Keys(alpha, digit)  values ( ' z ' , ' 9 ' );
select   *   from  Keys
-- 创建联系人T9码速查表------------------------------------------------------------------
create   table  ContactT9Info
(
 id 
int   not   null   REFERENCES  Contact(id),
 T9Code 
varchar ( 100 not   null ,
 
PRIMARY   KEY   CLUSTERED  (id, T9Code)
)
select   *   from  ContactT9Info
-- 函数:取得汉字的拼音---------------------------------------------------------------------
select  dbo.getPY( ' ' )
create   function  getPY( @word   varchar ( 5 ))
 
returns   varchar ( 10 )
as
begin
 
declare   @py   varchar ( 10 )
 
set   @py   =  ( select   top   1  py  from  dictionary
 
where  words  like   ' % '   +   @word   +   ' % ' )
 
return ( @py )
end
go

-- 函数:取得拼音的T9码---------------------------------------------------------------------
select  dbo.getT9CodeFromPY( ' huang ' )
create   function  getT9CodeFromPY( @pinyin   varchar ( 10 ))
 
returns   varchar ( 10 )
as
begin
 
declare   @i   int @length   int @alpha   char ( 1 ),  @code   varchar ( 10 )
 
set   @i   =   1
 
set   @length   =   len ( @pinyin )
 
set   @code   =   ''
 
 
while  ( @i   <=   @length )
 
begin
  
-- 对拼音的每个字母进行处理
   set   @alpha   =   substring ( @pinyin @i 1 )
  
set   @code   =   @code   +  dbo.getKeyFromAlpha( @alpha )
  
set   @i   =   @i   +   1
 
end
 
return   @code
end
go

-- 函数:取得字母的键码---------------------------------------------------------------------
select  dbo.getKeyFromAlpha( ' h ' )
create   function  getKeyFromAlpha( @alpha   char ( 1 ))
 
returns   char ( 1 )
as
begin
 
return
 (
  
select  digit  from  Keys
  
where  alpha  =   @alpha
 )
end
go

-- 存储过程:执行此存储过程创建联系人T9码速查信息数据-------------------------------------------
create   procedure  sp_createContactT9Info
as
begin
 
declare   @i   int @length   int @word   varchar ( 5 ),  @code   varchar ( 100 )
 
declare   @contactid   int @name   varchar ( 20 )
 
 
-- 先清空ContactT9Info表的数据
  delete   from  ContactT9Info
 
-- 声明一个游标取出contact表的数据,并打开此游标
  declare  contact_cursor  cursor   for
  
select  id, name  from  contact
 
open  contact_cursor
 
 
fetch   next   from  contact_cursor  into   @contactid @name
 
-- 检查@@fetch_status的值判断是否还能取得记录
  while   @@fetch_status   =   0
 
begin
  
-- 对contact_cursor中取得的每条记录进行操作
   set   @length   =   len ( @name )
  
set   @i   =   @length
  
set   @code   =   ''
 
  
while  ( @i   >=   1 )
  
begin
   
-- 从姓名的最后一个字开始,分别对每个字进行处理
    set   @word   =   substring ( @name @i 1 )
   
set   @code   =  dbo.getT9CodeFromPY(dbo.getPY( @word ))  +   @code
   
insert   into  ContactT9Info(id, T9Code)  values  ( @contactid @code )
   
set   @i   =   @i   -   1
  
end
  
fetch   next   from  contact_cursor  into   @contactid @name
 
end
 
close  contact_cursor
 
deallocate  contact_cursor
 
print   ' success! '
end
go
exec  sp_CreateContactT9Info
-- 存储过程:通过输入T9码速查联系人----------------------------------------------------------
create   procedure  sp_getContactByT9
 
@T9Code   varchar ( 100 )
as
 
select  c. *
 
from  Contact c
 
where   exists  (
  
select   *   from  ContactT9Info i
  
where  i.id  =  c.id  and  i.T9Code  like   @T9Code   +   ' % '
    )
go
exec  sp_getContactByT9  ' 264 '

-- 存储过程:显示所有联系人的信息
create   procedure  sp_showContactInfo
as
 
select  c. * ,i.T9Code  from  Contact c
 
left   join  ContactT9Info i  on  c.id  =  i.id
go
exec  sp_showContactInfo

附加一部分代码,我已经忘了是做什么用的了,看来注释确实是个好习惯

select   *   from  NumberToChar 

drop   function  Get_StrArrayLength

CREATE   function  Get_StrArrayLength

(

 
@str   varchar ( 1024 ),   -- 要分割的字符串

 
@split   varchar ( 10 )    -- 分隔符号

)

returns   int

as

begin

 
declare   @location   int

 
declare   @start   int

 
declare   @length   int

 

 
set   @str = ltrim ( rtrim ( @str ))

 
set   @location = charindex ( @split , @str )

 
set   @length = 1

 
while   @location <> 0

 
begin

   
set   @start = @location + 1

   
set   @location = charindex ( @split , @str , @start )

   
set   @length = @length + 1

 
end

 
return   @length

end

这篇关于T9输入法在SQL Server2005 中的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/856311

相关文章

Ubuntu中远程连接Mysql数据库的详细图文教程

《Ubuntu中远程连接Mysql数据库的详细图文教程》Ubuntu是一个以桌面应用为主的Linux发行版操作系统,这篇文章主要为大家详细介绍了Ubuntu中远程连接Mysql数据库的详细图文教程,有... 目录1、版本2、检查有没有mysql2.1 查询是否安装了Mysql包2.2 查看Mysql版本2.

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Python3.6连接MySQL的详细步骤

《Python3.6连接MySQL的详细步骤》在现代Web开发和数据处理中,Python与数据库的交互是必不可少的一部分,MySQL作为最流行的开源关系型数据库管理系统之一,与Python的结合可以实... 目录环境准备安装python 3.6安装mysql安装pymysql库连接到MySQL建立连接执行S

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("