ODBC数组更新和检索
ODBC 标准不支持数组类型,尤其是整数数组。而 SmartEDB 允许在模式中声明数组类型。因此,通过 ODBC 接口传递数组有时是有益的。SmartEDB SQL 引擎提供了解析整数数组的能力,有助于以下解决方法。首先,应将数组定义为字符串。例如:
"[2,4,6,8]"
然后,该字符串由 SQL 引擎解析为实际的数组。 在以下代码片段中,将 array_val 的值打印到 int8_arr_str 字符数组中,并使用此 C 字符串进行更新(为清晰起见,省略了错误检查和其他验证)。
class Person {
char<64> name;
int4 age;
float weight;
int8 array_val[10];
tree<name> by_name;
};
char int8_arr_str[MAX_INT8_ARRAY_STR_LENGTH];
//....
// Update record
void updatePersonData(Person* p)
{
SQLHSTMT hStmt;
SQLAllocStmt(hDbc, &hStmt)); // allocate statement
SQLPrepare(hStmt, (SQLCHAR*)"update Person set age=?, weight=?, array_val=? where name=?", SQL_NTS); // perpare query
// Bind parameters
SQLBindParameter(hStmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &p->age, 0, NULL);
SQLBindParameter(hStmt, 2, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_REAL, 0, 0, &p->weight, 0, NULL);
// PASSING ARRAY AS A STRING -------------
SQLBindParameter(hStmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 0, 0, (SQLPOINTER)p->int8_arr_str, 0, NULL);
SQLBindParameter(hStmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 0, 0, (SQLPOINTER)p->name, 0, NULL);
SQLExecute(hStmt); // execute prepared statement
SQLFreeStmt(hStmt, SQL_DROP); // drop statement
}
//...
p.name = "John Smith";
p.age = 36;
p.weight = 75.2f;
c = p.int8_arr_str;
*c = '\0';
c += sprintf(c, "[");
for (int i = 0; i < 10; i++) {
c += sprintf(c, "%lld", 100LL + i);
if (i != 9)
c += sprintf(c, ",");
}
c += sprintf(c, "]");
updatePersonData(&p);