mysql数据库编程

//============================================================================
// Name        : CExercise.cpp
// Author      : Haier
// Version     : 0.1
// Copyright   : Your copyright notice
// Description : Connect Mysql in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstring>
#include <linux/socket.h>
#include <mysql/mysql.h>
using namespace std;

int main() 
{
	MYSQL mysql;
	MYSQL_RES *result;
	MYSQL_ROW row;

	mysql_init(&mysql);
	if(!mysql_real_connect(&mysql,"10.87.30.13X","root","XXXXXX","mysql",3306,NULL,0))
	{
		cout<<"mysql connect failed !"<<endl;
		return 0;
	}

	if(mysql_real_query(&mysql,"select * from user",(unsigned long)strlen("select * from user")))
	{
		cout<<"select failed !"<<endl;
		return 0;
	}

	if(NULL==(result=mysql_store_result(&mysql)))
	{
		cout<<"stroe result failed !"<<endl;
		return 0;
	}

	while(row=mysql_fetch_row(result))
	{
		cout<<row[0]<<"\t"<<row[1]<<endl;
	}

	mysql_free_result(result);
	mysql_close(&mysql);



	cout << "!!!Hello World\t!!!" << endl; // prints !!!Hello World!!!
	return 0;
}

运行示例:

localhost	root
10.96.17.147	root
localhost	
10.96.17.148	root
localhost	pma
localhost	bugfree
10.96.17.188	root
10.96.17.175	root
localhost	zentao
10.96.17.188	zentao
10.96.17.7	root
10.96.17.7	zentao
10.96.17.190	zentao


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值