mysql connector c编程_MySQL数据库之MySQL Connector 编程

本文主要向大家介绍了MySQL数据库之MySQL Connector 编程 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

MySQL Connector 是MySQL数据库客户端编程的接口, 它提供了通过网络访问数据库的接口, 这些功能在动态链接库(.dll, .so)或者静态对象库(.lib, .a)中实现.

使用时必须注意这些库是32位还是64位的.

下面是一个例子:

#include 

#include 

#include 

// 使用静态对象库

//#pragma comment(lib, "C:\\Program Files\\MySQL\\MySQL Connector C 6.1\\lib\\vs12\\mysqlclient.lib")

// 使用动态链接库

// 确保 libmysql.dll 在系统路径中可以搜到

#pragma comment(lib, "C:\\Program Files\\MySQL\\MySQL Connector C 6.1\\lib\\libmysql.lib")

void simpleUsega()

{

MYSQL *conn;

conn = mysql_init(NULL);

if (conn == NULL) {

printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));

exit(1);

}

if (mysql_real_connect(conn, "localhost", "user_name",

"user_password", NULL, 0, NULL, 0) == NULL) {

printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));

exit(1);

}

if (mysql_query(conn, "create database frist_db")) {

printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));

exit(1);

}

mysql_close(conn);

}

int main() {

MYSQL *mysql = NULL;

char pwd[1024];

char usr[1024];

printf("Target platform word length : %d \n", sizeof(void*) );

printf("Connector version: %s \n", mysql_get_client_info());

//simpleUsage();

//return 0;

printf("Initializing MySQL Connector... \n");

mysql_library_init(0, NULL, NULL); // 在其他work线程产生之前初始化mysql c库, 不要让mysql_init来调用, 否则可能导致线程安全问题

if (!(mysql = mysql_init(NULL))) {

printf("Field. \n");

goto end;

}

printf("OK, Conecting... \n");

// 配置用户和密码

if (0) {

printf("Please keyin user_name and password \n"

"name: ");

scanf_s("%s", usr, 1024);

printf("pwd : ");

scanf_s("%s", pwd, 1024);

} else {

sprintf_s(usr, 1024, "default_user_name");

sprintf_s(pwd, 1024, "default_user_password");

}

// 连接 localhost 上的服务器

if (!mysql_real_connect(mysql, "localhost", usr, pwd, (const char*) 0, 3306, NULL, 0)) {

printf("Filed, Error %u, %s \n", mysql_errno(mysql), mysql_error(mysql) );

goto end;

}

printf("Login succeed. \n");

// 销毁密码

sprintf_s(pwd, 1024, "00000000000000");

// 查询数据库服务器时间

mysql_query(mysql, "SELECT NOW();");

if (!mysql_errno(mysql)) {

MYSQL_RES *result;

MYSQL_ROW row;

int num_fields;

int i;

result = mysql_store_result(mysql);

num_fields = mysql_num_fields(result);

while ((row = mysql_fetch_row(result)))

{

for(i = 0; i 

{

printf("%s ", row[i] ? row[i] : "NULL");

}

printf("\n");

}

mysql_free_result(result);

}

end:

system("pause");

mysql_close(mysql);

mysql_library_end();

return 0;

}

本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值