/*!
*Connect to the database acorrding to the information reading form the configure file
*\return mysqlpp::Connection ->the connection if success,else exit
*/
MYSQL* connectToDB( )
{
MYSQL* mysql=mysql_init(NULL);
try
{
if( mysql_real_connect(mysql,this->confg_.host_name,this->confg_.user_name,this->confg_.password,this->confg_.db_name,3306,NULL,0))
{
std::cout<<"mysql_real_connect() succeed"<<std::endl;
}
else
{
std::cout<<"mysql_real_connect() failed"<<std::endl;
}
return mysql;
}
catch (std::exception& er)
{
std::cerr << "Connection failed: " << er.what() << std::endl;
return mysql;
}
}
void insertlod()
{
MYSQL* mysql;
NGIDataBaseManager * mydb = NGIDataBaseManager::Instance();
char* query=new char[500000000];
char* end;
std::string slet="INSERT INTO test values(";
try
{
mysql=mydb->connectToDB();
if(mysql)
{
end = strcpy(query,"INSERT INTO test values(");
int len=slet.length();
end=end+len;
*end++= '\'';
for (int f_i=0;f_i<5;f_i++)
{
float fval;
if (f_i%2)
{
fval=14567.1235537;
}
else
fval=1.1234566;
/*方案二*/char tempbuf[10];
char* buf=new char [10];
memset(tempbuf,'\0',10);
_gcvt(fval,9,tempbuf);/*方案二结束*/
tempbuf[9]='#';
/*方案一char tempbuf[16];
memset(tempbuf,'\0',16);
std::sprintf(tempbuf,"%16.8f",fval);//后面的小数不见了
tempbuf[15]='#'; 方案一结束 */
buf=strdup(tempbuf);
end += mysql_real_escape_string(mysql,end,buf,10);
}
*end++ ='\'';
*end++ = ')';
if (mysql_real_query( mysql,query,(unsigned int) (end-query)) )
{
std::cout<<"mysql_query() insert data succeed"<<std::endl;
}
mysql_close(mysql);
}
}
catch (const mysqlpp::BadQuery& er) {
// Handle any query errors
std::cerr << "Query error: " << er.what() << std::endl;
mysql_close(mysql); exit (0);
}
catch (std::exception& er)
{
std::cerr << "Failed: " << er.what() << std::endl;
mysql_close(mysql); exit (0);
}
void NGIVoxetDBIO::getLod()
{
MYSQL* mysql;
std::string lod;
char* str_result;
NGIDataBaseManager * mydb = NGIDataBaseManager::Instance();
try
{
if( mysql=mydb->connectToDB() )
{
//查询 SQL
std::string query = "SELECT lod FROM test";
if(mysql_real_query(mysql, query.c_str(), (unsigned long)query.size()) !=0 )
{
std::cout<< "Query Failed"<<std::endl;
exit (0);
}
//! 使用结果集
MYSQL_RES* result;
if(!(result = mysql_store_result(mysql)))
{
std::cout<< "Query Failed"<<std::endl;
exit (0);
}
//! 显示内容
unsigned int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while (row = mysql_fetch_row(result))
{
unsigned long *lengths;
lengths = mysql_fetch_lengths(result);
for (unsigned int i =0; i < num_fields; ++i)
{
str_result=row[i];
QString td= QString(str_result);//从数据库中得到数据转为float型
QStringList gg=td.split("#");
std::vector<float> cstfl;
for (int j= 0;j<gg.count()-1;j++)
{
QString ss= gg.at(j);
cstfl.push_back(ss.toFloat());
/*std::string ss=static_cast<std::string> (gg.at(j));
const char* temp_ss=ss.c_str();
float fval=atof(temp_ss);
cstfl.push_back(fval);*/
}
float sum=cstfl.at(0)+cstfl.at(1);
for (int k=0;k<cstfl.size();k++)
{
printf("%f ",cstfl.at(k));
}
std::cout << std::endl;
//std::cout << row[i] << std::endl;
// std::cout <<str_result << std::endl;
//printf("%.*s", (int) lengths[i], row[i] ? row[i] : "NULL");
}
std::cout << std::endl;
}
mysql_free_result(result);
}
}
catch (const mysqlpp::BadQuery& er)
{
// Handle any query errors
std::cerr << "Query error: " << er.what() << std::endl;
exit (0);
}
catch (std::exception& er)
{
std::cerr << "Failed: " << er.what() << std::endl;
exit (0);
}
}
评论