使用MySQL C API-检查使用准备好的语句插入行是否成功

使用MySQL C API-检查使用准备好的语句插入行是否成功,mysql,c,prepared-statement,Mysql,C,Prepared Statement,我开始学习如何使用MySQL C API,并遇到了一些准备好的语句。我以前没有在其他语言中使用过这些,所以对我来说都是新的 我在网上查过了,我知道了如何使用准备好的语句从SELECT查询中检索数据,现在我要做的是INSERT一些数据,看看是否成功。我的第一部分已经写得差不多了,但是我的问题是:如何才能确定我的插入操作是否成功执行? 我已经通读了一些MySQL文档中的C API/prepared语句,还有Google/search等等。我所能找到的例子只有SELECTprepared语句,仅此而已

我开始学习如何使用MySQL C API,并遇到了一些准备好的语句。我以前没有在其他语言中使用过这些,所以对我来说都是新的

我在网上查过了,我知道了如何使用准备好的语句从
SELECT
查询中检索数据,现在我要做的是
INSERT
一些数据,看看是否成功。我的第一部分已经写得差不多了,但是我的问题是:如何才能确定我的
插入操作是否成功执行?

我已经通读了一些MySQL文档中的C API/prepared语句,还有Google/search等等。我所能找到的例子只有
SELECT
prepared语句,仅此而已

我附加了一些我创建的代码,这些代码成功地插入了一行

#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "session.h"

char *createSessionId(MYSQL *dbc) {
    const char *sPS2 = "INSERT INTO `sessions` (`SessionID`) VALUES (?)";
    char *sID;
    MYSQL_STMT *stmt;
    MYSQL_BIND param[1];
    unsigned long l = 32;

    // Allocate a statement handle
    stmt = mysql_stmt_init(dbc);
    if(stmt == NULL) {
        printf("Unable to create new session: Could not init statement handle\n");
        return NULL;
    }

    // Init
    memset(param, 0, sizeof(param));
    sID = malloc(33);
    sprintf(sID, "01234567890123456789012345678901");
    if(mysql_stmt_prepare(stmt, sPS2, strlen(sPS2)) != 0) {
        printf("Unable to create new session: Could not prepare statement\n");
        return NULL;
    }

    // Initialise param structure
    param[0].buffer_type = MYSQL_TYPE_VARCHAR;
    param[0].buffer = sID;
    param[0].is_unsigned = 0;
    param[0].is_null = 0;
    param[0].length = &l;

    // Bind param structure to statement
    if(mysql_stmt_bind_param(stmt, param) != 0) {
        printf("Unable to create new session: Could not bind parameters\n");
        return NULL;
    }

    // Execute prepared statement
    if(mysql_stmt_execute(stmt) != 0) {
        printf("Unable to create new session: Could not execute statement\n");
        return NULL;
    }

    mysql_stmt_free_result(stmt);
    mysql_stmt_close(stmt);

    return sID;
}
#包括
#包括
#包括
#包括
#包括“session.h”
char*createSessionId(MYSQL*dbc){
const char*sPS2=“插入到`sessions`(`SessionID`)值(?);
字符*sID;
MYSQL_STMT*STMT;
MYSQL_BIND参数[1];
无符号长l=32;
//分配语句句柄
stmt=mysql\u stmt\u init(dbc);
如果(stmt==NULL){
printf(“无法创建新会话:无法初始化语句句柄\n”);
返回NULL;
}
//初始化
memset(param,0,sizeof(param));
sID=malloc(33);
sprintf(sID,“012345678901234567890123456789012345678901”);
如果(mysql\u stmt\u prepare(stmt,sPS2,strlen(sPS2))!=0){
printf(“无法创建新会话:无法准备语句\n”);
返回NULL;
}
//初始化参数结构
参数[0]。buffer\u type=MYSQL\u type\u VARCHAR;
参数[0]。缓冲区=sID;
参数[0]。无符号=0;
参数[0]。为_null=0;
参数[0]。长度=&l;
//将参数结构绑定到语句
if(mysql\u stmt\u bind\u param(stmt,param)!=0){
printf(“无法创建新会话:无法绑定参数\n”);
返回NULL;
}
//执行准备好的语句
如果(mysql\u stmt\u execute(stmt)!=0){
printf(“无法创建新会话:无法执行语句\n”);
返回NULL;
}
mysql_stmt_free_result(stmt);
mysql\u stmt\u close(stmt);
返回sID;
}

您已经有了代码

if(mysql_stmt_execute(stmt) != 0) {
        printf("Unable to create new session: Could not execute statement\n");
        return NULL;
}
如果失败,则不会插入任何行。这些文件包含一个完整的

您还可以在成功执行mysql_stmt_execute()之后使用()来确定插入/更新/删除了多少行