![](/img/trans.png)
[英]How can I get the native C API connection structure from MySQL Connector/C++?
[英]get current connection from mysql connector c++ api
我使用的是mysql連接器c ++ api,我將程序執行后連接到數據庫。
但是現在我必須對其他cpp文件進行查詢。
我應該在每次查詢時打開一個新連接並關閉它,還是應該保留一個連接?
如果保持一個連接更好,那么如何在其他cpp文件上獲得當前連接呢?
主要在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();
然后“騙子”用紅色下划線標出,因為他找不到。 我該怎么辦,他才能找到“騙子”?
您為什么不制作一個database
類,其中的連接可變。 然后,您可以在需要的.cpp文件中傳遞此類。 您可以創建類似於database::excetute(std::string s)
的函數,甚至可以重載<< operator
,因此其他.cpp文件中的代碼是干凈的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.