Pregunta

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

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

It is impossible to find this on google. Also it is impossible to find any documentation for the C++/Connector. This is pretty frustrating.

If anyone knows any solution for my problem or any documentation page that would be very very helpful.

Thank you

¿Fue útil?

Solución

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));

Regards,

Otros consejos

I have this working, here is the code I have 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());

Note that I was looking for this solution to be able to bundle queries, not call stored procedures specifically, the following is a stored procedure that enables this in 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');

Whether this is quicker or slower than launching each query independently depends on the lag in network infrastructure between your code and the DB, although in my case solutions are comparative in terms of speed when bundling queries.

Post back if you have any issues with implementation.

Regards,

James

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top