C++中连接MySql数据库

// 首先要配置VC6.0
//
// 工具->选项->目录——>添加MySql安装目录的lib文件夹和include文件夹
//
// 将libmysql.dll拷贝到bin文件夹中,否则执行时出错;

#include <WINSOCK.H>
#include <IOSTREAM>
#include <MYSQL.H>
#include <string>

#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"libmysql.lib")

using namespace std;

void main(){
MYSQL mydata;
mysql_library_init(0,NULL,NULL);
mysql_init(&mydata);
mysql_options(&mydata,MYSQL_SET_CHARSET_NAME,"gbk");
mysql_real_connect(&mydata,"localhost","root","1","student",3306,NULL,0);
//string sqlstr="insert into stuinfo values('011','李彦宏','男',40,'百度')";
string sqlstr="select * from stuinfo";
MYSQL_RES *result=NULL;
mysql_query(&mydata,sqlstr.c_str());
result=mysql_store_result(&mydata);
int rowcount=mysql_num_rows(result);
unsigned int fieldcount=mysql_num_fields(result);
MYSQL_FIELD *field=NULL;
for(unsigned int i=0;i<fieldcount;i++)
{
field=mysql_fetch_field_direct(result,i);
cout<<field->name<<"\t";
}
cout<<endl;
MYSQL_ROW row=NULL;
row=mysql_fetch_row(result);
while(NULL!=row)
{
for(int i=0; i<fieldcount;i++)
{
cout<<row[i]<<"\t";
}
cout<<endl;
row=mysql_fetch_row(result);
}
mysql_free_result(result);
mysql_close(&mydata);
mysql_server_end();
}

你可能感兴趣的:(C++,mysql,null,insert,include,library)