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

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

下面是一个例子:

#include <stdio.h>
#include <stdlib.h>
#include <C:\Program Files\MySQL\MySQL Connector C 6.1\include\mysql.h> // 使用静态对象库
//#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();
}
if (mysql_real_connect(conn, "localhost", "user_name",
"user_password", NULL, , NULL, ) == NULL) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit();
}
if (mysql_query(conn, "create database frist_db")) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit();
}
mysql_close(conn); } int main() {
MYSQL *mysql = NULL;
char pwd[];
char usr[]; 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(, NULL, NULL); // 在其他work线程产生之前初始化mysql c库, 不要让mysql_init来调用, 否则可能导致线程安全问题
if (!(mysql = mysql_init(NULL))) {
printf("Field. \n");
goto end;
} printf("OK, Conecting... \n");   // 配置用户和密码
if () {
printf("Please keyin user_name and password \n"
"name: ");
scanf_s("%s", usr, );
printf("pwd : ");
scanf_s("%s", pwd, );
} else {
sprintf_s(usr, , "default_user_name");
sprintf_s(pwd, , "default_user_password");
}   // 连接 localhost 上的服务器
if (!mysql_real_connect(mysql, "localhost", usr, pwd, (const char*) , , NULL, )) {
printf("Filed, Error %u, %s \n", mysql_errno(mysql), mysql_error(mysql) );
goto end;
}
printf("Login succeed. \n"); // 销毁密码
sprintf_s(pwd, , "");

  // 查询数据库服务器时间
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 = ; 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;
}

MySQL Connector 编程的更多相关文章

  1. 创建ASP.NET Core MVC应用程序(2)-利用MySQL Connector NET连接到MySQL

    创建ASP.NET Core MVC应用程序(2)-利用MySQL Connector NET连接到MySQL 用惯.NET的研发人员都习惯性地使用SQLServer作为数据库.然而.NET Core ...

  2. [转]MySQL Connector/C++(一)

    http://www.cnblogs.com/dvwei/archive/2013/04/18/3029464.html#undefined#undefined MySQL Connector/C++ ...

  3. python_基础学习_04_mysql库验证与安装(mysql-python,mysql.connector)

    验证python-mysql是否安装 1:python 2: import MySQLdb 安装步骤: 1.sudo apt-get install python-setuptools 2.sudo ...

  4. MySQL Connector/J

    5.1 Developer Guide 1. MysQL为由Java语言编程的客户端程序提供连接:MySQL Connector/J,这是一个实现Java Database Connectivity( ...

  5. MySql Connector/C++8简介

    MySql Connector/C++8是一个用于连接MySQL服务器的C++应用程序.Connector/C++8可用于访问实现文档存储的 MySQL服务器,或者使用SQL查询以传统方式访问.它支持 ...

  6. vc++2013中使用MySQL connector/C++ 1.1.4静态链接报错

    包含头文件 #include <mysql_connection.h> #include <mysql_driver.h> #include <cppconn/state ...

  7. Using MySQL Connector .NET 6.6.4 with Entity Framework 5

    I had been waiting for the latest MySQL connector for .NET to come out so I can move on to the new a ...

  8. mysql.connector操作mysql的blob值

    This tutorial shows you how to work with MySQL BLOB data in Python, with examples of updating and re ...

  9. Ubuntu & MacOS安装Mysql & connector

    Ubuntu & MacOS安装Mysql & connector 1. 安装MySql sudo apt-get install mysql-server apt-get insta ...

随机推荐

  1. 【UOJ#246】套路(动态规划)

    [UOJ#246]套路(动态规划) 题面 UOJ 题解 假如答案的选择的区间长度很小,我们可以做一个暴力\(dp\)计算\(s(l,r)\),即\(s(l,r)=min(s(l+1,r),s(l,r- ...

  2. ubuntu配置mysql

    1.安装mysql: sudo apt-get install mysql-server sudo apt-get install mysql-client sudo apt-get install ...

  3. cf366C Dima and Salad (dp)

    是一个01分数规划的形式,只不过已经帮你二分好了. 把b乘过去,再减回来,找和等于0的a的最大值就行了 #include<bits/stdc++.h> #define pa pair< ...

  4. UDS(ISO14229-2006) 汉译(No.7 应用层协议)

    标签:cte   amp   通信   pac   condition   man   没有   参数错误   family 7.1定义 应用层协议通常作为确认消息的传输,意味着从客户端发送的每一个请 ...

  5. ftp 两台服务器传输文件 apache

    import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputS ...

  6. 数组和list互转

    数组转list 方法1: String[] stringArray = { "a", "b", "c", "d", &q ...

  7. [THUWC2017]在美妙的数学王国中畅游

    [THUWC2017]在美妙的数学王国中畅游 e和sin信息不能直接合并 泰勒展开,大于21次太小,认为是0,保留前21次多项式即可 然后就把e,sin ,kx+b都变成多项式了,pushup合并 上 ...

  8. MVC知识点汇总

    一,MVC创建控制器,视图 1新建 ASP.NET MVC web应用程序项目,在Controllers文件夹下创建控制器,MVC 5 控制器,然后打开控制器,在所对应的控制器中会存在一个Action ...

  9. 【清北学堂2018-刷题冲刺】Contest 1

    Task 1:最小公倍数  输入n,求n与246913578的最小公倍数.  结果对1234567890取模. [样例输入] 3 [样例输出] 246913578 [数据规模和约定] 对于30%的数据 ...

  10. (string stoi 栈)leetcode682. Baseball Game

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...