vc通过postgre的存储过程来测试

PGresult   *res;
m_conn
= PQsetdbLogin("127.0.0.1","5432","","","postgres","postgres","123");
PQsetClientEncoding(m_conn,
"GBK");
if (PQstatus(m_conn) != CONNECTION_OK)
{
CString strErr;
strErr.Format(_T(
"连接数据库失败 [%s]"),CString(PQerrorMessage(m_conn)));
AfxMessageBox(strErr);
return;

}
////事务
res = PQexec(m_conn, "BEGIN");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
CString strErr;
strErr.Format(_T(
"执行事务失败 [%s]"),CString(PQerrorMessage(m_conn)));
PQclear(res);
AfxMessageBox(strErr);
return;
}
DWORD dwbegin
= ::GetTickCount();
//for (int i = 0;i<TESTCOUNT;i++)
//{
CTime tim=CTime::GetCurrentTime();
CString str
=tim.Format("%Y-%m-%d-%M-%S");//显示年月日
CStringA str1(str);
char pSql[200];
memset(pSql,
0,sizeof(pSql));
sprintf(pSql,
"select adddata($1,$2)");
const char *paramValues[2];
paramValues[
0] = "1111111";
paramValues[
1] = "22222222222222222";
PGresult
*result = PQexecParams(m_conn, pSql,2,NULL,paramValues,NULL,NULL,0);
if (PQresultStatus(result) != PGRES_TUPLES_OK)
{
CString strErr;
strErr.Format(_T(
"执行存储过程失败 [%s]"),CString(PQerrorMessage(m_conn)));
AfxMessageBox(strErr);
PQclear(result);
return;
}
//}

res
= PQexec(m_conn, "END");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
CString strErr;
strErr.Format(_T(
"连接数据库失败 [%s]"),PQerrorMessage(m_conn));
PQclear(res);
AfxMessageBox(strErr);
return;
}
DWORD dwend
= ::GetTickCount();
CString strOut;
strOut.Format(_T(
"通过存储过程 插入%d条数据 pglib所用时间为:%lf秒"),TESTCOUNT,(double)(dwend-dwbegin)/1000);
AfxMessageBox(strOut);
PQfinish(m_conn);

经测试 插入一万条数据 需要0.09秒左右 100W条记录需要14秒左右

posted @ 2011-08-11 15:30  银翼的魔术师  阅读(332)  评论(0编辑  收藏  举报