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


發佈了56 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章