mercredi 2 janvier 2019

How fix strange syntax problem when using execute function?

I met an error when trying to use a function called execute to use one database. The information shows me that's about syntax, but I don't think so. The following is my critical code and important information about the error.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'u' at line 1

#include <stdlib.h>
#include <iostream>

/*
  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<string>

#include "mysql_connection.h"

#include <mysql_driver.h>

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
using namespace sql;

int main()
{

    try {
  sql::mysql::MySQL_Driver driver;
  cout<<" driver "<<endl;
  sql::Connection *con = NULL;
  cout<<" con"<<endl;
  sql::Statement *stmt;
  cout<<" stmt"<<endl;
  sql::ResultSet *res;
  cout<<" res"<<endl;
  /* Create a connection */
  //driver = sql::mysql::MySQL_Driver::MySQL_Driver();

  SQLString ip_port("localhost:3306");
  cout<<"some debug string"<<endl;
  SQLString user("debian-sys-maint");
  SQLString password("fTlykRye1LwttC8f");
  con = driver.connect(ip_port, user, password);
  assert(con!=NULL);
  cout<<" root"<<endl;
  /* Connect to the MySQL test database */

  SQLString schema("account");

  //con->setSchema(schema); 
  cout<<" table"<<endl;
  stmt = con->createStatement();
  cout<<" table*2"<<endl;
  assert(stmt!=NULL);
  SQLString db("use account");
  stmt->execute(db);
  res = stmt->executeQuery("SELECT * from user");
  while (res->next()) {
    cout << "\t... MySQL replies: ";
    /* Access column data by alias or column name */
    cout << res->getString("name") << endl;
    cout << "\t... MySQL says it again: ";
    /* Access column data by numeric offset, 1 is the first column */
    cout << res->getString("password") << endl;
  }
  delete res;
  delete stmt;
  delete con;

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

}

cout << endl;

return EXIT_SUCCESS;
}

I was confused that why the information was not about the sentence "use account". Instead, it was about "u". Thanks very much.

Aucun commentaire:

Enregistrer un commentaire