ODBC驱动可以搜索“数据库类型 windows ODBC 插件”安装;DSN需要在开始菜单,Windows管理工具中找“ODBC 数据源(32 位)”,此处如果用32位的AHK则用32位的版本添加,不然无法访问。
a := new adodb a.open("DSN=testdb;UID=thinkai;PWD=password;") ;到DSN里面配置了用户名密码则UID PWD不用配置。 ret := a.GetTable("select getdate()") MsgBox % ret.1.1 class adodb { ;static conn __New() ;新建 { this.conn:= ComObjCreate("ADODB.connection") ;初始化COM } open(connect_str) ;打开文件 { try this.conn.Open(connect_str) catch e return e.Message } close() ;关闭文件 { this.conn.Close() } GetTable(sql) { t := [] query := this.conn.Execute(sql) if RegExMatch(sql,"i)^select*") { try { fetchedArray := query.GetRows() ;取出数据(二维数组) colSize := fetchedArray.MaxIndex(1) + 1 ;列最大值 tips:从0开始 所以要+1 rowSize := fetchedArray.MaxIndex(2) + 1 ;行最大值 tips:从0开始 所以要+1 loop, % rowSize { i := (y := A_index) - 1 t[y] := [] loop, % colSize { j := (x := A_index) - 1 t[y][x] := fetchedArray[j,i] ;取出二维数组内值 } } } query.Close() return t } } }
Elastix 2.5的系统是Centos 5.9,Asterisk 11。在测试dialpan拨号方案使用odbc mysql的时候报错驱动/usr/lib64/libmyodbc3_r.so找不到,经验证是系统的mysql-connector-odbc没有安装。
在通过yum安装的时候又发现源已经停止服务了,无法下载,找到一个国外源fq下载完,在另外一台机子上则是选择本地安装yum包,下载地址:
https://centos.pkgs.org/5/centos-x86_64/mysql-connector-odbc-3.51.26r1127-2.el5.x86_64.rpm.html (x64)
https://centos.pkgs.org/5/centos-i386/mysql-connector-odbc-3.51.26r1127-2.el5.i386.rpm.html (x86)
下载完后可以放到当前目录 shell执行:
yum -y localinstall mysql-connector-odbc-3.51.26r1127-2.el5.x86_64.rpm
安装完成后还需配置/etc/odbc.ini:
[MYSQL-asteriskcdrdb] Description = MySQL connection to 'asterisk' database Driver = MySQL Database = asteriskcdrdb Server = localhost Port = 3306 Socket = /var/lib/mysql/mysql.sock User = root Password = MyPass Option = 3
配置完成后可以在shell中测试:
[root@Elastix ~]# isql -v MYSQL-asteriskcdrdb +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> show tables; +-----------------------------------------------------------------+ | Tables_in_asteriskcdrdb | +-----------------------------------------------------------------+ | cdr | +-----------------------------------------------------------------+ SQLRowCount returns 1 1 rows fetched
这就算完成了一半,还需在/etc/asterisk/res_odbc.conf中配置dsn:
[asteriskcdrdb] enabled=>yes dsn=>MySQL-asteriskcdrdb pooling=>no limit=>1 pre-connect=>yes share_connections => yes isolation => repeatable_read forcecommit => yes
在/etc/asterisk/func_odbc.conf中配置自定义函数(此处dsn指res_odbc.conf中的[name]):
[SQL] dsn=asteriskcdrdb readsql=${ARG1} synopsis=ODBC_SQL(sql)
具体配置说明详见:https://wiki.asterisk.org/wiki/display/AST/Getting+Asterisk+Connected+to+MySQL+via+ODBC
http://www.asteriskdocs.org/en/3rd_Edition/asterisk-book-html-chunk/getting_funky.html
配置完后注意在Asterisk CLI中执行reload或者shell执行:
asterisk -x reload
最后就可以在dialpan中测试使用了,如下面的例子(/etc/asterisk/extensions.conf)拨打876在asterisk日志中输出结果:
[from-internal] exten => 876,1,Answer exten => 876,n,Noop(${ODBC_SQL("select now()")}) exten => 876,n,Noop(${ODBC_SQL("select top 1 uniquid from cdr order by calldate desc")}) exten => 876,n,Background(star) exten => 876,n,Wait(60) include => from-internal-noxfer include => from-internal-xfer include => bad-number ; auto-generated
49 queries in 1.374 seconds |