c++ mysql wrapper_mysql C++ wrapper

前言:

I don't really understand why they made a C++ wrapper for the C API (MySQL++ = http://www.tangentsoft.net/mysql++/ ) because shouldn't the C API work in C++?

The mysql C api will work in C++ - I suppose someone just wanted to do a c++ wrapper.

库推荐:

SOCI is a database access library for C++ that makes the illusion ofembeddingSQL queries in the regular C++ code, staying entirely within the Standard C++.

The idea is to provide C++ programmers a way to access SQL databases in the most natural and intuitive way. If you find existing libraries too difficult for your needs or just distracting, SOCI can be a good alternative.

MySQL++ is a C++ wrapper forMySQL’s C API. It is built around the same principles as the Standard C++ Library, to make dealing with the database as easy as dealing with STL containers. In addition, MySQL++ provides facilities that let you avoid the most repetitive sorts of SQL within your own code, providing native C++ interfaces for these common tasks.

个人编写:

1.  借助Boost库

This article present a lean C++ wrapper of mySQL client. All of the functions are defined in two header files. Users can build the file with mySQL C API library and header files ofboost::shared_ptrwhich is used to manage the MYSQL connection and result set.

3. 不借助外部库

#include

#include

int main()

{

MYSQL *conn;

MYSQL_RES *res;

MYSQL_ROW row;

char *server = "server";

char *user = "user";

char *password = "password"; // got tot keep my data secret

char *database = "cpp_test";

conn = mysql_init(NULL);

// connect to database

if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))

{

fprintf(stderr, "%s\n", mysql_error(conn));

return -1;

}

// send SQL query

if(mysql_query(conn, "select * from cpp_testTAB"))

{

fprintf(stderr, "%s\n", mysql_error(conn));

return -1;

}

res = mysql_use_result(conn);

// output table name

printf("MySQL Tables in mysql database:\n");

while ((row = mysql_fetch_row(res)) != NULL)

{

printf("%s %s %s %s\n", row[0], row[1], row[2], row[3]);

}

// close connection

mysql_free_result(res);

mysql_close(conn);

return 0;

}

Output is:

MySQL Tables in mysql database:

1 John Bartholomew Pippin

2 Thomas Tiberius Tabernathy

3 Delta Echo Gamma

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值