任务:C的应用程序往DB2数据库中插入cardid,name,idcard(卡号,姓名,身份证号),需要在插入操作前检查数据中是否有相同的身份证号。用CLI的话我只会让检查和插入操作在同一模块进行,如果将检查工作单独写一个函数的话:
nt CheckObj(char *tb,char *obj,char *value)
{
/*检查身份证号是否已存在*/
int RetCode;
extern ConHandle;
extern StmtHandle;
extern SQLStmt[255];
/*定义查询语句句柄*/
RetCode = SQLAllocHandle(SQL_HANDLE_STMT,ConHandle,&StmtHandle);
strcpy((char*)SQLStmt, "select * from ? where ?=? ");
RetCode = SQLPrepare(StmtHandle,SQLStmt,SQL_NTS);
RetCode = SQLBindParameter(StmtHandle,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,sizeof(tb),0,tb,0,0); /*绑定参数tb与 '? '*/
RetCode = SQLBindParameter(StmtHandle,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,sizeof(obj),0,obj,0,0); /*绑定参数idcard与 '? '*/
RetCode = SQLBindParameter(StmtHandle,3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,sizeof(Value),0,value,0,0); /*绑定参数value与 '? '*/
RetCode = SQLExecute(StmtHandle);
RetCode = SQLFetch(StmtHandle);
if(SQL_NO_DATA != RetCode)
return NORECODE;
SQLStmt[0] = '\0 ';
SQLFreeHandle(SQL_HANDLE_STMT,StmtHandle);
return 0;
}
//
int InsertCounter(void)
{
SQLHANDLE EnvHandle=0;
SQLHANDLE ConHandle=0;
SQLHANDLE StmtHandle=0;
SQLRETURN RetCode=SQL_SUCCESS;
SQLCHAR SQLStmt[255];
char *cardid;
char *name;
char *idcard;
/*定义环境句柄*/
SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&EnvHandle);
/*声明db2版本*/
SQLSetEnvAttr(EnvHandle,SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,SQL_IS_UINTEGER);
/*定义连接句柄*/
SQLAllocHandle(SQL_HANDLE_DBC,EnvHandle,&ConHandle);
/*连接数据库test*/
RetCode = SQLConnect(ConHandle,(SQLCHAR *) "usercd ",
SQL_NTS,(SQLCHAR *) "myinst ",
SQL_NTS,(SQLCHAR *) "112112 ",
SQL_NTS);
RetCode = CheckObj( "counter ", "idcard ",idcard);
if(NORECODE == RetCode)
printf( "idcard not exists ");
}/*省略部全纠错与句柄释放的语句*/
编译器会报错误和警告: