MySQL C ++ / Connector setClientOption to support multiple statements

I have a MySQL client written in C ++. I want to enable the MultipleStatement parameter as described here, but for C ++, of course:

http://dev.mysql.com/doc/refman/5.0/en/c-api-multiple-queries.html

This cannot be found on google. It is also not possible to find documentation for C ++ / Connector. This is pretty unpleasant.

If anyone knows any solution to my problem or any documentation page that will be very helpful.

thanks

+4
source share
2 answers

This worked for me:

sql::ConnectOptionsMap connection_properties;

connection_properties["hostName"]=sql::SQLString("localhost");
connection_properties["userName"]=sql::SQLString("username");
connection_properties["password"]=sql::SQLString("password");
connection_properties["CLIENT_MULTI_STATEMENTS"]=(true);

sql::Driver * driver = get_driver_instance();
std::unique_ptr<sql::Connection> con(driver->connect(connection_properties));

Hello,

+3
source

This works for me, here is the code I used:

#include <mysql_connection.h>
#include <driver.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>

using namespace std;
using namespace sql;

Driver *m_dDBDriver;
Connection *m_cDBConn;

m_dDBDriver = get_driver_instance();
m_cDBConn = m_dDBDriver->connect(url,user,pass);
m_cDBConn->setSchema(schema);

Statement *stmt;
ResultSet *res;

stmt = m_cDBConn->createStatement();

sQSQL = "CALL multiExecute('SELECT 1;SELECT 2;');";
stmt->execute(sQSQL);

do {
  res = stmt->getResultSet();
  while (res->next()) {
    cout << "Value: " << res->getString(1) << "\n";
  }
} while (stmt->getMoreResults());

, , , , - , MySQL:

DROP PROCEDURE IF EXISTS multiExecute;

DELIMITER $$

CREATE PROCEDURE multiExecute(VQuery VARCHAR(1000))
BEGIN

DECLARE VQueryExecuteLen INTEGER;

IF RIGHT(VQuery, 1) != ';' THEN SET VQuery = CONCAT(VQuery, ';'); END IF;

WHILE INSTR(VQuery, ";") > 0 DO

    SET VQueryExecuteLen = INSTR(VQuery, ";");

    SET @VQueryExecute = LEFT(VQuery, VQueryExecuteLen); 

    PREPARE stmt FROM @VQueryExecute;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;    

    SET VQuery = RIGHT(VQuery, CHAR_LENGTH(VQuery) - VQueryExecuteLen);

END WHILE;

END$$

DELIMITER ;

CALL multiExecute('SELECT 1; SELECT 2');

, .

, .

,

0

All Articles