C++对Mysql数据库的访问查询(基于Mysql5.0的API,vs2010中操作
程序员文章站
2024-01-11 20:36:58
...
右击项目名称-属性,配置引用Mysql数据库的库目录,见图: 添加附加包目录为C:\Program Files\MySQL\MySQL Server 5.0\include 拷贝libmySQL.dll(C:\Program Files\MySQL\MySQL Server 5.0\bin)和libmysql.lib(C:\Program Files\MySQL\MySQL Server 5.0\lib
右击项目名称->属性,配置引用Mysql数据库的库目录,见图:
添加附加包目录为C:\Program Files\MySQL\MySQL Server 5.0\include
拷贝libmySQL.dll(C:\Program Files\MySQL\MySQL Server 5.0\bin)和libmysql.lib(C:\Program Files\MySQL\MySQL Server 5.0\lib\opt)到运行目录下,如调试时放在Debug下
然后代码如下
#include "windows.h" #include "winsock.h" #include "stdio.h" #include "mysql.h" #pragma comment(lib,"libmySQL.lib") #includeusing namespace std; int main() { char dbuser[30]="root"; char dbpasswd[30]="password"; char dbip[30]="localhost"; char dbname[50]="temperature"; MYSQL *con=mysql_init((MYSQL *)0); if (con!=NULL&&mysql_real_connect(con,dbip,dbuser,dbpasswd,dbname,3306,NULL,0)) { printf("SUCCESSFULLY."); mysql_query(con,"select * from temperature"); MYSQL_RES *result=mysql_store_result(con); unsigned int fieldcount=mysql_num_fields(result); coutname