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 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);
}

 

你可能感兴趣的:(C++,mysql)