MySQL Connector 编程
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 编程的更多相关文章
- 创建ASP.NET Core MVC应用程序(2)-利用MySQL Connector NET连接到MySQL
创建ASP.NET Core MVC应用程序(2)-利用MySQL Connector NET连接到MySQL 用惯.NET的研发人员都习惯性地使用SQLServer作为数据库.然而.NET Core ...
- [转]MySQL Connector/C++(一)
http://www.cnblogs.com/dvwei/archive/2013/04/18/3029464.html#undefined#undefined MySQL Connector/C++ ...
- python_基础学习_04_mysql库验证与安装(mysql-python,mysql.connector)
验证python-mysql是否安装 1:python 2: import MySQLdb 安装步骤: 1.sudo apt-get install python-setuptools 2.sudo ...
- MySQL Connector/J
5.1 Developer Guide 1. MysQL为由Java语言编程的客户端程序提供连接:MySQL Connector/J,这是一个实现Java Database Connectivity( ...
- MySql Connector/C++8简介
MySql Connector/C++8是一个用于连接MySQL服务器的C++应用程序.Connector/C++8可用于访问实现文档存储的 MySQL服务器,或者使用SQL查询以传统方式访问.它支持 ...
- vc++2013中使用MySQL connector/C++ 1.1.4静态链接报错
包含头文件 #include <mysql_connection.h> #include <mysql_driver.h> #include <cppconn/state ...
- 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 ...
- mysql.connector操作mysql的blob值
This tutorial shows you how to work with MySQL BLOB data in Python, with examples of updating and re ...
- Ubuntu & MacOS安装Mysql & connector
Ubuntu & MacOS安装Mysql & connector 1. 安装MySql sudo apt-get install mysql-server apt-get insta ...
随机推荐
- Hdoj 2602.Bone Collector 题解
Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bone C ...
- ⌈洛谷1312⌋⌈NOIP提高组2011⌋Mayan游戏【搜索】
感想 真的,感觉这道题目好坑爹,我这个蒟蒻调了好几个世纪才调出来. 重构代码千万遍,依旧只有-1输出. 正解 非常明显的一道搜索题目. 每一次记录上一级的状态,这样实现比较不容易出错. 然后考虑剪枝: ...
- AXURE 8弄一个轮播图的步骤
这个图是网上找到,7.0可以使用. 如果是8.0.没有找到"动态面板"这个地方,如下图所示
- base64加密图片处理
场景:下载html中内嵌的base64加密图片 举个例子,博客园的插入图片有两种方式,一是引用图片链接,二是直接粘贴2进制图片文件.以第二种方式的图片则是以base64加密的方式内嵌在html页面中. ...
- http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part3.html
Building Microservices with Spring Boot and Apache Thrift. Part 3. Asynchronous services Posted on 4 ...
- SRM 600 div 2 T 1
贪心+枚举 #include <bits/stdc++.h> using namespace std; class TheShuttles { public: int getLeast ...
- CF 1013E Hills
这是一道DP题...我居然有那么半个小时思考非DP解决方案,实在是太弱了. 题意:给您若干山,您可以花费1代价削去1高度,求有k个山峰时的最小代价. 输出k = 1 ~ (n + 1) >> ...
- 将pandas的Dataframe对象读写Excel文件
Dataframe对象生成Excel文件 需要xlrd库 命令 pip install xlrd #导入pandas import pandas as pd import numpy as np ...
- (五)Oracle 的 oracle 表查询
http://www.hechaku.com/Oracle/oracle_tables_chack.html 通过scott用户下的表来演示如何使用select语句,接下来对emp.dept.salg ...
- latex 导入pdf
pdflatex \includepdf[addtotoc={1,section,1,something would show in catalog,cc},pages=-,offset=0cm 0. ...