📄 database.cpp
字号:
#include "usergg.h"
#define DSN1 "userdata"
HENV henv;
HDBC hdbc_self,hdbc_as400b;
HSTMT hstmt_self,hstmt_as400b;
RETCODE init_odbc(),close_odbc();
extern void play_caseno(int);
extern void out_dial(int,char *);
extern PORT_TYPE port[MAX_CHAN+1];
extern MPORT_TYPE mport[MAX_STAT+1];
struct tm *tm_time;
time_t t_time;
long ldate;
int ltime;
char cc[10],cldate[9],cltime[7];
long errcaseno;
//int dt_brdnum; /* DTI board handle */
//int msidevhndl; /* MSI board handle */
char ctemp[17];
/***********
#define SQL_INVALID_HANDLE (-2)
#define SQL_ERROR (-1)
#define SQL_SUCCESS 0
#define SQL_SUCCESS_WITH_INFO 1
#define SQL_NO_DATA_FOUND 100
************/
void g_time(void)
{
_strtime(cc); //hh:mm:ss
cltime[0]=cc[0];
cltime[1]=cc[1];
cltime[2]=cc[3];
cltime[3]=cc[4];
cltime[4]=cc[6];
cltime[5]=cc[7];
cltime[6]=0;
//ltime=(cc[0]-'0')*1000+(cc[1]-'0')*100+(cc[3]-'0')*10+(cc[4]-'0'); //hhmm
ltime=(cc[0]-'0')*100000+(cc[1]-'0')*10000+(cc[3]-'0')*1000+(cc[4]-'0')*100+(cc[6]-'0')*10+(cc[7]-'0'); //hhmmss
time(&t_time);
tm_time=localtime(&t_time);
//_getsystime(&tm_time);
sprintf(cldate,"%d%02d%02d",tm_time->tm_year+1900,tm_time->tm_mon+1,tm_time->tm_mday);
ldate=tm_time->tm_year*1000+(tm_time->tm_mon+1)*100+tm_time->tm_mday;
}
int get_user(char *useno,int channel)
{
RETCODE retcode;
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH+1];
SDWORD fNativeError,cbValue;
SWORD cbErrorMsg;
SWORD cbParamLen=SQL_NTS;
char szBuffer[26];
char szCommand[256];
SQLFreeStmt(hstmt_self,SQL_CLOSE);
sprintf(szCommand,"SELECT password,telno,edate FROM use where useno='%s'",useno);
retcode=SQLExecDirect(hstmt_self,(unsigned char *)szCommand,SQL_NTS);
if(retcode<=SQL_ERROR)
{
SQLError(henv,hdbc_self,hstmt_self,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error execute:%d %s %s\n",retcode,szSqlState,szErrorMsg);
return(-1);
}
else
{
retcode=SQLFetch(hstmt_self);
if (retcode==SQL_NO_DATA_FOUND)
{
return(0);
}
retcode=SQLGetData(hstmt_self,3,SQL_C_CHAR,szBuffer,sizeof(szBuffer),&cbValue);
if(retcode<=SQL_ERROR)
{
SQLError(henv,hdbc_self,hstmt_self,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error getdata:%d %s %s\n",retcode,szSqlState,szErrorMsg);
return(-1);
}
else
{
g_time();
printf("edate:%s",szBuffer);
if(strcmp(cldate,szBuffer)>0)
return 2;
}
retcode=SQLGetData(hstmt_self,2,SQL_C_CHAR,szBuffer,sizeof(szBuffer),&cbValue);
if(retcode<=SQL_ERROR)
{
SQLError(henv,hdbc_self,hstmt_self,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error getdata:%d %s %s\n",retcode,szSqlState,szErrorMsg);
return(-1);
}
else
{
if(strlen(szBuffer)==0)
strcpy(port[channel].tele,"0");
else
strcpy(port[channel].tele,szBuffer);
}
retcode=SQLGetData(hstmt_self,1,SQL_C_CHAR,szBuffer,sizeof(szBuffer),&cbValue);
if(retcode<=SQL_ERROR)
{
SQLError(henv,hdbc_self,hstmt_self,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error getdata:%d %s %s\n",retcode,szSqlState,szErrorMsg);
return(-1);
}
else
strcpy(port[channel].pass,szBuffer);
}
return 1;
}
int check_last(int channel)
{
RETCODE retcode;
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH+1];
SDWORD fNativeError;
SWORD cbErrorMsg;
SWORD cbParamLen=SQL_NTS;
//char szBuffer[26];
char szCommand[256];
g_time();
SQLFreeStmt(hstmt_self,SQL_CLOSE);
sprintf(szCommand,"SELECT checkno FROM check where checkno='%s' and useno='%s'",port[channel].checkno,port[channel].useno);
retcode=SQLExecDirect(hstmt_self,(unsigned char *)szCommand,SQL_NTS);
if(retcode<=SQL_ERROR)
{
SQLError(henv,hdbc_self,hstmt_self,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error execute:%d %s %s\n",retcode,szSqlState,szErrorMsg);
return(-1);
}
else
{
retcode=SQLFetch(hstmt_self);
if (retcode==SQL_NO_DATA_FOUND)
{
SQLFreeStmt(hstmt_self,SQL_CLOSE);
sprintf(szCommand,"insert into check values('%s','%s','%s','%s','%s')",port[channel].checkno,port[channel].useno,port[channel].bankno,cldate,cltime);
retcode=SQLExecDirect(hstmt_self,(unsigned char *)szCommand,SQL_NTS);
if(retcode<=SQL_ERROR)
{
SQLError(henv,hdbc_self,hstmt_self,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error execute:%d %s %s\n",retcode,szSqlState,szErrorMsg);
return(-1);
}
return 0;
}
else
{
return 1;
}
}
}
int get_last(int channel)
{
RETCODE retcode;
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH+1];
SDWORD fNativeError,cbValue;
SWORD cbErrorMsg;
SWORD cbParamLen=SQL_NTS;
char szBuffer[26];
char szCommand[256];
g_time();
SQLFreeStmt(hstmt_self,SQL_CLOSE);
sprintf(szCommand,"SELECT date FROM check where checkno='%s' and useno='%s'",port[channel].checkno,port[channel].useno);
retcode=SQLExecDirect(hstmt_self,(unsigned char *)szCommand,SQL_NTS);
if(retcode<=SQL_ERROR)
{
SQLError(henv,hdbc_self,hstmt_self,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error execute:%d %s %s\n",retcode,szSqlState,szErrorMsg);
return(-1);
}
else
{
retcode=SQLFetch(hstmt_self);
if (retcode==SQL_NO_DATA_FOUND)
{
return 0;
}
else
{
retcode=SQLGetData(hstmt_self,1,SQL_C_CHAR,szBuffer,sizeof(szBuffer),&cbValue);
if(retcode<=SQL_ERROR)
{
SQLError(henv,hdbc_self,hstmt_self,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error getdata:%d %s %s\n",retcode,szSqlState,szErrorMsg);
return(-1);
}
else
strcpy(port[channel].lasttime,szBuffer)==0;
return 1;
}
}
return 0;
}
int change_pass(int channel)
{
RETCODE retcode;
//char szBuffer[26];
char szCommand[256];
SQLFreeStmt(hstmt_self,SQL_CLOSE);
sprintf(szCommand,"UPDATE use SET password='%s' where useno='%s'",port[channel].pass,port[channel].useno);
retcode=SQLExecDirect(hstmt_self,(unsigned char *)szCommand,SQL_NTS);
if(retcode==SQL_SUCCESS)
{
return(0);//密码修改成功
}
else
{
return(-1);//密码修改不成功
}
}
int get_bank(char *bankno)
{
RETCODE retcode;
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH+1];
SDWORD fNativeError;
SWORD cbErrorMsg;
SWORD cbParamLen=SQL_NTS;
char szCommand[256];
SQLFreeStmt(hstmt_self,SQL_CLOSE);
sprintf(szCommand,"SELECT bankno FROM bank where bankno='%s'",bankno);
retcode=SQLExecDirect(hstmt_self,(unsigned char *)szCommand,SQL_NTS);
if(retcode<=SQL_ERROR)
{
SQLError(henv,hdbc_self,hstmt_self,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error execute:%d %s %s\n",retcode,szSqlState,szErrorMsg);
return(-1);
}
else
{
retcode=SQLFetch(hstmt_self);
if (retcode==SQL_NO_DATA_FOUND)
{
return(-1);
}
}
return 0;
}
RETCODE init_odbc()
{
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH+1];
SDWORD fNativeError;
SWORD cbErrorMsg;
RETCODE retcode;
retcode=0;
if((retcode=SQLAllocEnv(&henv))<=SQL_ERROR)
printf("error alloc env\n");
if((retcode=SQLAllocConnect(henv,&hdbc_self))<=SQL_ERROR)
{
SQLError(henv,SQL_NULL_HDBC,SQL_NULL_HSTMT,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error alloc self connect:%d %s %s\n",retcode,szSqlState,szErrorMsg);
}
if((retcode=SQLConnect(hdbc_self,(unsigned char *)DSN1,SQL_NTS,(unsigned char *)"admin",SQL_NTS,(unsigned char *)"0517",SQL_NTS))<=SQL_ERROR)
{
SQLError(henv,hdbc_self,SQL_NULL_HSTMT,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error connect:%d %s %s\n",retcode,szSqlState,szErrorMsg);
}
if((retcode=SQLAllocStmt(hdbc_self,&hstmt_self))<=SQL_ERROR)
{
SQLError(henv,hdbc_self,SQL_NULL_HSTMT,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error alloc stmt:%d %s %s\n",retcode,szSqlState,szErrorMsg);
}
return(retcode);
}
RETCODE close_odbc()
{
UCHAR szSqlState[6];
UCHAR szErrorMsg[SQL_MAX_MESSAGE_LENGTH+1];
SDWORD fNativeError;
SWORD cbErrorMsg;
RETCODE retcode;
retcode=0;
if((retcode=SQLFreeStmt(hstmt_self,SQL_DROP))<=SQL_ERROR)
printf("error free self stmt\n");
if((retcode=SQLDisconnect(hdbc_self))<=SQL_ERROR)
printf("error self disconnect\n");
if((retcode=SQLFreeConnect(hdbc_self))<=SQL_ERROR)
{
SQLError(henv,SQL_NULL_HDBC,SQL_NULL_HSTMT,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error free self connect:%d %s %s\n",retcode,szSqlState,szErrorMsg);
}
if((retcode=SQLFreeEnv(henv))<=SQL_ERROR)
{
SQLError(henv,SQL_NULL_HDBC,SQL_NULL_HSTMT,szSqlState,&fNativeError,szErrorMsg,sizeof(szErrorMsg),&cbErrorMsg);
printf("error free env:%d %s\n",retcode,szSqlState);
}
return(retcode);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -