我使用MySQL连接器c++ api和我做的,我的程序在我执行它之后连接到数据库。
但现在我必须对其他cpp文件进行查询。
我是应该打开一个新连接,并在每次执行查询时关闭它,还是应该保留这个连接?
如果保持一个连接更好,那么如何才能在其他cpp文件上获得当前连接?
main on 1.cpp
int AuthServerMain(int argc, _TCHAR* argv[])
{
sql::mysql::MySQL_Driver *driver;
sql::Connection * con;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://ip:3306", "root", "password");
con->setSchema("dbo");
}现在我的问题是“骗局”
如果我写在2.cpp上
sql::Statement * stmt;
sql::ResultSet * resset;
stmt = con->createStatement();然后"con“是红色下划线,因为他找不到它。我该怎么做他才能找到“骗子”?
发布于 2014-06-15 22:57:08
为什么不创建一个database类,其中包含了连接变量。然后,您可以在需要它的.cpp文件周围传递这个类。您可以创建像database::excetute(std::string s)这样的函数,甚至可以重载<< operator,因此其他.cpp文件中的代码是干净的。
https://stackoverflow.com/questions/24234836
复制相似问题