> 데이터 베이스 > MySQL 튜토리얼 > 调用Mysql存储过程输入输出参数_MySQL

调用Mysql存储过程输入输出参数_MySQL

WBOY
풀어 주다: 2016-06-01 13:17:21
원래의
1094명이 탐색했습니다.

存储过程如下:

DELIMITER $$

USE `gface_taisau`$$

DROP PROCEDURE IF EXISTS `p_InsertFace`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `p_InsertFace`(
IN Id BIGINT(8),
IN UserID BIGINT(8),
IN FeaLen SMALLINT(2),
IN Fea BLOB,
IN ImgUrl CHAR(64),
IN Type1 INT(4),
IN Type2 INT(4),
IN Type3 INT(4),
IN Type4 CHAR(32),
IN Remarks CHAR(64),
OUT OID BIGINT(8))
BEGIN

DECLARE l_Id BIGINT(8);
SET l_Id= 0;
IF (Id>0) THEN
SET l_Id=Id;
END IF;


INSERT INTO t_face
(ID,
UserID,
FeaLen,
Fea,
ImgUrl,
Type1,
Type2,
Type3,
Type4,
Stamp,
IsDel,
UpdateTime,
Remarks
)
VALUES
(l_Id,
UserID,
FeaLen,
Fea,
ImgUrl,
Type1,
Type2,
Type3,
Type4,
NOW(),
0,
NOW(),
Remarks
);


IF(l_Id=0) THEN
SET OID=LAST_INSERT_ID();
END IF;

END$$

DELIMITER ;

用到的结构体如下

#define MAX_FEA_LEN 10240 //定义10k
#define MAX_URL_LEN 200
//图片信息
typedef struct _FACE_INFO
{
//ULONGLONG nID;
ULONGLONG nID;
ULONGLONG nUserID;
DWORD nFeaLen;
TCHAR szFea[MAX_FEA_LEN];
TCHAR szUrl[MAX_URL_LEN];
DWORD nType1;
DWORD nType2;
DWORD nType3;
TCHAR szType4[32];
TCHAR szStamp[20];//2012-08-08 08:08:08
TCHAR szRemarks[64];
_FACE_INFO()
{
nID = 0;
nUserID = 0;
nFeaLen = 0;
memset(szFea,0,sizeof(szFea));
memset(szUrl,0,sizeof(szUrl));
nType1 = 0;
nType2 = 0;
nType3 = 0;
memset(szType4,0,sizeof(szType4));
memset(szStamp,0,sizeof(szStamp));
memset(szRemarks,0,sizeof(szRemarks));

}
}FACE_INFO, *PFACE_INFO;

//调研代码如下

HRESULT CFaceDB::RegFaceP(FACE_INFO info,ULONGLONG & nID)
{
MYSQL_STMT * stmt_mysql = mysql_stmt_init(m_pMysql);
try
{
MYSQL_BIND bind[11];
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_LONGLONG;
bind[0].buffer = (unsigned char*)&nID;
bind[0].buffer_length = (unsigned long)sizeof(nID);
bind[0].length = 0;

bind[1].buffer_type = MYSQL_TYPE_LONGLONG;
bind[1].buffer = (unsigned char*)&info.nUserID;
bind[1].buffer_length = (unsigned long)sizeof(info.nUserID);
bind[1].length = 0;

bind[2].buffer_type = MYSQL_TYPE_SHORT;
bind[2].buffer = (unsigned char*)&info.nFeaLen;
bind[2].buffer_length = (unsigned long)sizeof(info.nFeaLen);
bind[2].length = 0;

unsigned long nFeaLen = info.nFeaLen;
bind[3].buffer_type = MYSQL_TYPE_BLOB; //特征值
bind[3].buffer = (unsigned char*)info.szFea;
bind[3].buffer_length = (unsigned long)nFeaLen;
bind[3].length = &nFeaLen;

unsigned long nUrlLen = strlen(info.szUrl);
bind[4].buffer_type = MYSQL_TYPE_STRING; //ImgUrl
bind[4].buffer = (char*)info.szUrl;
bind[4].buffer_length = 64;
bind[4].length = &nUrlLen;

bind[5].buffer_type = MYSQL_TYPE_LONG; //type1
bind[5].buffer = (unsigned char*)&info.nType1;
bind[5].buffer_length = (unsigned long)sizeof(info.nType1);
bind[5].length = 0;

bind[6].buffer_type = MYSQL_TYPE_LONG; //type2
bind[6].buffer = (unsigned char*)&info.nType2;
bind[6].buffer_length = (unsigned long)sizeof(info.nType2);
bind[6].length = 0;

bind[7].buffer_type = MYSQL_TYPE_LONG; //type3
bind[7].buffer = (unsigned char*)&info.nType3;
bind[7].buffer_length = (unsigned long)sizeof(info.nType3);
bind[7].length = 0;

unsigned long nType4Len = strlen(info.szType4);
bind[8].buffer_type = MYSQL_TYPE_STRING; //typ4
bind[8].buffer = (char*)info.szType4;
bind[8].buffer_length = 32;
bind[8].length = &nType4Len;

unsigned long nRemarksLen = strlen(info.szRemarks);
bind[9].buffer_type = MYSQL_TYPE_STRING; //remarks
bind[9].buffer = (char*)info.szRemarks;
bind[9].buffer_length = 64;
//bind[9].is_null = 0;
bind[9].length = &nRemarksLen;

bind[10].buffer_type = MYSQL_TYPE_LONGLONG;
bind[10].buffer = (unsigned char*)&nID;
bind[10].buffer_length = (unsigned long)sizeof(nID);
bind[10].length = 0;


char szInsert[1024] = {0};
strcpy_s( szInsert,1024,"CALL P_INSERTFACE(?,?,?,?,?,?,?,?,?,?,@1);");
//strcpy_s( szInsert,1024,"CALL P_INSERTFACE(?,?,?);");

mysql_stmt_prepare(stmt_mysql, szInsert, (unsigned long)strlen(szInsert));
mysql_stmt_bind_param(stmt_mysql, bind);
mysql_stmt_bind_result(stmt_mysql,bind + 10);
if( mysql_stmt_execute(stmt_mysql)==0 )
{
CString cmdStr=_T("SELECT @1");
int nRet = mysql_query(m_pMysql, cmdStr);
if(0 == nRet)
{
MYSQL_RES* pResult = NULL;
pResult = mysql_store_result(m_pMysql);
if (pResult)
{
MYSQL_ROW rdRow;
rdRow = mysql_fetch_row(pResult);
nID = StrToLONG64(CString(rdRow[0]));//自定义函数 该函数是吧字符串转64位整型
}
mysql_free_result(pResult);
}

mysql_stmt_close(stmt_mysql);
return S_OK;
}
else
{
mysql_stmt_close(stmt_mysql);
return S_FALSE;
}

}
catch(...)
{
mysql_stmt_close(stmt_mysql);
}

return S_FALSE;

}


관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿