1.int mysql_library_init(int argc, char **argv, char **groups)
在调用任何其他MySQL函数之前,调用此函数初始化MySQL客户端库。
2.void mysql_library_end(void)
完成使用库后调用它(例如,在断开与服务器的连接后)。
#include
#include
using namespace std;
int main()
{
if (mysql_library_init(0, NULL, NULL)) {
fprintf(stderr, "could not initialize MySQL client library\n");
exit(1);
}
/* Use any MySQL API functions here */
mysql_library_end();
return 0;
}
3.MYSQL *mysql_init(MYSQL *mysql)
初始化一个用于连接的对象,如果 mysql
是NULL
指针,则函数分配,初始化并返回新对象。否则,初始化对象并返回对象的地址。如果 mysql_init()
分配新对象,则在mysql_close()
调用它以关闭连接时将释放该对象 。
4.void mysql_close(MYSQL *mysql)
关闭先前打开的连接。
5.int mysql_options(MYSQL *mysql, enum mysql_option option, const void *arg)
可用于设置额外的连接选项并影响连接的行为。可以多次调用此函数以设置多个选项。要检索选项值,可使用mysql_get_option()
。
6.MYSQL *mysql_real_connect(
MYSQL *mysql, //现有MYSQL
结构的地址
const char *host, //主机名或IP地址
const char *user, //用户的MySQL登录ID
const char *passwd, //密码
const char *db, //数据库名称
unsigned int port, //如果port
不为0,则该值用作TCP / IP连接的端口号
const char *unix_socket, //如果unix_socket
不是 NULL
,则字符串指定要使用的套接字或命名管道
unsigned long client_flag) //值client_flag
通常为0,但可以设置为特定组合以启用某些功能
尝试连接正在运行的mysql的host, mysql_real_connect()
必须先成功完成,然后才能执行需要有效MYSQL连接
的任何其他API函数。
7.int mysql_query(MYSQL *mysql, const char *stmt_str)
执行sql语句,第二个参数是包含sql语句的字符串数组或字符指针
8.MYSQL_RES *mysql_store_result(MYSQL *mysql)
在任何有结果集的mysql_query()调用之后需要再调用mysql_store_result()来获得结果
9.void mysql_free_result(MYSQL_RES *result)
完成结果集后,必须通过调用释放它使用的内存,释放后不要尝试访问结果集
10.my_ulonglong mysql_num_rows(MYSQL_RES *result)
结果集中的行数
11.unsigned int mysql_num_fields(MYSQL_RES *result)
返回结果集中的列数
12.MYSQL_FIELD *mysql_fetch_field_direct(MYSQL_RES *result, unsigned int fieldnr)
使用此函数可以检索任意列的定义
13.MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)
检索任意行的结果
14.MYSQL_RES *mysql_list_dbs(MYSQL *mysql, const char *wild)
返回由服务器上与wild
参数指定的简单正则表达式匹配的数据库名称组成的结果集,调用 mysql_list_dbs()
类似于执行查询 SHOW DATABASES [LIKE
wild
]
15.
bool mysql_change_user(MYSQL *mysql, const char *user, const char *password, const char *db)
更改用户并使指定的数据库 db
成为指定连接
示例代码:test.cpp
#include
#include
using namespace std;
int Mysqltest()
{
MYSQL mydata;
//初始化
if (0 == mysql_library_init(0, NULL, NULL)) {
cout << "mysql_library_init() succeed" << endl;
}
else {
cout << "mysql_library_init() failed" << endl;
}
if (NULL != mysql_init(&mydata)) {
cout << "mysql_init(mydata) succeed" << endl;
}
else {
cout << "mysql_init(mydata) failed" << endl;
return -1;
}
//处理中文
if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "utf8")) {
cout << "mysql_options() succeed" << endl;
}
else {
cout << "mysql_options() failed" << endl;
}
//连接数据库
if (NULL != mysql_real_connect(&mydata, "localhost", "root", "12345", "test", 3306, NULL, 0)) {
cout << "mysql_real_connect() succeed" << endl;
}
else {
cout << "mysql_real_connect() failed" << endl;
return -1;
}
string sqlstr;
//建表
sqlstr = "CREATE TABLE IF NOT EXISTS new_paper(";
sqlstr += "NewID int(11) NOT NULL AUTO_INCREMENT,";
sqlstr += "NewCaption varchar(40) NOT NULL,";
sqlstr += "NewContent text,";
sqlstr += "NewTime datetime DEFAULT NULL,";
sqlstr += "PRIMARY KEY(NewID)";
sqlstr += ")ENGINE = InnoDB DEFAULT CHARSET = utf8"
;
if (0 == mysql_query(&mydata, sqlstr.c_str())) {
cout << "mysql_query() create table succeed" << endl;
}
else {
cout << "mysql_query() create table failed" << endl;
mysql_close(&mydata);
return -1;
}
//插入数据
for (int i = 0; i < 5; i++) {
sqlstr = "INSERT INTO test.new_paper(NewID,NewCaption,NewContent,NewTime)";
sqlstr += "VALUES(default,'小明','吃了两个西瓜','2017-01-11');";
if (0 == mysql_query(&mydata, sqlstr.c_str())) {
cout << "mysql_query() insert data succeed" << endl;
}
else {
cout << "mysql_query() insert data failed" << endl;
mysql_close(&mydata);
return -1;
}
}
//显示数据
sqlstr = "SELECT NewID,NewCaption,NewContent,NewTime FROM test.new_paper";
MYSQL_RES *result = NULL;
if (0 == mysql_query(&mydata, sqlstr.c_str())) {
cout << "mysql_query() select data succeed" << endl;
result = mysql_store_result(&mydata);
int rowcount = mysql_num_rows(result);
cout << "row count:" << rowcount << endl;
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\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\t";
}
cout << endl;
row = mysql_fetch_row(result);
}
}
else {
cout << "mysql_query() select data failed" << endl;
mysql_close(&mydata);
return -1;
}
//删除表
sqlstr = "DROP TABLE test.new_paper";
if (0 == mysql_query(&mydata, sqlstr.c_str())) {
cout << "mysql_query() drop table succeed" << endl;
}
else {
cout << "mysql_query() drop table failed" << endl;
mysql_close(&mydata);
return -1;
}
mysql_free_result(result);
mysql_close(&mydata);
mysql_library_end();
return 0;
}
int main()
{
Mysqltest();
return 0;
}
编译:
g++ test.cpp `mysql_config --cflags --libs` -o test
运行结果:
fs@ubuntu:~$ ./test
mysql_library_init() succeed
mysql_init(mydata) succeed
mysql_options() succeed
mysql_real_connect() succeed
mysql_query() create table succeed
mysql_query() insert data succeed
mysql_query() insert data succeed
mysql_query() insert data succeed
mysql_query() insert data succeed
mysql_query() insert data succeed
mysql_query() select data succeed
row count:5
NewID NewCaption NewContent NewTime
1 小明 吃了两个西瓜 2017-01-11 00:00:00
2 小明 吃了两个西瓜 2017-01-11 00:00:00
3 小明 吃了两个西瓜 2017-01-11 00:00:00
4 小明 吃了两个西瓜 2017-01-11 00:00:00
5 小明 吃了两个西瓜 2017-01-11 00:00:00
mysql_query() drop table succeed
注:使用完数据库之后一定不要忘记释放内存和断开连接,关闭数据库!(mysql_free_result()、mysql_close()、mysql_library_end())