intH_Utility::insertRecordsToMySQL(const char*dataFilePath,const char*host,const char*user,const char*password,const char*schema,const char*table,const intport,const char*logFilePath)
{
ifstream rpf;
rpf.open(dataFilePath);int lineCount = 0;if(rpf.is_open())
{
MYSQL mysql;
mysql_init(&mysql);if(!mysql_real_connect(&mysql, host, user, password, schema, port, NULL, 0))
{
printf("MySQL数据库连接失败。\n");return -1;
}
ofstream f1(logFilePath);if (!f1.is_open())
{
printf("日志文件创建失败!\n");return 0;
}
mysql_autocommit(&mysql, 0);//关闭自动提交
char* out_text = new char[1024];int cursor = 0;while (!rpf.eof())
{
memset(out_text,0x00,1024);
rpf.getline(out_text,1024);stringstr(out_text);if (str.length()>0)
{
lineCount++;if (lineCount>1)
{
std::vectorstrVec;int cellCount = H_Utility::stringSplitToVector(str.c_str(), strVec, ",");if (cellCount<3)
{
printf("第%d行数据不完整,写入失败:\n",lineCount);
f1<
}string sql_str = "";
sql_str.append("INSERT INTO `").append(SCHEMA_NAME).append("`.`").append(TABLE_NAME).append("`");
sql_str.append("(id,name,birthday) values (");
sql_str.append(strVec[0]).append(",'").append(strVec[1]).append("',");
sql_str.append("STR_TO_DATE('").append(strVec[31]).append("','%Y-%m-%d %H:%i:%s'))");int iSuccess = mysql_query(&mysql, sql_str.c_str());if (iSuccess != 0)
{const char *mysql_err = mysql_error(&mysql);
printf("%s\n",mysql_err);
f1<
}else{
cursor++;
}if (cursor==50000)//每50000条记录提交一次
{
mysql_commit(&mysql);
cursor= 0;
printf("%d\n",lineCount);
}
}
}
}
delete []out_text;
rpf.close();
f1.flush();
f1.close();
mysql_close(&mysql);
}returnlineCount;
}