例程可通过下载
本文档中出现的sql anywhere 11在其它地方可能出现为sql anywhere panorama。
json web service
jason(javascript object notation)是一种简单、轻量级的数据交换格式,非常适用于展现和交换数据结构。虽然json独立于编程语言,但大多数情况下都使用javascript因为json对象能轻松重建和使用数据结构。sql anywhere 11通过内置的http服务器支持web服务的请求来返回json格式的结果集。结果集通过键值对的数组来返回,每个键代表结果集中的一列。
json web service例程
本例程使用sql anywhere demo数据库用json格式返回employees列表并显示在浏览器中。其中使用了json列表,员工的名字也显示了出来。employees的json列表通过一个返回到服务器的ajax请求来获得。本例使用了sql anywhere的内置http服务器,需要支持javascript的浏览器。
1. 确保没有数据库服务器运行。若有,请关闭。
2. 在命令行中,浏览至例程的json目录。
3. 执行以下命令将sql anywhere 11 demo数据库拷贝至当前目录:
copy %sqlanysamp11%demo.db .
4. 启动demo数据库的http服务器。以下命令指定http服务器的端口号为8080(如果需要,请指定其它端口):
dbsrv11 demo.db -xs http(port=8080)
5. 启动interactive sql并连接至demo数据库:
dbisql -c eng=demo;uid=dba;pwd=sql
6. 创建一个从当前目录读取html页面的存储过程。拷贝以下代码至interactive sql中,按f5运行:
create procedure sp_root()
begin
call dbo.sa_set_http_header( ''content-type'', ''text/html'' );
select xp_read_file( ''json.html'');
end;
7. 创建root web服务,返回html页面。拷贝以下代码至interactive sql中,按f5运行:
create service root
type ''raw''
authorization off
user dba
as call sp_root();
8. 创建employees web服务,由json.html调用。拷贝以下代码至interactive sql中,按f5运行:
create service employees
type ''json''
authorization off
user dba
as select * from employees;
9. 打开浏览器,浏览。
10. 点击“get employees”发起ajax对emplyees web服务的请求demo数据库中的employees列表即显示。
