Hey guys, i am having a problem in my query statement. I am using Mysql in Netbeans and c++. Really Hope for some help. I posted it here as i read up a bit on it and it seems the solution is based on c++.


What i am trying to do is for the user to enter a certain value and then the program will store the value into the database...

string NewMovie ;

Cout <<" Enter your new movie  : " << endl ; 
 cin << NewMovie ;

Basically, the person will enter a new movie(in this case Avatar) and the input will be saved to a string called NewMovie.

Here is when my problem starts...How do i input this variable into an insert sql statement. I checked my statement and i tried hardcoding my data like this...

mysql_query(conn,"INSERT INTO movie_info (movie_title , values ('Avatar')"   );

The database will be updated.


But if im using a variable called NewMovie, i cant just insert it into the statement as it will not read the value of NewMovie. S

What can i do to solve this problem so the sql statement can read the variable and then save it into the database.

I read up Cstring and ostreamstring but i do not get how they are used. Could somwbody show me how they are being used based on the example i gave, or if not, show me another way on how this problem can be solved

Have you tried using string concatenation to build the query before you send it?

string NewMovie, queryText;
cout << "Enter the title of a movie: ";
getline(cin, NewMovie);
/*use getline() instead of cin >> because some movies have more than 1 word in name
cin >> breaks input stream @ a whitespace */
queryText = "INSERT INTO movie_info VALUES('" + NewMovie + "')";
mysql_query(conn, queryText);
MYSQL *conn;
      int main()

      {





          string NewMovie = "James";
          string NewTitle = "Pikachi";
          string queryText ;


          queryText = "INSERT INTO movie_info (movie_title , movie_actor) VALUES('" + NewMovie + "','" + NewTitle +"')" ;







MYSQL_RES *res_set;
    MYSQL_ROW row;

   conn = mysql_init(NULL);
    if( conn == NULL )
    {
        printf("Failed to initate MySQL\n");
        return 1;
    }

  
    if( ! mysql_real_connect(conn,host,username,password,database,0,NULL,0) )
    {
        printf( "Error connecting to database: %s\n", mysql_error(conn));
        return 1;
    }

    unsigned int i;

  
  //Finally, we query the database with the following query using the mysql_query() function: "SELECT * FROM users" like so:
  mysql_query(conn,queryText);


    res_set = mysql_store_result(conn);

    unsigned int numrows = mysql_num_rows(res_set);
    unsigned int num_fields = mysql_num_fields(res_set);


    //Finally, we retrive the result using the function mysql_fetch_row() and then print out all of the data in large chunks.
   // This is done like so:
    while ((row = mysql_fetch_row(res_set)) != NULL)
    {

        for(i = 0; i < num_fields; i++)
        {
             printf("%s\t", row[i] ? row[i] : "NULL");
        }
        printf("\n");

    }

    //Last but certainly not least, we close our connection to the database
    mysql_close(conn);
    return 0;

      }

This was the coding i used.....

but when i tried to compile there was error statement saying....


newmain.cpp:98: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘2’ to ‘int mysql_query(MYSQL*, const char*)'

and it was pointing to this line...

mysql_query(conn,queryText);

what is the problem?

Hmmm..... I don't have any background with MySQL from C++. I can do it pretty well from PHP though. I was only applying a couple things I know from that before. But now I'm stuck...

Unfortunately, I know that std::string and char * (character arrays/c-style strings) are not compatible types and I'm not all that great with C-style strings yet. Have a look at this page for another way to build your query string. You'll most likely want strcpy() and strcat(). Hopefully that will work for you.

I doubt you can do something with an explicit cast without an overload of some sort. I'm not too sure I can be much more help. Maybe someone else can shed some light on this situation.

error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘2’ to ‘int mysql_query(MYSQL*, const char*)'

That says that you cannot pass a std::string to the mysql_query(). Since it expects a const char * , you need to use the .c_str() method, so simply change to;

mysql_query(conn, queryText.c_str());

error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘2’ to ‘int mysql_query(MYSQL*, const char*)'

That says that you cannot pass a std::string to the mysql_query(). Since it expects a const char * , you need to use the .c_str() method, so simply change to;

mysql_query(conn, queryText.c_str());

I knew about that method, but I never realized that was how it worked, interesting....

*scribbles down note*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.