mysql_store_result(&mysql) 返回结果集
mysql_num_fields 返回列数
mysql_num_rows 返回行数
/*查*/
/*j选择查什么信息:0=id,1=name,2=num,3=price,4=sign*/
string Modify_Goods::Search_Goods(int j) {
cout << " ----请输入商品编号---- " << endl;
unsigned int i;
cin >> i;
try {
DbConnection db(host, username, password, database);
//执行select命令,查询t_user表中的所有记录
string sql;
sql = "select * from goods";
MYSQL_RES res = db.executeQuery(sql.c_str()); //结果集类
//输出字段名
unsigned int num_fields = mysql_num_fields(&res); //列数
unsigned int num_rows = mysql_num_rows(&res); //行数
if (i > num_rows) {
cout << "查无此编号商品" << endl;
return " ";
}
//cout << num_fields << " " << num_rows << endl;
MYSQL_FIELD* fields = mysql_fetch_fields(&res); //数组存每一列
cout << endl;
MYSQL_ROW row;
int sign = 1;
while ((row = mysql_fetch_row(&res))) {
if (i == sign) {
return row[j];
break;
}
sign++;
}
}
catch (DbExcepiton exp) {
cout << exp.what() << endl;
}
}
读取文件连接数据库
mysql=mysql_init((MYSQL*)0);
ifstream file;
string path="D:/data.txt";
file.open(path.c_str());
string port1;
string url;
string name;
string pass;
string dataname;
if(file.is_open())//打开文件
{
char tmp[1000];
while(!file.eof())//循环读取每一行
{
file.getline(tmp,1000);
string line(tmp);
size_t pos=line.find('=');
string tmpkey=line.substr(0,pos);//取=号之前
if(tmpkey == "url")
{
url=line.substr(pos+1);//取=号之后
}
else if(tmpkey == "username")
{
name=line.substr(pos+1);
}
else if(tmpkey == "password")
{
pass=line.substr(pos+1);
}
else if(tmpkey == "dataname")
{
dataname=line.substr(pos+1);
}
else if(tmpkey == "port")
{
port1=line.substr(pos+1);
}
}
}
else
{
cout << "Connot open config file" << endl;
}
if(NULL!=mysql_real_connect(mysql,url.data(),name.data(),pass.data(),dataname.data(),atoi(port1.c_str()),NULL,0))
//if(NULL!=mysql_real_connect(mysql,"127.0.0.1","root","root","newems",3306,NULL,0))
{
temp="连接数据库成功";
showStatus(temp);
}
else
{
temp="连接失败";
showStatus(temp);
}
return true;
远程连接失败的解决办法:http://t.csdn.cn/JyRFI