libmysql方法

基本操作

void ReadFromDatabase(MYSQL* connection)
{
	int result;
	result = mysql_query(connection, "select ip,port,census,starts,timeout,domain,code from server group by ip;");

	if (result != 0)
	{
		std::cout << mysql_error(connection) << std::endl;
		return;
	}

	MYSQL_RES* mysql_res;
	MYSQL_ROW mysql_row;
	unsigned int cols;
	mysql_res = mysql_store_result(connection);
	cols = mysql_num_fields(mysql_res);

	if (mysql_res != NULL)
	{
		g_lock.lock();

		//store the consequence from mysql
		while ((mysql_row = mysql_fetch_row(mysql_res)))
		{
			g_IPList.push_back(std::string(mysql_row[0]));
			g_portQueue.push_back(atoi(mysql_row[1]));
			g_Counts.push_back(atoi(mysql_row[2]));
			g_timeoutCounts.push_back(atoi(mysql_row[4]));
			g_domainQueue.push_back(std::string(mysql_row[5]));
			g_codeQueue.push_back(std::string(mysql_row[6]));
		}
		g_lock.unlock();


		//free
		mysql_free_result(mysql_res);
	}
	//for (auto& i : g_IPList)
	//	std::cout << i << std::endl;
	//for (auto& i : g_Counts)
	//	std::cout << i << std::endl;
	//std::cout << g_IPList << std::endl;
	//std::cout << g_Counts << std::endl;

	//serial running
	//Worker(connection);

	//asynchronus running
	std::list<std::thread > threadList;
	for (int i = 0; i < g_threadCounts; i++) {
		//std::thread t(Worker, connection);
		threadList.push_back(std::thread(Worker, connection));
	}

	for (auto& i : threadList)
		i.join();

	dumplicateDataString.append(" else census end");
	resetDataString.append(" else starts end");
	timeoutDataString.append(" else timeout end");

	//std::cout << dumplicateDataString << std::endl;
	if (mysql_query(connection, dumplicateDataString.c_str()) != 0)
	{
		printf("mysql_query:%s", mysql_error(connection));
	}
	if (mysql_query(connection, resetDataString.c_str()) != 0)
	{
		printf("mysql_query:%s", mysql_error(connection));
	}
	if (mysql_query(connection, timeoutDataString.c_str()) != 0)
	{
		printf("mysql_query:%s", mysql_error(connection));
	}

	//close
	//mysql_close(connection);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值