我们都知道ERP系统的业务数据最终几乎都是保存在数据库里的,尤其是开发报表,会频繁的去处理这些数据,那么Genero AP端如何去链接数据库的呢?这里提供了一个小的案列,帮助理解:
Step1:首先看FORM,根据输入的store number关联查找先关信息,最终以Table形式显示出来
step2:上代码
#定于全局变量及mycust动态数组(Table)
GLOBALS
DEFINE l_input INTEGER
DEFINE i INT
DEFINE mycust DYNAMIC ARRAY OF RECORD
store_num INTEGER,
store_name CHAR(20),
addr CHAR(20),
city CHAR(20),
contact_name CHAR(30),
phone CHAR(18),
order_num INT,
quantity SMALLINT,
PRICE DECIMAL(8,2),
stock_num INT,
description CHAR(15)
END RECORD
END GLOBALS
MAIN
DEFINE l_connect STRING
#sqlite的driver 'dbmsqt3xx', source值为数据库在本地或服务器的绝对地址
LET l_connect = "AnyDBName+driver='dbmsqt3xx',source='C:/Users/mtk68099/Documents/My Genero Files/samples/databases/custdemo/sqlite/custdemo.db'"
#通过driver和source链接到数据库,不需要密码访问
CONNECT TO l_connect
#判断是否链接成功,失败则退出
IF SQLCA.SQLCODE THEN
EXIT PROGRAM SQLCA.SQLCODE
END IF
#成功链接到数据库以后,就可以展开相关的数据库操作了(比如游标)
PREPARE p1 FROM
"SELECT a.store_num, a.store_name, a.addr, a.city, a.contact_name, a.phone,
b.order_num, c.quantity, c.price, c.stock_num, d.description
FROM customer AS a
INNER JOIN orders AS b
INNER JOIN items AS c
INNER JOIN stock AS d
WHERE a.store_num = b.store_num AND b.order_num = c.order_num
AND c.stock_num = d.stock_num AND a.store_num = ?"
DECLARE c1 CURSOR FOR p1
CLOSE WINDOW SCREEN
OPEN WINDOW w1 AT 1,1 WITH FORM "ConnectToDB"
CALL display_store() #store相关信息显示
MENU ""
ON ACTION NEXT
CALL display_store()
ON ACTION QUIT
EXIT MENU
ON ACTION CLOSE
EXIT MENU
END MENU
CLOSE FORM ConnectToDB
CLOSE WINDOW w1
END MAIN
FUNCTION display_store()
INPUT l_input FROM store_input #store number输入传递到变量
AFTER FIELD store_input #输入完成后打开游标操作
OPEN c1 USING l_input
WHILE i >= 1
CLEAR record1[i].* #清除上次的屏幕显示数据
LET i = i - 1
END WHILE
CALL mycust.clear() #清除数组变量值
FOREACH c1 INTO mycust[i+1].*
DISPLAY mycust[i+1].* TO record1[i+1].* #将数据库抓去的值显示到屏幕
LET i = i + 1
END FOREACH
CLOSE c1
END INPUT
END FUNCTION
step3:编译执行,显示结果
总结:l_connect 只是个人觉得比较好的写法,具体的driver和source对于每种数据库而言都是不一样的参数,当然你也可以通过fgl配置文件直接指定,下次就不用写入参数了
官方的文档有这样的说明
理解这几个DB connect参数,只要是genero支持的数据库,对号入座就是了,刚好我的本地也有MySQL 5.5,我的MySQL就可以这样连接(记得配置windows/unix环境变量,指定path到数据库lib)
MAIN
DEFINE l_connect STRING
LET l_connect = "AnyDBName+driver='dbmmys55x',source = 'mytest@localhost:3306'"
CONNECT TO l_connect USER "root" USING "123456"
IF SQLCA.SQLCODE THEN
EXIT PROGRAM SQLCA.SQLCODE
END IF
DISPLAY SQLCA.SQLCODE
END MAIN
结果OK,返回SQLCA.SQLCODE = 0