-1
错误:'sql'未命名类型 sql :: Connection connect_mysql(); 有没有办法可以从这个函数返回连接对象? 我知道在C中返回连接对象,但不在C++中发生。
编辑版本:connect_mysql()的返回类型已更改为sql :: Connection *。早些时候它是sql :: Connection。是否有可能从C++代码中的mysql函数返回连接对象?
#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","[email protected]");
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;
}
没错,但错误的是更基本的:'错误:“SQL”没有指定类型的SQL ::连接connect_mysql ();'。 – juanchopanza
建议更改后,我收到此错误: 错误:'sql'不会命名一个类型 sql :: Connection * connect_mysql(); –
@ShreyasBhargav您的代码中没有使用任何头文件。那就是问题所在。 – john