1. 下载mysql安装包,记住安装的时候选择完全安装,不然是找不到mysql.h的
2. vs2012中新建一个win32 console项目,右击项目,选择属性。找到 配置属性--->VC++目录----->包含目录
找到 配置属性--->VC++目录----->库目录
找到 配置属性--->链接器----->输入----->附加依赖项
3. 将libmySQL.dll拷贝到生成的exe文件的目录中。
测试代码:
#include <stdio.h>
#include <stdlib.h>
#include "my_global.h"
#include "mysql.h"
int main(int argc, char *argv[])
{
MYSQL my_connection;
mysql_init(&my_connection);
//填写用户和密码
if (mysql_real_connect(&my_connection, "localhost", "****", "****","test",0,NULL,CLIENT_FOUND_ROWS))
{
printf("Connection success\n");
int res = mysql_query(&my_connection, "INSERT INTO Orders(order_num, order_date, cust_id)VALUES(20011, '2004-02-08', '1000000001');");
if (!res) {
printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
}
else {
fprintf(stderr, "Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
mysql_close(&my_connection);
}
else {
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection)) {
fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
system("pause");
return EXIT_SUCCESS;
}