使用 MySQL C API - 检查使用准备好的语句插入行是否成功 [英] Using MySQL C API - check success of inserting rows using prepared statements

查看:72
本文介绍了使用 MySQL C API - 检查使用准备好的语句插入行是否成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm beginning to learn how to use the MySQL C API and have encountered prepared statements. I haven't used these before in other languages so it's all new to me.

我在网上看过,我已经想出了如何使用准备好的语句从 SELECT 查询中检索数据,现在我要做的是 INSERT 一些数据,看看它是否成功.我已经完成了第一部分,但是我的问题是:我怎样才能知道我的 INSERT 是否已成功执行?

I've looked online and I've figured out how to use prepared statements to retrieve data from a SELECT query, and now what I'm trying to do is to INSERT some data and find out if it was successful. I've got the first part pretty much down, but my question is: how can I find out if my INSERT was successfully executed?

我已经阅读了一些关于 C API/准备好的语句的 MySQL 文档 - 并在 SO 上进行了 Google/搜索.我能找到的所有示例都是 SELECT 准备好的语句,仅此而已.

I've had a read through some MySQL documents for the C API/prepared statements - and had a Google/search on SO. All I've been able to find examples of are SELECT prepared statements and nothing more.

我附上了一些我创建的代码,它成功地插入了一行.

I've attached some code that I've created, which successfully inserts a row.

#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;
}

推荐答案

你已经有代码在

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

如果失败,则您没有插入任何行.文档包含完整的示例

If that fails, you didn't insert any rows. The docs contain a full example

您也可以使用mysql_stmt_affected_rows() 在成功的 mysql_stmt_execute() 之后找出有多少行被插入/更新/删除.

You can also use mysql_stmt_affected_rows() after a successful mysql_stmt_execute() to find out how many rows were insterted/updated/deleted.

这篇关于使用 MySQL C API - 检查使用准备好的语句插入行是否成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆