#include"stdafx.h" #include<winsock2.h>//这个是必须添加的,不然就会出现102个错误 #include"mysql.h" #include<my_global.h> #include<stdio.h> #include<stdlib.h> staticMYSQL*conn; // 连接connection //初始化函数,主要是进行数据库连接等工作 void init(){ char*server= "localhost"; // 数据库所在主机的ip char*user = "root"; // 数据库的root用户 char*password= "huachao"; //root用户的密码 char*database= "test"; // 操作的数据库名 //连接到mysql数据库 conn=mysql_init(NULL); //根据具体的配置信息链接数据库. if(!mysql_real_connect(conn , server, user, password, database, 0, NULL,0)) { printf("Conection error : %s\n",mysql_error(conn )); exit(1); } } //通过sql语句进行插入数据 bool insertBySQL(char*str){ /**//*执行插入语句*/ if(mysql_query(conn,str)){ printf("执行插入失败"); returnfalse; }else{ printf("插入成功,受影响行数:%lu\n",(ulong)mysql_affected_rows(conn)); returntrue; } } //通过sql语句来对数据库表进行修改数据 void updateBySQL(char*sql){ /**//*执行插入语句*/ if(mysql_query(conn,sql)){ printf("修改失败"); }else{ printf("修改成功,受影响行数:%lu\n",(ulong)mysql_affected_rows(conn)); } } //根据sql语句来对数据库表进行查询 void getBySQL(char *sql,intcolNum){ MYSQL_RES *res; //结果 MYSQL_ROW row; //结果行(一行一行) //将查询发送到数据库 if(mysql_query(conn, sql)) { printf("MySQL query error : %s\n",mysql_error(conn)); exit(1); } res=mysql_use_result(conn); while((row = mysql_fetch_row(res)) !=NULL) { printf("%s\t%s\t%s\n", row[0],row[1],row[2]); } mysql_free_result(res); } void deleteBySQL(char*sql){ if(mysql_query(conn,sql)){ printf("删除失败"); }else{ printf("删除成功,受影响行数:%lu\n",(ulong)mysql_affected_rows(conn)); } } int_tmain(intargc,_TCHAR* argv[]) { init(); //insertBySQL("insertinto test(name,password) values('hh','22')"); //getBySQL("select* from test",2); //updateBySQL("updatetest set name='lisi' where id=2"); deleteBySQL("delete from test where id=5"); mysql_close(conn); getchar(); return0; }
出现如下错误:
error LNK2019: 无法解析的外部符号 _mysql_real_connect@32,该符号在函数 _main 中被引用
error LNK2019: 无法解析的外部符号 _mysql_query@8,该符号在函数 _main 中被引用
error LNK2019: 无法解析的外部符号 _mysql_init@4,该符号在函数 _main 中被引用
error LNK2019: 无法解析的外部符号 _mysql_close@4,该符号在函数 _main 中被引用
如果按照之前的步骤还会出现这种错误,可能是因为新建的项目是win32,而操作系统和mysql软件都市64位的,此时如下解决:
右击项目->属性->配置管理器,如图:
然后在“活动解决方案平台”点击下拉,“新建”选择x64即可