gpt4 book ai didi

c++ - 错误: 'driver' was not declared in this scope

转载 作者:太空宇宙 更新时间:2023-11-04 05:45:18 25 4
gpt4 key购买 nike

遵循 MySQL C++ 连接器上 MySQL 站点的语法,但没有得到好的结果,这里是一个片段:

#include "/usr/include/mysql++/mysql++.h"
#include "/usr/include/mysql/mysql.h"
#include "/usr/include/mysql/mysql_version.h"
#include "/usr/include/mysql_connection.h"
#include "/usr/include/mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>


using namespace std;
using namespace sql::mysql;
using namespace sql;


sql::mysql::MySQL_Driver *driver;
driver = sql::mysql::get_mysql_driver_instance();
sql::Connection *con;
con = driver->connect("tcp://127.0.0.1:3306", "db", "password");
sql::PreparedStatement *prep_stmt;
prep_stmt = con->prepareStatement("INSERT INTO db(srcIP, srcCountry, destIP, destHost, destPort, blIP) VALUES (?, ?, ?, ?, ?, ?)");

prep_stmt->setString(1, src_ip);
prep_stmt->setString(2, country_code);
prep_stmt->setString(3, dest_ip);
prep_stmt->setString(4, host_name);
prep_stmt->setString(5, dest_prt);
prep_stmt->setString(6, blip);
prep_stmt->execute();

delete prep_stmt;
delete con;

}
else

sql::mysql::MySQL_Driver *driver;
driver = sql::mysql::get_mysql_driver_instance();
sql::Connection *con;
con = driver->connect("tcp://127.0.0.1:3306", "db", "password");
sql::PreparedStatement *prep_stmt;
prep_stmt = con->prepareStatement("INSERT INTO db(srcIP, srcCountry, destIP, destHost, destPort, blIP) VALUES (?, ?, ?, ?, ?, ?)");

prep_stmt->setString(1, src_ip);
prep_stmt->setString(2, country_code);
prep_stmt->setString(3, dest_ip);
prep_stmt->setString(4, he->h_name);
prep_stmt->setString(5, dest_prt);
prep_stmt->setString(6, blip);
prep_stmt->execute();

delete prep_stmt;
delete con;

编译:

g++ sql.c -o sql -I/usr/include -I/usr/include/mysql -I/usr/include/mysql++ -I/usr/local/include -I/usr/local/include/cppconn -lmysqlcppconn -lmysql++ -lstdc++ -lmysqlpp -lmysqlclient

并且总是在第二组 MySQL 驱动程序上出现错误:

错误:“驱动程序”未在此范围内声明

最佳答案

仅根据我所看到的,您似乎在希望成为 else 情况一部分的语句周围缺少大括号。如果您这样做:

if (x)
{
// do something
}
else
Foo* foo = new foo();
foo->someFunction();

您将在读取 foo->someFunction(); 的行上收到错误,因为 else 只包含一个不带大括号的语句,并且 foo 之后就超出了范围。您需要这样做:

if (x)
{
// do something
}
else
{
Foo* foo = new foo();
foo->someFunction();
}

关于c++ - 错误: 'driver' was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12752280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com