修改
后续问题:mysql resultset is always null
-
resultset
似乎是空的,我确信该表存在,没有SQLException
抛出。
它应该打印计数,但它没有。
CDatabase标头
class CDatabase
{
bool verify(string, string);
public:
sql::Connection *con;
CDatabase();
~CDatabase();
};
的CDatabase ::的CDatabase
con = driver->connect("", "", "");
con->setSchema("");
的CDatabase ::验证
bool CDatabase::verify(string channel, string hyper_key)
{
if (!con) return false;
try
{
sql::Statement *stmt;
sql::ResultSet *res;
stmt = con->createStatement();
stmt->execute("SELECT COUNT(*) FROM dark_souls2_widgets");
res = stmt->getResultSet();
cout << res->rowsCount();
delete stmt;
delete res;
}
catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line »" << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
return false;
}
return false;
}
答案 0 :(得分:2)
它不应显示计数,它应显示1
,因为此查询将失败或结果只有一行。如果您想知道表dark_souls2_widgets
中存在多少行,那么您可以使用
res = stmt->getResultSet();
while (res->next()) {
cout << res->getInt(1); // getInt(1) returns the first column
}
答案 1 :(得分:1)
我暂时没有使用带有c ++的mysql,但我会给出一个镜头......这是dev.mysql的一个例子
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
首先我注意到你使用的是execute()而不是executeQuery()。最重要的是尽管rowsCount()如果成功则总是为1行,因为查询只返回1行,其中包含查询结果。试试这个..
stmt->executeQuery("SELECT COUNT(*) FROM dark_souls2_widgets");
res = stmt->getResultSet();
while (res->next()) {
cout << res->getInt(1);
}
甚至这应该可行,因为它会选择每一行并输出返回的行数
stmt->executeQuery("SELECT * FROM dark_souls2_widgets");
cout << res->rowsCount();
编辑 - 试试这个
stmt = con->creteStatement()
res = stmt->executeQuery("SELECT COUNT(*) FROM dark_souls2_widgets");
while (res->next()) {
cout << res->getInt(1);
}