我对 C++ 很陌生,所以我不确定。
该应用程序的基本目标是允许用户输入 SQL 查询,然后将结果行从数据库返回到文本 (.txt) 文件。
这是之前的部分和 int Main() 的变量
#include <iostream>
#include <string>
#include <windows.h>
#include <mysql.h>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
MYSQL* conn;
MYSQL_ROW row;
MYSQL_RES *res;
unsigned int num_rows;
unsigned int ii;
int qstate;
这是导致程序崩溃的代码段,但我就是想不通是什么导致它崩溃
if(!qstate)
{
res = mysql_store_result(conn);
cout<<"Enter File Name: "<<endl;
std:string FileName;
std::getline(cin, FileName);
string FN = ""+FileName+".txt";
const char* fileN = FN.c_str();
ofstream theSaveFile(fileN, ios::app);
num_rows = mysql_num_rows(res);
row = mysql_fetch_row(res);
for(ii = 0; ii < num_rows; ii++)
{
if(!theSaveFile)
{
cout<<"Printing to the file failed"
<<endl;
exit(1);
}
theSaveFile<<""+row+" \t\t";
}
最佳答案
row = mysql_fetch_row(res);
到现在为止一切看起来都很好,但是你将 row
视为一个字符串:
theSaveFile<<""+row+" \t\t";
这是错误的。您尾随的 +"\t\t"
和您奇怪的 ""+
(不要这样做;C++ 不是 JavaScript)可能正在执行越界指针算术。如果您正确地完成了流式传输:
theSaveFile << row << " \t\t";
然后我想你的编译器会更好地警告你 row
不能这样处理。
There's an example in the manual of how to use MYSQL_ROW
.提示:这是一个数组;结果集中的每个列(或“字段”)一个元素。
关于c++ - 任何人都知道我如何修复此 C++ 以尝试将从 MySQL 数据库返回的行打印到文本文件 (.txt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42979749/