插入數據到MySQL
bool mysqlinsert2(int sourceindex,int targetindex,string source,string target)
{
MYSQL *conn_ptr;
conn_ptr = mysql_init(NULL);
if(!conn_ptr)
{
fprintf(stderr,"mysql_init failed\n");
//return EXIT_FAILURE;
}
conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","nlpgroup","testdb",0,NULL,0);
if(conn_ptr)
{
int abc;
char sql_buffer[500] = {0};
sprintf(sql_buffer,"insert into EM2 (sourceindex,targetindex,source,target) values(\"%d\",\"%d\",\"%s\",\"%s\")",sourceindex,targetindex,source.c_str(),target.c_str());
if((abc = mysql_query(conn_ptr,sql_buffer)))
{
printf("error code:%s\n",mysql_error(conn_ptr));
}
mysql_close(conn_ptr);
return true;
//connection succees
}
else
{
printf("Error code: %d %s %s\n",mysql_errno(conn_ptr),mysql_sqlstate(conn_ptr),mysql_error(conn_ptr));
mysql_close(conn_ptr);
return false;
//return EXIT_SUCCESS;
}
}
用於運行循環和輸入for循環索引和值轉換成代碼MySQL的通過調用函數mysqlinsert2
void write_token_to_data()
{
typedef double* DynamicMatrix[l+m];
// DynamicMatrix Count;
typedef double* DynamicMatrix2[l+m];
//DynamicMatrix2 Prob;
for(int i=0; i<(l+m); i++)
{
for(int j=0; j<(l+m); j++)
{
if(mysqlinsert2(i,j,combine[i],combine[j]))
{
cout<<"insert OK!!"<<endl;
}
else
{
cout<<"insert failed"<<endl;
}
}
}
}//end of function
當MySQL插入確定程序將返回「插入好!!」 所以,這是我的問題
這些查詢可能是直到30000 * 30000次 ,我想知道如何加快這個,也許我可以優化MySQL的設置或改變在C++編寫代碼,我希望它有相同的結果
批量插入非常重要的是如何能保持該值,直到循環繼續? – 2014-09-30 06:41:15
你的意思是遞增價值?例如,您可以生成要插入的行數組(以您認爲合適的方式更改數值),然後返回對應於最後一個插入行的值。 – Ashalynd 2014-09-30 08:40:13