mysql变量绑定

源原件test.cpp如下

#include <iostream>

#include "mysql/mysql.h"

#include <string.h>

using namespace std;

MYSQL _mysql;

static void do_stmt_sql(MYSQL *ms_conn)
{
    if (ms_conn == NULL)
        return ;

    MYSQL_STMT *stmt = NULL;
    stmt = mysql_stmt_init(ms_conn);
    if (stmt == NULL)
    {
        std::cout << "stmt is NULL. mysql_stmt_init failed. "
            << mysql_error(ms_conn) << std::endl;
        return ;
    }
    std::cout << "MYSQL_STMT init successful." << std::endl;

    const char str_sql[] = "INSERT INTO atp_camp.FEISHU_ROBOT(url, type, remark) VALUES(?, ?, ?)";

    int res = 0;
    res = mysql_stmt_prepare(stmt, str_sql, sizeof(str_sql) - 1);
    if (res != 0)
    {
        std::cout << "mysql_stmt_prepare INSERT failed."
            << mysql_stmt_error(stmt) << std::endl;
        return ;
    }

    char szUrl[32];
    memset(szUrl, 0x00, sizeof(szUrl));
    sprintf(szUrl, "%s", "http://baidu.com");

    char szRemark[32];
    memset(szRemark, 0x00, sizeof(szRemark));
    //sprintf(szRemark, "%s", "'");
    sprintf(szRemark, "%s", "ha't'f''a'");
    //sprintf(szRemark, "%s", "\\");
    //sprintf(szRemark, "%s", "&");

    int nType = 2;

    MYSQL_BIND bind[3];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_STRING;
    //bind[0].is_null = NULL;
    bind[0].buffer = szUrl;
    bind[0].buffer_length = sizeof(szUrl);

    bind[1].buffer_type = MYSQL_TYPE_LONG;
    //bind[0].is_null = NULL;
    bind[1].buffer = &nType;
    //bind[1].buffer_length = sizeof(szUrl);

    bind[2].buffer_type = MYSQL_TYPE_STRING;
    //bind[0].is_null = NULL;
    bind[2].buffer = szRemark;
    bind[2].buffer_length = sizeof(szRemark);

    res = mysql_stmt_bind_param(stmt, bind);
    if (res != 0)
    {
        std::cout << "mysql_stmt_bind_param failed. " 
            << mysql_stmt_error(stmt) << std::endl;
        mysql_stmt_close(stmt), stmt = NULL;
        return ;
    }
    std::cout << "mysql_stmt_bind_param successful." << std::endl;

    res = mysql_stmt_execute(stmt);
    std::cout << "mysql_stmt_execute() func result is " << res << std::endl;
    if (res)
    {
        fprintf(stderr, " %s\\n", mysql_stmt_error(stmt));
    }

    mysql_stmt_close(stmt), stmt = NULL;
}

int main()
{
    mysql_thread_init();

    mysql_init(&_mysql);

    mysql_real_connect(&_mysql, "localhost", "用户", "密码",
                       "库名", 3306, NULL, 0);

    if (0 != mysql_errno(&_mysql)) {
        cout << "mysql connect failed" << endl;
        return -1;
    } else {
        cout << "mysql connect success" << endl;
    }

    do_stmt_sql(&_mysql);

    mysql_close(&_mysql);

    return 0;
}

编译时需要连接mysql的库
makefile如下

test: test.cpp
	g++ $^ -o $@ -lredis++ -lhiredis -lsqlite3 -pthread -L mysqlib/mysql/lib/ -lmysqlclient -std=c++11
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值