mydb.cpp

来自「基于linux下的故障录波前置程序」· C++ 代码 · 共 307 行

CPP
307
字号
#include"MyDb.h"#include"Global.h"MYSQL mysql;extern MYSQL *mysql_init(MYSQL *mysql);YcChannelCfg m_YcChannelCfg[YCCHANNEL];YcChannelCfg m_NsYcChannelCfg[YCCHANNEL];YxChannelCfg m_NsYxChannelCfg[YXCHANNEL];int ycchannelnum,nsycchannelnum,nsyxchannelnum;static int se = 0;int GetYcNum(){	return ycchannelnum;} int GetNsYcNum(){	return nsycchannelnum;}int GetNsYxNum(){	return nsyxchannelnum;}bool InitMysqlDB(){	int id;	MYSQL_ROW row;	MYSQL_RES * res;	int cpuid;		for(int i=0;i<YCCHANNEL;i++)	{		m_YcChannelCfg[i].id = -1;		m_YcChannelCfg[i].name = "";		m_YcChannelCfg[i].unit ="";		m_YcChannelCfg[i].ratio = 0;		m_YcChannelCfg[i].type = -1;		m_YcChannelCfg[i].max = 0;		m_YcChannelCfg[i].controlword = -1;		m_NsYcChannelCfg[i].id = -1;		m_NsYcChannelCfg[i].name = "";		m_NsYcChannelCfg[i].unit ="";		m_NsYcChannelCfg[i].ratio = 0;		m_NsYcChannelCfg[i].type = -1;		m_NsYcChannelCfg[i].max = 0;		m_NsYcChannelCfg[i].controlword = -1;		for(int m=0;m<8;m++)		{			m_YcChannelCfg[i].localvalue[m] = 0;			m_NsYcChannelCfg[i].localvalue[m] = 0;		}	}	for(int i=0;i<YXCHANNEL;i++)	{			m_NsYxChannelCfg[i].id = -1;		m_NsYxChannelCfg[i].name = " ";		m_NsYxChannelCfg[i].type = -1;		m_NsYxChannelCfg[i].high = 0;		m_NsYxChannelCfg[i].low = 0;		}		if(mysql_init(&mysql)== NULL)	{#ifdef _DEBUG		printf("mysql init failed!\n");#endif		return false;	}		//为什么第一个值又没有了??????	if(mysql_real_connect(&mysql,"192.168.5.233","sac","111111","lubo",0,NULL,0)== NULL)	{#ifdef _DEBUG		printf("mysql connect failed !\n");#endif		return false;	}		char sqlbuf[255];	sprintf(sqlbuf,"select * from YCDescription where UnitType <> %d",ZHILIUTYPE);          	if(mysql_real_query(&mysql,sqlbuf,strlen(sqlbuf))!= 0)	{#ifdef _DEBUG		printf("mysql query failed YCDescription!\n");                printf("%s\n",mysql_error(&mysql));#endif		return false;	}		if((res=mysql_store_result(&mysql))==NULL)	{#ifdef _DEBUG		printf("result NULL!\n");#endif		return false;	}	ycchannelnum =  mysql_num_rows(res) - 1;	if(ycchannelnum == 1)	{#ifdef _DEBUG		printf("YC DABABASE NO SACDATA!\n");#endif	//	return false;	}#ifdef _DEBUG	printf("SACYC CHANNEL NUM  = %d\n",ycchannelnum);#endif	while(row = mysql_fetch_row(res))	{				id = atoi(row[0]);		if(id == 1)			continue;		m_YcChannelCfg[se].id = id-1;		m_YcChannelCfg[se].name = row[1];		m_YcChannelCfg[se].ratio = atof(row[2]);		m_YcChannelCfg[se].max = atof(row[3]);		m_YcChannelCfg[se].unit = row[7];		m_YcChannelCfg[se].type = atoi(row[9]);		m_YcChannelCfg[se].localvalue[0] = atof(row[10]);		m_YcChannelCfg[se].localvalue[1] = atof(row[11]);		m_YcChannelCfg[se].localvalue[2] = atof(row[12]);		m_YcChannelCfg[se].localvalue[3] = atof(row[13]);		m_YcChannelCfg[se].localvalue[4] = atof(row[14]);		m_YcChannelCfg[se].localvalue[5] = atof(row[15]);		m_YcChannelCfg[se].localvalue[6] = atof(row[16]);		m_YcChannelCfg[se].localvalue[7] = atof(row[17]);		m_YcChannelCfg[se].controlword = atoi(row[18]);		#ifdef _DEBUG		printf("Channel%d ID = %d,Name = %s,Ratio = %f, Units = %s  MAX = %f type = %d!\n",id,m_YcChannelCfg[se].id,m_YcChannelCfg[se].name,m_YcChannelCfg[se].ratio,m_YcChannelCfg[se].unit,m_YcChannelCfg[se].max,m_YcChannelCfg[se].type);		for(int i = 0;i<8;i++)			printf(" localvalue%d = %f",i+1,m_YcChannelCfg[se].localvalue[i]);		printf("\n");		printf("Channel%d ControlWord is %d\n",id,m_YcChannelCfg[se].controlword);		#endif		se++;	}	//	mysql_free_result(res);	char nssqlbuf[255] ;	sprintf(nssqlbuf,"select * from YCDescription where UnitType = %d",ZHILIUTYPE);	if(mysql_real_query(&mysql,nssqlbuf,strlen(nssqlbuf))!= 0)	{#ifdef _DEBUG		printf("mysql query failed YCDescription!\n");#endif		return false;	}		if((res=mysql_store_result(&mysql))==NULL)	{#ifdef _DEBUG		printf("result NULL!\n");#endif		return false;	}	nsycchannelnum =  mysql_num_rows(res);#ifdef _DEBUG	printf("NZYC CHANNEL NUM  = %d\n",nsycchannelnum);#endif	if(nsycchannelnum == 0)	{#ifdef _DEBUG		printf("YC DABABASE NO NZDATA!\n");#endif		//return false;	}	se = 0;	while(row = mysql_fetch_row(res))	{				id = atoi(row[0]);		m_NsYcChannelCfg[se].id = id;		m_NsYcChannelCfg[se].name = row[1];		m_NsYcChannelCfg[se].ratio = atof(row[2]);		m_NsYcChannelCfg[se].max = atof(row[3]);		m_NsYcChannelCfg[se].unit = row[7];		m_NsYcChannelCfg[se].type = atoi(row[9]);		m_NsYcChannelCfg[se].localvalue[0] = atof(row[10]);		m_NsYcChannelCfg[se].localvalue[1] = atof(row[11]);		m_NsYcChannelCfg[se].localvalue[2] = atof(row[12]);		m_NsYcChannelCfg[se].localvalue[3] = atof(row[13]);		m_NsYcChannelCfg[se].localvalue[4] = atof(row[14]);		m_NsYcChannelCfg[se].localvalue[5] = atof(row[15]);		m_NsYcChannelCfg[se].localvalue[6] = atof(row[16]);		m_NsYcChannelCfg[se].localvalue[7] = atof(row[17]);		m_NsYcChannelCfg[se].controlword = atoi(row[18]);		#ifdef _DEBUG                             		printf("Channel%d ID = %d,Name = %s,Ratio = %f, Units = %s  MAX = %f type = %d!\n",id,m_NsYcChannelCfg[se].id,m_NsYcChannelCfg[se].name,m_NsYcChannelCfg[se].ratio,m_NsYcChannelCfg[se].unit,m_NsYcChannelCfg[se].max,m_NsYcChannelCfg[se].type);		for(int i = 0;i<8;i++)			printf(" localvalue%d = %f",i+1,m_NsYcChannelCfg[se].localvalue[i]);		printf("\n");		printf("Channel%d ControlWord is %d\n",id,m_NsYcChannelCfg[se].controlword);#endif		se++;	}	//mysql_free_result(res);         //////////////////////////////////////////////////////////////////////////////			char nssqlbufyx[255] ;	sprintf(nssqlbufyx,"select * from YXDescription where YXType = %d",ZHILIUTYPE);		if(mysql_real_query(&mysql,nssqlbufyx,strlen(nssqlbufyx))!= 0)	{#ifdef _DEBUG		printf("mysql query failed YXDescription!\n");#endif		return false;	}		if((res=mysql_store_result(&mysql))==NULL)	{#ifdef _DEBUG		printf("result NULL!\n");#endif		return false;	}		nsyxchannelnum =  mysql_num_rows(res);		if(nsyxchannelnum == 0)	{#ifdef _DEBUG		printf("YX DABABASE NO DATA!\n");#endif	//	return false;	}		se = 0;	while(row = mysql_fetch_row(res))	{				id = atoi(row[0]);		m_NsYxChannelCfg[se].id = atoi(row[0])-1;		m_NsYxChannelCfg[se].name = row[1];		m_NsYxChannelCfg[se].high = atoi(row[7]);		m_NsYxChannelCfg[se].low = atoi(row[6]);		m_NsYxChannelCfg[se].type = atoi(row[5]);		#ifdef _DEBUG		printf("ID = %d,Name = %s  HIGH = %d  LOW = %d TYPE = %d!\n",m_NsYxChannelCfg[se].id,m_NsYxChannelCfg[se].name,m_NsYxChannelCfg[se].high,m_NsYxChannelCfg[se].low,m_NsYxChannelCfg[se].type);#endif		se++;			}		mysql_free_result(res); 	mysql_close(&mysql);		return true;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?