Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Como ejecutar a un procedimiento almacenado en c++ para mysql

Estas en el tema de Como ejecutar a un procedimiento almacenado en c++ para mysql en el foro de C/C++ en Foros del Web. Hola a todos, si alguien sabe como llamar a procedimiento almacenado desde c++ para el servidor de mysql agreadezco aun no consigo nada claro. Agradezco ...
  #1 (permalink)  
Antiguo 12/10/2013, 20:54
Avatar de rocha7778  
Fecha de Ingreso: mayo-2013
Ubicación: Cartagena Colombia
Mensajes: 79
Antigüedad: 10 años, 11 meses
Puntos: 1
Como ejecutar a un procedimiento almacenado en c++ para mysql

Hola a todos, si alguien sabe como llamar a procedimiento almacenado
desde c++ para el servidor de mysql agreadezco aun no consigo nada claro.

Agradezco deje un link donde pueda ver un ejemplo.

Gracias.
  #2 (permalink)  
Antiguo 13/10/2013, 04:22
 
Fecha de Ingreso: agosto-2012
Mensajes: 601
Antigüedad: 11 años, 8 meses
Puntos: 83
Respuesta: Como ejecutar a un procedimiento almacenado en c++ para mysql

En el web de mysql tienes las secciones de triggers:

http://dev.mysql.com/doc/refman/5.1/...tatements.html

http://dev.mysql.com/doc/refman/5.1/...tatements.html

En el web tienes tambien la referencia de las funciones, o si te has bajado el manual de referencia en la seccion de la api para C tambien está todo.

Basicamente lo que te interesa es la instruccion CALL, y en el manual de referencia está la descripcion del funcionamiento, argumentos y un ejemplo; no lo he buscado en la web de mysql pero supongo que debe de haber lo mismo que en el manual de referencia, en cualquier caso te pongo una copia de la referencia a CALL:

Código SQL:
Ver original
  1. 12.2.1. CALL Syntax
  2. CALL sp_name([parameter[,...]])
  3. CALL sp_name[()]
  4.  
  5. The CALL statement invokes a stored PROCEDURE that was defined previously WITH CREATE PROCEDURE.
  6.  
  7. AS OF MySQL 5.0.30, stored procedures that take no arguments can be invoked WITHOUT parentheses. That IS, CALL p() AND CALL p are equivalent.
  8.  
  9. CALL can pass back VALUES TO its caller USING parameters that are declared AS OUT OR INOUT parameters. WHEN the PROCEDURE RETURNS, a client program can also obtain the NUMBER
  10. OF ROWS affected FOR the final statement executed WITHIN the ROUTINE: At the SQL level, CALL the ROW_COUNT() FUNCTION; FROM the C API, CALL the mysql_affected_rows() FUNCTION.
  11.  
  12. TO GET back a VALUE FROM a PROCEDURE USING an OUT OR INOUT parameter, pass the parameter BY means OF a USER variable, AND THEN CHECK the VALUE OF the variable
  13. after the PROCEDURE RETURNS. (IF you are calling the PROCEDURE FROM WITHIN another stored PROCEDURE OR FUNCTION, you can also pass a ROUTINE parameter OR LOCAL ROUTINE variable AS an IN OR INOUT parameter.)
  14. FOR an INOUT parameter, initialize its VALUE BEFORE passing it TO the PROCEDURE. The following PROCEDURE has an OUT parameter that the PROCEDURE sets TO the CURRENT server version,
  15. AND an INOUT VALUE that the PROCEDURE increments BY one FROM its CURRENT VALUE:
  16.  
  17. CREATE PROCEDURE p (OUT ver_param VARCHAR(25), INOUT incr_param INT)
  18. BEGIN
  19.   # SET VALUE OF OUT parameter
  20.   SELECT VERSION() INTO ver_param;
  21.   # INCREMENT VALUE OF INOUT parameter
  22.   SET incr_param = incr_param + 1;
  23. END;
  24.  
  25. BEFORE calling the PROCEDURE, initialize the variable TO be passed AS the INOUT parameter. After calling the PROCEDURE, the VALUES OF the two VARIABLES will have been SET OR modified:
  26.  
  27. mysql> SET @INCREMENT = 10;
  28. mysql> CALL p(@version, @INCREMENT);
  29. mysql> SELECT @version, @INCREMENT;
  30. +------------+------------+
  31. | @version   | @INCREMENT |
  32. +------------+------------+
  33. | 5.0.25-log | 11         |
  34. +------------+------------+
  35.  
  36. IN prepared CALL statements used WITH PREPARE AND EXECUTE, placeholder support IS available IN MySQL 5.0 FOR IN parameters, but NOT FOR OUT OR INOUT parameters. TO WORK
  37. around this limitation FOR OUT AND INOUT parameters, TO forgo the USE OF placeholders: Refer TO USER VARIABLES IN the CALL statement itself AND do NOT specify them IN the EXECUTE statement:
  38.  
  39. mysql> SET @INCREMENT = 10;
  40. mysql> PREPARE s FROM 'CALL p(@version, @increment)';
  41. mysql> EXECUTE s;
  42. mysql> SELECT @version, @INCREMENT;
  43. +-----------------+------------+
  44. | @version        | @INCREMENT |
  45. +-----------------+------------+
  46. | 6.0.7-alpha-log | 11         |
  47. +-----------------+------------+
  48.  
  49. TO WRITE C programs that USE the CALL SQL statement TO EXECUTE stored procedures that produce RESULT sets, the CLIENT_MULTI_RESULTS flag must be enabled. This IS
  50. because each CALL RETURNS a RESULT TO indicate the CALL STATUS, IN addition TO any RESULT sets that might be returned BY statements executed WITHIN the PROCEDURE. CLIENT_MULTI_RESULTS
  51. must also be enabled IF CALL IS used TO EXECUTE any stored PROCEDURE that contains prepared statements. It cannot be determined WHEN such a PROCEDURE IS loaded whether
  52. those statements will produce RESULT sets, so it IS necessary TO assume that they will.
  53.  
  54. CLIENT_MULTI_RESULTS can be enabled WHEN you CALL mysql_real_connect(), either explicitly BY passing the CLIENT_MULTI_RESULTS flag itself, OR implicitly BY passing CLIENT_MULTI_STATEMENTS (which also enables CLIENT_MULTI_RESULTS).
  55.  
  56. TO process the RESULT OF a CALL statement executed via mysql_query() OR mysql_real_query(), USE a loop that calls mysql_next_result() TO determine whether there are
  57. more results. FOR an example, see SECTION 20.8.12, “C API Support FOR Multiple Statement Execution”.
  58.  
  59. FOR programs written IN a LANGUAGE that provides a MySQL interface, there IS no native method FOR directly retrieving the results OF OUT OR INOUT parameters FROM CALL statements.
  60. TO GET the parameter VALUES, pass user-defined VARIABLES TO the PROCEDURE IN the CALL statement AND THEN EXECUTE a SELECT statement TO produce a RESULT SET containing the
  61. variable VALUES. TO handle an INOUT parameter, EXECUTE a statement prior TO the CALL that sets the corresponding USER variable TO the VALUE TO be passed TO the PROCEDURE.
  62.  
  63. The following example illustrates the technique (WITHOUT error checking) FOR the stored PROCEDURE p described earlier that has an OUT parameter AND an INOUT parameter:
  64.  
  65. mysql_query(mysql, "SET @increment = 10");
  66. mysql_query(mysql, "CALL p(@version, @increment)");
  67. mysql_query(mysql, "SELECT @version, @increment");
  68. RESULT = mysql_store_result(mysql);
  69. ROW = mysql_fetch_row(RESULT);
  70. mysql_free_result(RESULT);
  71.  
  72. After the preceding code executes, ROW[0] AND ROW[1] contain the VALUES OF @version AND @INCREMENT, respectively.


Saludos
vosk

Etiquetas: almacenado, mysql, procedimiento
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:39.