c++ - 在 C++ 中查询 MySQL 数据库不返回所有值
问题描述
这对我自己来说是一个新话题。我正在学习通过 C++ 连接到 SQL 数据库。现在我有一个用于连接到本地 SQL 服务器的表。服务器上有一个包含基本垃圾数据的模式,该数据在学期开始时用于实验室(我一直在使用这些数据来找出不同的 SQL 语法)
在这种情况下,使用的服务器是 MySql Workbench。
我们获得了一些关于设置 Visual Studio 以与此 sql 数据库通信和查询数据的基本说明。虽然它在某种程度上有效,但它似乎拒绝返回完整的数据表。在 SQL 中,如果我想查询一个名为 的表offices
,officeCode
我想查看的是 '1',我只需键入SELECT * FROM offices WHERE officeCode = 1;
Visual Studio 上的相同查询似乎只返回除第一行之外的所有内容。即使我扩大它,使其不使用“where”约束,它仍然拒绝将第一个条目返回到表中。我不确定它是否是我设置代码的方式,或者它没有正确连接到我的数据库,还有其他人发现我没有的东西吗?
#include <mysql.h>
#include <iostream>
#define CRT_SECURE_NO_WARNINGS
using namespace std;
int main() {
MYSQL* conn;
MYSQL_ROW row;
MYSQL_RES* res;
conn = mysql_init(0);
conn = mysql_real_connect(conn, "127.0.0.1", "<ommittedUser>", "<omittedPass>", "new_schema", 3306, nullptr, 0);
if (conn) {
cout << "successful connection to database" << endl;
}
else {
cout << "Connection failed!" << mysql_error(conn) << endl;
}
string query = "SELECT * FROM offices WHERE officeCode = 1 ;"; //doesnt detect the first row
const char* q = query.c_str();
int exequery;
exequery = mysql_query(conn, q);
if (!exequery) {
cout << "The query executed successfully with no error." << endl;
res = mysql_store_result(conn);
if (mysql_fetch_row(res) == nullptr) {
cout << "result is empty" << endl;
}
else {
while (row = mysql_fetch_row(res)) {
printf("OfficeCode: %s, city: %s\n", row[0], row[1]);
}
}
}
else {
cout << "Error message: " << mysql_error(conn) <<":" <<mysql_errno(conn) << endl;
}
delete conn;
}
解决方案
推荐阅读
- square-connect - 方形结帐重复用户
- python - 无法在我的 QuerySelectField 中显示不同的值 - Flask
- python - 在 Python 中计算 t 检验的功效
- css - 模糊矩形 div 边缘?
- django - 如何安全地将对象包装到 TypeScript 中的数组中?
- tensorflow - 检查目标时出错:预期的暗淡错误
- imagemagick - 如何让 jekyll-assets 与 ImageMagick 一起使用?
- r - 删除与特定字符不同的所有字符(即保留选择)
- scala - 在 http4s 版本 19.0.0 中找不到 ContextShift[cats.effect.IO] 的隐式值
- scala - 将参数限制为 Scala 中定义的类型