问题描述
我对以下使用 MariaDB Connector/C API 的代码有疑问。
bool database_account_create(char *name,uint8_t *password,uint64_t salt,char *ip,uint32_t *id)
{
MysqL_STMT *statement = MysqL_stmt_init(&CONNECTION);
if (statement == NULL) {
fprintf(stderr,"MysqL_stmt_init() Failed %d: %s\n",MysqL_errno(&CONNECTION),MysqL_error(&CONNECTION));
return false;
}
// Undocumented MariaDB feature: use -1 to tell MysqL_stmt_prepare() to use strlen() on the provided string
if (MysqL_stmt_prepare(statement,"INSERT INTO accounts (name,password,salt,lastIp) VALUES (?,?,?) RETURNING id",-1) != 0) {
fprintf(stderr,"MysqL_stmt_prepare() Failed %d: %s\n",MysqL_stmt_errno(statement),MysqL_stmt_error(statement));
goto error;
}
struct InputBinder i_binder;
MysqL_BIND input[4];
input_binder_init(&i_binder,4,input);
input_binder_next_varstring(&i_binder,name);
input_binder_next_binary(&i_binder,ACCOUNT_HASHED_PASSWORD_LENGTH,password);
input_binder_next_ulong(&i_binder,&salt);
input_binder_next_varstring(&i_binder,ip);
if (MysqL_stmt_bind_param(statement,input) != 0) {
fprintf(stderr,"MysqL_stmt_bind_param() Failed %d: %s\n",MysqL_stmt_error(statement));
goto error;
}
struct OutputBinder o_binder;
MysqL_BIND output[1];
output_binder_init(&o_binder,1,output);
output_binder_next_uint(&o_binder,id);
if (MysqL_stmt_bind_result(statement,output) != 0) {
fprintf(stderr,"MysqL_stmt_bind_result() Failed %d: %s\n",MysqL_stmt_error(statement));
goto error;
}
if (MysqL_stmt_execute(statement) != 0) {
fprintf(stderr,"MysqL_stmt_execute() Failed %d: %s\n",MysqL_stmt_error(statement));
goto error;
}
if (MysqL_stmt_fetch(statement) != 0) {
fprintf(stderr,"MysqL_stmt_fetch() Failed %d: %s\n",MysqL_stmt_error(statement));
goto error;
}
MysqL_stmt_close(statement);
return true;
error:
MysqL_stmt_close(statement);
return false;
}
顾名思义,该函数通过提供名称、散列密码、salt 和 IP 向数据库添加一个新帐户,并在 id
参数中返回新帐户的 ID。
CONNECTION
是连接到数据库的有效 MysqL
结构。
struct InputBinder
和 struct OuputBinder
是辅助结构,用于为 MysqL_BIND
的字段设置正确的值。
此代码在 MysqL_stmt_bind_result()
处失败,带有 CR_NO_STMT_METADATA
。如果删除了 RETURNING
子句和输出绑定,则该函数按预期工作,只是它不会取回 ID。
我做错了吗?如果是这种情况,取回插入行的 ID 的正确方法是什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)