VastbaseG100

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

Menu

执行更新

要更改数据(执行一个insert,update或者delete),可以使用SQLExecDirect函数执行完整的SQL,也可以使用预编译的方式绑定参数后再执行。下面的示例将以预编译的方式执行。

删除

rc = SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt);
rc = SQLPrepare(V_OD_hstmt, (SQLCHAR *) "delete from test_stu where id= ? ", SQL_NTS);
cbParam1 = SQL_NTS;
rc = 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(rc, "SQLBindParameter failed", V_OD_hstmt);
rc = SQLExecute(V_OD_hstmt);
CHECK_STMT_RESULT(rc, "SQLExecute failed", V_OD_hstmt);

批处理删除

SQLINTEGER *ids = NULL;
SQLLEN *bufflenIds = NULL;
SQLUSMALLINT
/* batchcount 为批处理数 */
for (i = 0; i < batchcount; i++) {
ids[i] = i;
bufflenIds[i]=sizeof(ids[i]);
}

rc = SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &hstmt);
rc = SQLPrepare(hstmt, (SQLCHAR *) "delete from test_stu where id= ? ", SQL_NTS);
rc = SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER batchcount), sizeof(batchcount));
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
SQL_C_SLONG,	/* value type */
SQL_INTEGER,	/* param type */
sizeof(ids[0]),		/* column size */
0,		/* dec digits */
&(ids[0]),		/* param value ptr */
0,		/* buffer len */
bufflenIds	/* StrLen_or_IndPtr */);
CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
rc = SQLExecute(hstmt);
CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);

更新

rc = SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt);
rc = SQLPrepare(V_OD_hstmt, (SQLCHAR *) "update stu_id set name = ‘Lisa’ where id = ? ", SQL_NTS);
cbParam1 = SQL_NTS;
rc = SQLBindParameter(V_OD_hstmt, 1, SQL_PARAM_INPUT,
		SQL_C_SLONG,	/* value type */
		SQL_INTEGER,	/* param type */
		20,				/* column size */
		0,				/* dec digits */
		“100”,			/* param value ptr */
		0,				/* buffer len */
		&cbParam1		/* StrLen_or_IndPtr */);
		
CHECK_STMT_RESULT(rc, "SQLBindParameter failed", V_OD_hstmt);
rc = SQLExecute(V_OD_hstmt);
CHECK_STMT_RESULT(rc, "SQLExecute failed", V_OD_hstmt);

批处理更新

SQLINTEGER *ids = NULL;
SQLLEN *bufflenIds = NULL;
/* batchcount 为批处理数 */
for (i = 0; i < batchcount; i++) {
ids[i] = i;
bufflenIds[i]=sizeof(ids[i]);
}

rc =  SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &hstmt);
rc = SQLPrepare(hstmt, (SQLCHAR *) "INSERT INTO test_stu VALUES (2, ?) ", SQL_NTS);
cbParam1 = SQL_NTS;
rc = SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER batchcount), sizeof(batchcount));
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
					SQL_C_SLONG,	/* value type */
					SQL_INTEGER,	/* param type */
					sizeof(ids[0]),		/* column size */
					0,		/* dec digits */
					&(ids[0]),		/* param value ptr */
					0,		/* buffer len */
					bufflenIds	/* StrLen_or_IndPtr */);
CHECK_STMT_RESULT(rc, "SQLBindParameter failed", hstmt);
rc = SQLExecute(hstmt);
CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);

插入

rc = SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt);
rc = SQLPrepare(V_OD_hstmt, (SQLCHAR *) "INSERT INTO test_stu VALUES (2, ?) ", SQL_NTS);
cbParam1 = SQL_NTS;
rc = SQLBindParameter(V_OD_hstmt, 1, SQL_PARAM_INPUT,
		SQL_C_CHAR,	/* value type */
		SQL_CHAR,	/* param type */
		20,			/* column size */
		0,			/* dec digits */
		“Jennifer”,	/* param value ptr */
		0,			/* buffer len */
		&cbParam1	/* StrLen_or_IndPtr */);
		
CHECK_STMT_RESULT(rc, "SQLBindParameter failed", V_OD_hstmt);
rc = SQLExecute(V_OD_hstmt);
CHECK_STMT_RESULT(rc, "SQLExecute failed", V_OD_hstmt);

批处理插入

SQLINTEGER *ids = NULL;
SQLLEN *bufflenIds = NULL;
/* batchcount 为批处理数 */
for (i = 0; i < batchcount; i++) {
ids[i] = i;
bufflenIds[i]=sizeof(ids[i]);
}

rc =  SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt);
rc = SQLPrepare(V_OD_hstmt, (SQLCHAR *) "INSERT INTO test_stu VALUES (2, ?) ", SQL_NTS);
cbParam1 = SQL_NTS;
rc = SQLBindParameter(V_OD_hstmt, 1, SQL_PARAM_INPUT,
					SQL_C_SLONG,	/* value type */
					SQL_INTEGER,	/* param type */
					sizeof(ids[0]),		/* column size */
					0,		/* dec digits */
					&(ids[0]),		/* param value ptr */
					0,		/* buffer len */
					bufflenIds	/* StrLen_or_IndPtr */);
CHECK_STMT_RESULT(rc, "SQLBindParameter failed", V_OD_hstmt);
rc = SQLExecute(V_OD_hstmt);
CHECK_STMT_RESULT(rc, "SQLExecute failed", V_OD_hstmt);