mysql et c++ sous codeblocks erreur o niveau de insert

mysql et c++ sous codeblocks erreur o niveau de insert - C++ - Programmation

Marsh Posté le 06-05-2009 à 09:29:36    

Bonjour voici mon code  
 

Code :
  1. #include <iostream>
  2. #include "Facturation.h"
  3. #include <string>
  4. #include <winsock.h>
  5. #include "MYSQL\mysql.h"
  6. #define def_nom_hote NULL // hote auquel se connecte le prog localhost
  7. #define def_nom_utilisateur NULL // par defaut le mien
  8. #define def_motdepasse NULL
  9. int c= 11;
  10. using namespace std;
  11. int main()
  12. {
  13. MYSQL *ptconn;
  14.   ptconn=mysql_init(NULL);
  15. if(mysql_real_connect(ptconn,"localhost","root","","facturation",0,NULL,0)){cout<< "all gud"<<endl;} else{ cout<<"toto"<<endl;}
  16.      int a=11;
  17.   string b="today";
  18.     int  c=1;
  19. string d="achatdetomate ";
  20.   float e=50.7;
  21.   string f="today";
  22.   float g=21.4;
  23. if( mysql_query(ptconn, "INSERT INTO facture(numfact,date) VALUES ('c','b')" )==0)
  24. {  cout<<"gud insert"<<endl;}
  25. else
  26. { cout<<"toto1"<<endl;cout << mysql_error(ptconn) << endl;}
  27.    if( mysql_query(ptconn, " SELECT * from facture" )==0)
  28.      {  cout<<"gud select"<<endl; }
  29.    else{ cout<<" toto"<<endl;
  30.    cout << mysql_error(ptconn) << endl;}
  31. MYSQL_RES *result = NULL;
  32. result = mysql_use_result(ptconn);
  33. if(result == NULL)
  34. {
  35. cout << "Résultat vide" << endl;
  36. cout << mysql_error(ptconn) << endl;
  37. }
  38. //On récupère le nombre de champs
  39. unsigned int nb_champs = mysql_num_fields(result);
  40. if(nb_champs != 3) cout << "C'est bizarre" << endl;
  41. // lecture du resultat
  42. MYSQL_ROW row;
  43. while ((row = mysql_fetch_row(result)))
  44. {
  45. cout << "ID : " << row[0] << ", Nom : " << row[1]
  46. << ", Prenom : " << row[2] << endl;
  47. }
  48. mysql_free_result(result);
  49. mysql_close(ptconn);
  50.     return 0;
  51. }


 
Le problème se situe au niveau de  
if( mysql_query(ptconn, "INSERT INTO facture(numfact,date) VALUES ('c','b')" )==0)
J'insere "c" et "b" et non leurs valeurs.
Je ne vois pas comment faire

Reply

Marsh Posté le 06-05-2009 à 09:29:36   

Reply

Marsh Posté le 06-05-2009 à 10:52:14    

babylicious a écrit :

J'insere "c" et "b" et non leurs valeurs.


c'est exactement ce que ton code demande.
mysql_query exécute la requête passée en paramètre. A toi de passer une requête valide.
Au passage, renseigne-toi sur les requêtes préparées (prepared statements).


---------------
Can't buy what I want because it's free -
Reply

Marsh Posté le 24-05-2009 à 23:22:49    

Voici le code donné pour étudier les prepared statement.Le nouveau  est que j'ai une erreur mysql_stmt_init@4 et ce pour tous les prepared statements que j'utilise.Je ne sais pas quoi faire car J'ai bien rajouté libmysqlclient.a

Code :
  1. #include <iostream>
  2. #include <winsock.h>
  3. #include "MYSQL\mysql.h"
  4. #include <string>
  5. #include <stdio.h>
  6. #define STRING_SIZE 50
  7. #define DROP_SAMPLE_TABLE "DROP TABLE IF EXISTS test_table"
  8. #define CREATE_SAMPLE_TABLE "CREATE TABLE test_table(col1 INT,\
  9.                                                  col2 VARCHAR(40),\
  10.                                                  col3 SMALLINT,\
  11.                                                  col4 TIMESTAMP)"
  12. #define INSERT_SAMPLE "INSERT INTO test_table(col1,col2,col3) VALUES(?,?,?)"
  13. using namespace std;
  14. int main()
  15. {
  16. MYSQL_STMT    *stmt;
  17. MYSQL_BIND    bind[3];
  18. my_bool       is_null;
  19. my_ulonglong  affected_rows;
  20. int           param_count;
  21. short         small_data;
  22. int           int_data;
  23. char          str_data[STRING_SIZE];
  24. unsigned long str_length;
  25. MYSQL *mysql;
  26. if(mysql_real_connect(mysql,"localhost","root","","facturation",0,NULL,0)){cout<< "all gud"<<endl;}
  27.   mysql=mysql_init(NULL);
  28.   stmt = mysql_stmt_init(mysql);
  29.       if (!stmt)
  30. {
  31.   fprintf(stderr, " mysql_stmt_init(), out of memory\n" );
  32.   exit(0);
  33. }
  34. if (mysql_query(mysql, DROP_SAMPLE_TABLE))
  35. {
  36.   fprintf(stderr, " DROP TABLE failed\n" );
  37.   fprintf(stderr, " %s\n", mysql_error(mysql));
  38.   exit(0);
  39. }
  40. if (mysql_query(mysql, CREATE_SAMPLE_TABLE))
  41. {
  42.   fprintf(stderr, " CREATE TABLE failed\n" );
  43.   fprintf(stderr, " %s\n", mysql_error(mysql));
  44.   exit(0);
  45. }
  46. /* Prepare an INSERT query with 3 parameters */
  47. /* (the TIMESTAMP column is not named; it will */
  48. /* be set to the current date and time) */
  49. int a= mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE));
  50. if (!stmt)
  51. {
  52.   fprintf(stderr, " mysql_prepare(), INSERT failed\n" );
  53.   fprintf(stderr, " %s\n", mysql_error(mysql));
  54.   exit(0);
  55. }
  56. fprintf(stdout, " prepare, INSERT successful\n" );
  57. /* Get the parameter count from the statement */
  58. param_count=mysql_stmt_param_count(stmt);
  59. fprintf(stdout, " total parameters in INSERT: %d\n", param_count);
  60. if (param_count != 3) /* validate parameter count */
  61. {
  62.   fprintf(stderr, " invalid parameter count returned by MySQL\n" );
  63.   exit(0);
  64. }
  65. /* Bind the data for all 3 parameters */
  66. /* INTEGER PARAM */
  67. /* This is a number type, so there is no need to specify buffer_length */
  68. bind[0].buffer_type= MYSQL_TYPE_LONG;
  69. bind[0].buffer= (char *)&int_data;
  70. bind[0].is_null= 0;
  71. bind[0].length= 0;
  72. /* STRING PARAM */
  73. bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
  74. bind[1].buffer= (char *)str_data;
  75. bind[1].buffer_length= STRING_SIZE;
  76. bind[1].is_null= 0;
  77. bind[1].length= &str_length;
  78. /* SMALLINT PARAM */
  79. bind[2].buffer_type= MYSQL_TYPE_SHORT;
  80. bind[2].buffer= (char *)&small_data;
  81. bind[2].is_null= &is_null;
  82. bind[2].length= 0;
  83. /* Bind the buffers */
  84. if (mysql_stmt_bind_param(stmt, bind))
  85. {
  86.   fprintf(stderr, " mysql_bind_param() failed\n" );
  87.   fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  88.   exit(0);
  89. }
  90. /* Specify the data values for the first row */
  91. int_data= 10;             /* integer */
  92. strncpy(str_data, "MySQL", STRING_SIZE); /* string  */
  93. str_length= strlen(str_data);
  94. /* INSERT SMALLINT data as NULL */
  95. is_null= 1;
  96. /* Execute the INSERT statement - 1*/
  97. if (mysql_stmt_execute(stmt))
  98. {
  99.   fprintf(stderr, " mysql_execute(), 1 failed\n" );
  100.   fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  101.   exit(0);
  102. }
  103. /* Get the total number of affected rows */
  104. affected_rows= mysql_stmt_affected_rows(stmt);
  105. fprintf(stdout, " total affected rows(insert 1): %ld\n", affected_rows);
  106. if (affected_rows != 1) /* validate affected rows */
  107. {
  108.   fprintf(stderr, " invalid affected rows by MySQL\n" );
  109.   exit(0);
  110. }
  111. /* Specify data values for second row, then re-execute the statement */
  112. int_data= 1000;
  113. strncpy(str_data, "The most popular open source database", STRING_SIZE);
  114. str_length= strlen(str_data);
  115. small_data= 1000;         /* smallint */
  116. is_null= 0;               /* reset */
  117. /* Execute the INSERT statement - 2*/
  118. if (mysql_stmt_execute(stmt))
  119. {
  120.   fprintf(stderr, " mysql_execute, 2 failed\n" );
  121.   fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  122.   exit(0);
  123. }
  124. /* Get the total rows affected */
  125. affected_rows= mysql_stmt_affected_rows(stmt);
  126. fprintf(stdout, " total affected rows(insert 2): %ld\n", affected_rows);
  127. if (affected_rows != 1) /* validate affected rows */
  128. {
  129.   fprintf(stderr, " invalid affected rows by MySQL\n" );
  130.   exit(0);
  131. }
  132. /* Close the statement */
  133. if (mysql_stmt_close(stmt))
  134. {
  135.   fprintf(stderr, " failed while closing the statement\n" );
  136.   fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
  137.   exit(0);
  138. }
  139. }

Reply

Marsh Posté le 25-05-2009 à 07:43:10    

Problème de version de mysql? C'est quoi l'erreur exacte? A la compilation ou  l'exécution?


Message édité par skeye le 25-05-2009 à 07:44:06

---------------
Can't buy what I want because it's free -
Reply

Marsh Posté le 25-05-2009 à 08:55:55    

A LA COMPILATION
undefined reference to mysql_stmt_init@4
undefined reference to mysql_stmt_prepared@12
undefined reference to mysql_stmt_param_count@4
undefined reference to mysql_stmt_bind_param@8
undefined reference to mysql_stmt_error@4
undefined reference to mysql_stmt_execute@4
undefined reference to mysql_stmt_affexted_rows@4
 
voila les seuls erreurs que j'ai a la compilation

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed