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 ...
随机推荐
- 前端之Android入门(3):MVC模式(上)
很多Android的入门书籍,在前面介绍完布局后就会逐个介绍组件,然后开始编写组件使用的例子.每每到此时小伙伴们都可能会有些疑问:是否应该先啃完一本<Java编程思想>学点 Java 知识 ...
- redis在centos7下安装
https://blog.csdn.net/wzygis/article/details/51705559 1.redis下载地址:http://www.redis.cn/download.html ...
- bzoj1047/luogu2216 理想的正方形 (单调队列)
开b组单调队列,分别维护此时某一列中的最大/最小值 然后我每次把它们的头取出来,塞到维护行的单调队列里,就是n*n的最大/最小值 #include<bits/stdc++.h> #defi ...
- js,jquery 获取滚动条高度和位置, 元素距顶部距离
一,获取滚动条高度和位置 jQuery 获取览器显示区域的高度: $(window).height(); 获取浏览器显示区域的宽度:$(window).width(); 获取页面的文档高度:$(do ...
- 【UVA1194】Machine Schedule
题目大意:给定 N 个任务和两台机器,每个任务可以在任意一台机器上执行,每台机器有 N 个启动状态,不同任务需要机器在不同的状态下执行,求执行所有任务需要多少个不同的状态. 题解:由于一个任务一定要被 ...
- Django session相关操作
Django session 是存储在数据库中的所以要先跟数据库建立连接 本连接有Django跟数据库建立连接的操作:https://www.cnblogs.com/Niuxingyu/p/10296 ...
- PKUWC2019滚粗记
真的不想说些什么了,还是自己太菜了,水平低啊.深深感受到NOIP2017以后的那种绝望,这最好的签约清北的机会,就这样白白错过了.本以为自己的应该有NOIAg的实力,运气好是前120吧,没想到自己还是 ...
- 第十五篇-EditText做简单的登录框
TextView和EditText的简单应用. MainActivity.java package com.example.aimee.edittexttest; import android.sup ...
- io系列之其他类
一.File类:将文件或者文件夹封装成对象. 方便对文件和文件夹的属性信息进行操作. File对象可以作为参数传递给流的构造函数. 注意: 文件夹名称也可带有扩展名. 构造函数: File(Strin ...
- Codeforces Round #518 (Div. 2) B LCM
传送门 https://www.cnblogs.com/violet-acmer/p/10163375.html 题解: 这道题有点意思,有点数学的味道. 根据定义“[a,b] / a”可得这求得是l ...