VastbaseG100

基于openGauss内核开发的企业级关系型数据库。

Menu

执行查询

直接执行

调用SQLAllocHandle()函数获取句柄,调用SQLExecDirect()函数执行查询。

HSTMT V_OD_hstmt = SQL_NULL_HSTMT; //语句句柄
ret = SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt);
if (!SQL_SUCCEEDED(ret))
{
 	printf("failed to allocate stmt handle");
 	return;
}

/*执行语句*/
ret = SQLExecDirect(V_OD_hstmt, (SQLCHAR *) "select * from test_stu", SQL_NTS);

/*获取结果*/
ret = SQLFetch(V_OD_hstmt);
if (ret == SQL_NO_DATA)
	return;
if (ret == SQL_SUCCESS)
{
	char buf[40];
	SQLLEN ind;

/*获取列数据*/
ret = SQLGetData(V_OD_hstmt, 1, SQL_C_CHAR, buf, sizeof(buf), &ind);
}

预编译方式执行

调用SQLAllocHandle()函数获取句柄,使用SQLPrepare预编译语句,使用SQLBindParameter绑定参数,最后调用SQLExecute执行查询。

/*初始化句柄*/
ret = SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt);
if (!SQL_SUCCEEDED(ret))
{
	printf("failed to allocate stmt handle");
	exit(1);
}

/*预编译SQL*/
ret = SQLPrepare(V_OD_hstmt, (SQLCHAR *) "SELECT * FROM test_stu WHERE id = ?", SQL_NTS);
CHECK_STMT_RESULT(ret, "SQLPrepare failed", V_OD_hstmt);
SQLLEN cbParam1 = SQL_NTS;
/*绑定参数*/
ret = SQLBindParameter(V_OD_hstmt, 1, SQL_PARAM_INPUT,
		SQL_C_SLONG,	/* value type */
		SQL_INTEGER,	/* param type */
		20,			    /* column size */
		0,				/* dec digits */
		"1",			/* param value ptr */
		0,			    /* buffer len */
		&cbParam1		/* StrLen_or_IndPtr */);
CHECK_STMT_RESULT(ret, "SQLBindParameter failed", V_OD_hstmt);

/*执行*/
ret = SQLExecute(V_OD_hstmt);
CHECK_STMT_RESULT(ret, "SQLExecute failed", V_OD_hstmt);

/*获取结果*/
ret = SQLFetch(V_OD_hstmt);
if (ret == SQL_NO_DATA)
	return;
if (ret == SQL_SUCCESS)
{
	char buf[40];
	SQLLEN ind;
	
/*获取列数据*/
	ret = SQLGetData(V_OD_hstmt, 1, SQL_C_CHAR, buf, sizeof(buf), &ind);
}