关键部分代码:备忘
//=========================================================================
//函数名:produce_txt
//功 能:产生.txt文件
//参 数:FILEINFO &fileInfo, string filename, MYSQL *mysql, string boardnum
//返回值:成功返回1
//==========================================================================
int produce_txt(FILEINFO &fileInfo, string filename, MYSQL *mysql, string boardnum)
{
MYSQL_ROW row = NULL;
MYSQL_RES *result = NULL;
//string sz_buf;
//string sz_errordb = "ERROR_DB";
char szFileFullPath[512] = {0};
char sztxt[1024*2] = {0};
string szvalue;
FILE *fp;
//新建一个txt文件
sprintf_s(szFileFullPath,sizeof(szFileFullPath),"%s\\%s",g_Downfilepath,filename.c_str());
fopen_s(&fp, szFileFullPath,"wb");
if (fp == NULL)
{
return 0;
}
//查询2D扫描数据库
sprintf_s(sztxt, sizeof(sztxt), "SELECT FIELDVALUE FROM SCAN2D_TBL WHERE BOARDNO='%s'\
ORDER BY FIELDNAME", boardnum.c_str());
if(0 != mysql_real_query(mysql,sztxt, strlen(sztxt)))
{
//有问题
fileInfo.errorstr = "查询失败!sql:";
fileInfo.errorstr += sztxt;
return 0;
}
result = mysql_store_result(mysql);
if(NULL != result)
{
while((row = mysql_fetch_row(result)))
{
if(NULL != row)
{
szvalue = row[0];
szvalue += "\r\n";
fputs(szvalue.c_str(), fp);
}
else
{
fileInfo.errorstr = "查询无记录!sql:";
//fileInfo.errorstr += sz;
return 0;
}
}
mysql_free_result(result);
}
fclose(fp);
return 1;
}
使用完数据库 一定记得释放~~~~~
mysql_close(&mysql);