Start New Discussion within our Software Development Community
#include <stdlib.h>
#include <iostream>
#include <cstring>
#include <string>

/*
  Include directly the different
  headers from cppconn/ and mysql_driver.h + mysql_util.h
  (and mysql_connection.h). This will reduce your build time!
*/
#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)
{

  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;

  /* Create a connection */
  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "dddd");
  con->setSchema("Filter");
  

  stmt = con->createStatement();
  string No = "111";
  res = stmt->executeQuery("SELECT * FROM Tasks WHERE Notes = '"No.c_str()"' ");


  while (res->next())
  {

      cout << "Notes " << res->getInt(1) << " " ;
      cout << "ID " << res->getString("TaskID") << endl;



  }
  
  delete res;
  delete stmt;
  delete con;


return EXIT_SUCCESS;
}

Hey guys , the problem which i am facing is that i would like to use a variable in order to base my SELECT statement upon. I am not sure how to go about getting it to be accepted by the SQL syntax. Hope for some help here. I tried using c_str() , but it does not seem to be working. How can i fix this in order for my SELECT statement to search based on my variable.

Slight change, but you were close:

string sSQL = "SELECT * FROM Tasks WHERE Notes = '" + No + "'";
res = stmt->executeQuery(sSQL);

Of course, this assumes that "Notes" in your database is a character column. If it is a numeric column, then leave the single-ticks out of the SQL string.

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