/*
vim /etc/vimrc--始具体情况而定
vim 显示行号、c语言高亮、自动缩进等
set nu
set cindent
set autoindent
syntax on
编译前提:
环境:os RH5_x86_64,mysql5
使用rpm包安装好了mysql,如果其他环境请自行设置编译命令;
编译命令:
g++ $(mysql_config --cflags) $(mysql_config --libs) mysql.c
mysql官方帮助文档:
http://dev.mysql.com/doc/refman/5.1/en/c-api-functions.html
*/
#include <iostream>
#include "mysql.h"
//pragma comment
using namespace std;
void use_mysql_query(char* const);
int main()
{
cout<<"---------- 下面开始连接mysql数据库,看仔细了----------"<<endl;
use_mysql_query("select * from mysql.user");
}
void use_mysql_query(char* const str)
{
MYSQL *mysql=mysql_init(0);
mysql=mysql_real_connect(mysql,"localhost","root","","mysql",3306,0,0);
int nResult=mysql_query(mysql,str);
cout<<"查询的返回01: "<<nResult<<endl;
if(nResult!=0)
{
cout<<"查询数据库出错!"<<endl;
exit(1);
}
MYSQL_RES *res=mysql_use_result(mysql);
MYSQL_FIELD *field;
int field_count=res->field_count;
while((field=mysql_fetch_field(res))!=NULL)
{
cout<<"字段:"<<field->name<<"介绍开始------------;"<<endl;
cout<<"名称:"<<field->name<<endl;
cout<<"长度:"<<field->length<<endl;
cout<<"类型:"<<field->type<<endl;
cout<<"字段:"<<field->name<<"介绍结束------------;"<<endl;
cout<<""<<endl;
}
MYSQL_ROW row;
while((row=mysql_fetch_row(res))!=NULL)
{
cout<<"============"<<endl;
for(int i=0;i<field_count;i++)
{
cout<<row[i]<<"---------";
}
}
cout<<"开始释放资源,关闭数据库连接-----"<<endl;
mysql_free_result(res);
mysql_close(mysql);
}
linux mysql api c/c++
最新推荐文章于 2022-08-26 09:18:01 发布