VC++ access MySQL query problem
Hi guys,
Could you help me out with
------------
string str1
res=stmt->executeQuery("select * from test2 where english='str1'");
------------
anyone can tell me where is wrong with this query code?below is the error msg I got:

1>.\temp6.cpp(48) : error C2146: syntax error : missing ')' before identifier 'str1'
1>.\temp6.cpp(48) : error C2059: syntax error : ')'

I don't see anything wrong with it -- but did you post exactly the same thing that is in your program? Maybe you need to post more of the program.

I don't see anything wrong with it -- but did you post exactly the same thing that is in your program? Maybe you need to post more of the program.

#include <stdafx.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>


using namespace std;

int main(void)

{
        string (str1);
try
{
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;


  /* Create a connection */
  driver = get_driver_instance();
  con = driver->connect("localhost", "root", "qwerty");
  /* Connect to the MySQL menagerie database */
  con->setSchema("menagerie");

        stmt = con->createStatement();
        stmt->execute("set names \'GBK\'");


        cout << "Please enter an english word: ";
        cin >> str1;

        res=stmt->executeQuery("select * from test2 where english="str1"");----------提示错误就是说这句!但是我不知道是怎么错了?
        while (res->next())
{
    cout << res<<endl;

        cout <<setiosflags(ios::left)<<setw(9)<<res->getString(1)<<"     ";//1 is english
        cout <<setiosflags(ios::left)<<setw(9)<<res->getString(2);//2 is chinese

  }
  delete res;
  delete stmt;
  delete con;
}
catch (sql::SQLException &e)
{
  cout << "# ERR: SQLException in " << __FILE__;
  cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
  cout << "# ERR: " << e.what();
  cout << " (MySQL error code: " << e.getErrorCode();
  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}

cout << endl;

return EXIT_SUCCESS;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

>>res=stmt->executeQuery("select * from test2 where english="str1"");

That has two problems:
1) "str1": you have to escape the double quotes. \"str1\"

2) The string has to be properly formatted before sending it to executeQuery() function. C++ does not automatically replace "str1" in the string with whatever word you entered.

std::string command = "select * from test2 where english=\"" + str1 + "\"";
res=stmt->executeQuery(command.c_str());

Edited 7 Years Ago by Ancient Dragon: n/a

>>res=stmt->executeQuery("select * from test2 where english="str1"");

That has two problems:
1) "str1": you have to escape the double quotes. \"str1\"

2) The string has to be properly formatted before sending it to executeQuery() function. C++ does not automatically replace "str1" in the string with whatever word you entered.

std::string command = "select * from test2 where english=\"" + str1 + "\"";
res=stmt->executeQuery(command.c_str());

It is working ! Thanks a lot Ancient Dragon!

This article has been dead for over six months. Start a new discussion instead.