error: ‘sql’ does not name a type
sql::Connection connect_mysql();
Is there a way i can return the connection object from this function?
I know to return the connection object in C but not happening in C++.
edited version: return type of connect_mysql() has been changed to sql::Connection * . earlier it was sql::Connection.
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include<mysql/mysql.h>
#include<cppconn/driver.h>
#include<cppconn/exception.h>
#include<cppconn/resultset.h>
#include<cppconn/statement.h>
sql::Connection * connect_mysql();
int main()
{
sql::Connection *con;
con = connect_mysql();
return 0;
}
sql::Connection * connect_mysql()
{
cout << "CONNECTING DATABASE..............."<<endl;
try{
cout<<"inside try while connecting to mysql"<<endl;
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = get_driver_instance();
con = driver->connect("localhost","root","Aricent@123");
con->setSchema( "COE" );
stmt = con->createStatement();
res = stmt->executeQuery( "show tables" );
while( res->next() )
{
cout<<"MYSQL replies:"<<endl;
cout<<res->getInt(1);
cout<<res->getString(1)<<endl;
}
}
catch( exception e )
{
cout << "# ERR: SQLException in " << __FILE__;
cout << "# ERR: ";
cout << " (MySQL error code: ";
cout << ", SQLState: ";
}
return con;
}
That's not an object, it's a pointer. Try this
sql::Connection* connect_mysql()
{
...
}
Another error is the lack of prototype for connect_mysql
. You should have something like this
sql::Connection* connect_mysql(); // prototype
int main()
{
sql::Connection *con;
con = connect_mysql();
...
But what is confusing me about your error message is that it refers to the line sql::Connection* connect_mysql()
but apparently using the same type in the line sql::Connection *con;
is OK. If you have any other error messages or warnings please post those as well.
It shouldn't need stating that for help with compiler error messages you should post the exact code, the exact error messages, and all of both of those.
I'm not an expert on mysql but I wonder if you need a header file #include <cppconn/connection.h>