前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mysql connector详解_MySQL Connector 编程

mysql connector详解_MySQL Connector 编程

作者头像
全栈程序员站长
发布2022-09-17 09:53:09
1.4K0
发布2022-09-17 09:53:09
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

#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”)

voidsimpleUsega()

{

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”);gotoend;

}

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) );gotoend;

}

printf(“Login succeed. \n”);//销毁密码

sprintf_s(pwd, 1024, “00000000000000”);

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

mysql_query(mysql,”SELECT NOW();”);if (!mysql_errno(mysql)) {

MYSQL_RES*result;

MYSQL_ROW row;intnum_fields;inti;

result=mysql_store_result(mysql);

num_fields=mysql_num_fields(result);while ((row =mysql_fetch_row(result)))

{for(i = 0; i < num_fields; 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;

}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159509.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档