|
// MySql simple program: Select & fetch
/COPY MYSQLI_H
* Variables for Environment, Connection and Statement ID. dEnvId s LIKE(MYSQL_HDL) dConId s LIKE(MYSQL_HDL) dStmtId s LIKE(MYSQL_HDL)
* External DS 'custDS' is the image of the SELECT record set dcustDS E DS EXTNAME(S_CUST_DS)
* Various Variables used in program DUser s 30A DPassWord s 30A DiRet s 10I 0 DFlags s 10I 0 dSqlStmt s 1024 inz(*blanks) * dErrMessage s * dErrMsgLen s 10i 0 dErrNative s 10i 0 dErrStatus s 8A dMsgDta s 1000A dMysUidPwd PR extpgm('MYS_UIDPWD') d User 30A d Password 30A /include SNDPMSG_H
******************************************************************** * Program entry point * ********************************************************************
*------------------------------------------------------------------ * Get environment: Initialize MySql extension, retrieve Environment Id *------------------------------------------------------------------ c eval EnvId = mysql_get_env(*omit) c if Envid = -1 c goto ErrorFatal c endif
*------------------------------------------------------------------ * Connect to MySql database; Retrieve Connection Id *------------------------------------------------------------------ c* Get MySQL User and Password c callp MysUidPwd(User: Password) c eval ConId = mysql_connect(*omit c : User: Password: Flags: EnvId) c if ConId=-1 c goto ErrorFatal c endif
*------------------------------------------------------------------ * Prepare 'Select' statement for reading: Retrieve Statement Id *------------------------------------------------------------------ c eval SqlStmt = 'select * from test.customer' c eval stmtid = mysql_prepare(SqlStmt: ConId) c if stmtid=-1 c goto ErrorFatal c endif
/free //------------------------------------------------------------------ // Bind result set columns to 'custDS' members //------------------------------------------------------------------ iRet = mysql_bind_ds(stmtid: 'S_CUST_DS': %ADDR (custDS));
//------------------------------------------------------------------ // Execute select statement //------------------------------------------------------------------ iRet = mysql_execute(Stmtid);
//------------------------------------------------------------------ // Fetch records from result set. // After each fetch, DS 'custDS' is fill up with contain of record. //------------------------------------------------------------------ dow mysql_fetch(stmtid) = 0; dsply (Contact); enddo;
if mysql_errno(StmtId) = MYSQL_NODATA; mysql_free(stmtid); mysql_close(EnvId); return; endif; /end-free
c ErrorFatal tag /free iRet = mysql_error_info(ErrMessage: ErrMsgLen :ErrNative: ErrStatus: *omit); MsgDta = %str(ErrMessage: 1000); SPMErrCode.QUSBPRV = 0; callp SendPgmMsg ('CPF9898': 'QCPFMSG QSYS': MsgDta: %len(MsgDta): '*ESCAPE': '*PGMBDY': 1: SPMMsgKey: SPMErrCode); return; /end-free
|