C Programer  - 讨论区

标题:使用MySQL++ Library构建数据库应用程序

2014年02月05日 星期三 20:16

MySQL++是一套C++语言的MySQL客户端开发库,历史悠久且复杂,从我的实践来看,API容易理解和使用,是典型的C++风格。

关于MySQL++与官方MySQL Connector/C++的对比,可以参考作者的自述:

How does MySQL++ compare to MySQL’s Connector/C++?

Connector/C++ is a new C++ wrapper for the MySQL C API library, fully developed under the MySQL corporate aegis. By contrast, MySQL++ has a long, complex development history. MySQL++ started out as a third-party library, was maintained and enhanced by MySQL for several years, then got spun back out again, this time probably for good. MySQL does still host our mailing list, for which we thank them, but they don’t control the hosting or development of MySQL++ any more.

MySQL decided to create a competing library for two reasons. First, having the entire thing to themselves mean they can do the same dual-licensing thing they do with the C API library. Second, after Sun bought MySQL, they wanted to put MySQL support into OpenOffice, and wanted a JDBC style API for that support.

By contrast with Connector/C++’s Java-style database API, MySQL++ is very much a native C++ library: we use STL and other Standard C++ features heavily. If you are a Java developer or simply admire its database interface design, you may prefer Connector/C++.

Another way to look at it is that Connector/C++ is new and therefore perhaps less crufty, while MySQL++ is mature and featureful.

CentOS系统的EPEL库包括此软件,安装非常方便。

代码示例如下:

#include <iostream>
#include <string>
#include <mysql++.h>

using namespace std;
using namespace mysqlpp;

int main(int argc,char **argv) {
	Connection conn(false);
	conn.connect(
		"test","10.1.1.161","mengguang","1234qwer");
	if(!conn) {
		cerr << "ERROR: " << conn.error() << endl;
		return -1;
	}
	Query query=conn.query(
		"insert into friends (name,email) values( %0q,%1q)");
	query.parse();
	query.execute("mengguang","mengguang@gmail.com");
	cout << "ID: " << query.insert_id() << endl;
	query.execute("mengkang","mengkang@163.com");
	cout << "ID: " << query.insert_id() << endl;

	query.reset();
	query << "select * from friends";
	StoreQueryResult res=query.store();
	int id; string name;string email;
	for(auto row : res) {
		for(auto item : row) {
			cout << item << " " ;
		}
		cout << endl;

		for(unsigned int i=0;i<row.size();i++) {
			cout << row[i] << " ";
		}
		cout << endl;

		cout << row["id"] << " " << row["name"] 
			<< " " << row["email"] << endl;

		id = row["id"];
		row["name"].to_string(name);
		row["email"].to_string(email);
		cout << id << " " << name << " " << email << endl;

	}
	query.reset();
	query << "delete from friends";
	query.execute();
	cout << "rows deleted: " << query.affected_rows() << endl;
	return 0;
}

参考资料:

http://tangentsoft.net/mysql++/

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号