我想存储一个double数组进入Mysql数据库,希望详细告诉mysql中数据库用什么类型。以及对于的odbc代码如何编写,下面是基本类型double/int/char的存储,但double[31]如何实现存入数据库,并读取出来
SQLINTEGER cbValue = SQL_NTS;
RETCODE retcode;
char namebuf[32];
double price;
double density;
int pwnum;
char libnamebuf[32];
int category; retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,32,0,namebuf,strlen(namebuf),&cbValue);
retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),2,SQL_PARAM_INPUT,SQL_C_DOUBLE,SQL_DOUBLE,0,0,&price,0,&cbValue);
retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),3,SQL_PARAM_INPUT,SQL_C_DOUBLE,SQL_DOUBLE,0,0,&density,0,&cbValue);
retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),4,SQL_PARAM_INPUT,SQL_C_SSHORT,SQL_INTEGER,0,0,&pwnum,0,&cbValue);
retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),5,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,32,0,libnamebuf,strlen(libnamebuf),&cbValue);
retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),6,SQL_PARAM_INPUT,SQL_C_SSHORT,SQL_INTEGER,0,0,&category,0,&cbValue); retcode =SQLPrepare(m_dbconn.getStatmentHandle(),(SQLCHAR*)"insert into dope values(?,?,?,?,?,?)", SQL_NTS); memcpy(namebuf,dope.name.c_str(),dope.name.size());
namebuf[dope.name.size()]='\0';
price=dope.price;
density=dope.density;
pwnum=dope.pwnum;
memcpy(libnamebuf,dope.libName.c_str(),dope.libName.size());
libnamebuf[dope.libName.size()]='\0';
category=dope.category; retcode =SQLExecute(m_dbconn.getStatmentHandle());
if(retcode<0)
{
cout<<"insert errors"<<retcode<<endl;
//得到具体出错信息
SQLCHAR* pstate=new SQLCHAR[20];
SQLINTEGER * pnativeerro=new SQLINTEGER ;
SQLCHAR* erromsg=new SQLCHAR[1024];
SQLSMALLINT* smallint=new SQLSMALLINT;
SQLGetDiagRec(SQL_HANDLE_STMT,this->m_dbconn.getStatmentHandle(),1,pstate,pnativeerro,erromsg,1024,smallint);
cout<<"erro msg:"<<erromsg<<endl;
delete []pstate;
delete pnativeerro;
delete []erromsg;
delete smallint;
return false;
}mysqlodbc
SQLINTEGER cbValue = SQL_NTS;
RETCODE retcode;
char namebuf[32];
double price;
double density;
int pwnum;
char libnamebuf[32];
int category; retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,32,0,namebuf,strlen(namebuf),&cbValue);
retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),2,SQL_PARAM_INPUT,SQL_C_DOUBLE,SQL_DOUBLE,0,0,&price,0,&cbValue);
retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),3,SQL_PARAM_INPUT,SQL_C_DOUBLE,SQL_DOUBLE,0,0,&density,0,&cbValue);
retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),4,SQL_PARAM_INPUT,SQL_C_SSHORT,SQL_INTEGER,0,0,&pwnum,0,&cbValue);
retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),5,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,32,0,libnamebuf,strlen(libnamebuf),&cbValue);
retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),6,SQL_PARAM_INPUT,SQL_C_SSHORT,SQL_INTEGER,0,0,&category,0,&cbValue); retcode =SQLPrepare(m_dbconn.getStatmentHandle(),(SQLCHAR*)"insert into dope values(?,?,?,?,?,?)", SQL_NTS); memcpy(namebuf,dope.name.c_str(),dope.name.size());
namebuf[dope.name.size()]='\0';
price=dope.price;
density=dope.density;
pwnum=dope.pwnum;
memcpy(libnamebuf,dope.libName.c_str(),dope.libName.size());
libnamebuf[dope.libName.size()]='\0';
category=dope.category; retcode =SQLExecute(m_dbconn.getStatmentHandle());
if(retcode<0)
{
cout<<"insert errors"<<retcode<<endl;
//得到具体出错信息
SQLCHAR* pstate=new SQLCHAR[20];
SQLINTEGER * pnativeerro=new SQLINTEGER ;
SQLCHAR* erromsg=new SQLCHAR[1024];
SQLSMALLINT* smallint=new SQLSMALLINT;
SQLGetDiagRec(SQL_HANDLE_STMT,this->m_dbconn.getStatmentHandle(),1,pstate,pnativeerro,erromsg,1024,smallint);
cout<<"erro msg:"<<erromsg<<endl;
delete []pstate;
delete pnativeerro;
delete []erromsg;
delete smallint;
return false;
}mysqlodbc
char reflectbuf[WAVELEN*sizeof(double)+1];
retcode=SQLBindParameter(m_dbconn.getStatmentHandle(),4,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_UNKNOWN_TYPE,WAVELEN*sizeof(double)+1,0,reflectbuf,WAVELEN*sizeof(double)+1,&cbValue); memcpy(reflectbuf,samplecard.reflect,sizeof(double)*WAVELEN);
reflectbuf[sizeof(double)*WAVELEN]='\0';
取数据如下:
char reflectbuf[sizeof(double)*WAVELEN+1]={0};
retcode = SQLBindCol(this->m_dbconn.getStatmentHandle(), 4, SQL_C_CHAR,reflectbuf, sizeof(double)*WAVELEN+1,&cbreflect);
double reflect[WAVELEN]={0};
memcpy(reflect,reflectbuf,sizeof(double)*WAVELEN);