MFC中如何用C API连接MYSQL在控制台中连接成功的代码如下
[code]
#include "stdafx.h"#include <windows.h>
#include <iostream>
#include <mysql.h>
#include <stdio.h>
using namespace std;
int main()
{
//connection params
FILE *file;
char *host = "localhost";
char *user = "root";
char *pass = "123456";
char *db = "mysql";
//sock
MYSQL *sock;
MYSQL_RES *results;
MYSQL_ROW record;
sock = mysql_init(0);
if (sock) cout << "sock handle ok!" << endl;
else {
cout << "sock handle failed!" << mysql_error(sock) << endl;
}
//connection
if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
cout << "connection ok!" << endl;
else {
cout << "connection fail: " << mysql_error(sock) << endl;
}
mysql_set_character_set(sock,"gb2312");
//connection character set
cout << "connection character set: " << mysql_character_set_name(sock) << endl;
//wait for posibility to check system/mysql sockets
if(mysql_query(sock,"select * from admin_user"))
{
cout<<"very good!"<<endl;
}
results=mysql_store_result(sock);
printf("用户名\t等级\n");
while(record=mysql_fetch_row(results))
{
printf("%s\t%s \n",record[1],record[3]);
}
mysql_free_result(results);
system("PAUSE");
//closing connection
mysql_close(sock);
return EXIT_SUCCESS;
}
[/code]
具体的编译连接环境如下: 我的mysql安装路径为d:\mysql
所以要在VC中设置include路径和lib的路径。添加MySql的include目录到VC工作台中
Project->Settings->C/C++->Category->Preprocessor->Additional include directories中添加:D:\mysql5\include。
添加lib的路径:Tools->Options->Directories中选择Library files,然后添加lib的目录:D:\MYSQL\LIB
然后在Project->Settings->Link中,添加libmysql.lib到Object/libray modules中。还要确保Project Options中为/subsystem:console而不是/subsystem:window,否则编译错误
在MFC中该如何使用这些代码呢?
分不多的话可以再加
[code]
#include "stdafx.h"#include <windows.h>
#include <iostream>
#include <mysql.h>
#include <stdio.h>
using namespace std;
int main()
{
//connection params
FILE *file;
char *host = "localhost";
char *user = "root";
char *pass = "123456";
char *db = "mysql";
//sock
MYSQL *sock;
MYSQL_RES *results;
MYSQL_ROW record;
sock = mysql_init(0);
if (sock) cout << "sock handle ok!" << endl;
else {
cout << "sock handle failed!" << mysql_error(sock) << endl;
}
//connection
if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
cout << "connection ok!" << endl;
else {
cout << "connection fail: " << mysql_error(sock) << endl;
}
mysql_set_character_set(sock,"gb2312");
//connection character set
cout << "connection character set: " << mysql_character_set_name(sock) << endl;
//wait for posibility to check system/mysql sockets
if(mysql_query(sock,"select * from admin_user"))
{
cout<<"very good!"<<endl;
}
results=mysql_store_result(sock);
printf("用户名\t等级\n");
while(record=mysql_fetch_row(results))
{
printf("%s\t%s \n",record[1],record[3]);
}
mysql_free_result(results);
system("PAUSE");
//closing connection
mysql_close(sock);
return EXIT_SUCCESS;
}
[/code]
具体的编译连接环境如下: 我的mysql安装路径为d:\mysql
所以要在VC中设置include路径和lib的路径。添加MySql的include目录到VC工作台中
Project->Settings->C/C++->Category->Preprocessor->Additional include directories中添加:D:\mysql5\include。
添加lib的路径:Tools->Options->Directories中选择Library files,然后添加lib的目录:D:\MYSQL\LIB
然后在Project->Settings->Link中,添加libmysql.lib到Object/libray modules中。还要确保Project Options中为/subsystem:console而不是/subsystem:window,否则编译错误
在MFC中该如何使用这些代码呢?
分不多的话可以再加
解决方案 »
- 用VC制作按键精灵,请高手指点下自动寻怪
- 数据结构交流群83889943
- 关于CString赋值问题的问题!谢谢
- 使对话框透明产生背景遗留的问题
- VC++ .NET 2002右侧的资源视图,类视图和下面的输出窗口是如何实现的?
- 我看到老梁的书,说不要在debug下调试,而要在realease下调,为什么啊,我一直都在debug下调的啊
- ADO连接SQL SERVER
- 如何sql语句中引用变量
- 我上不了AMD公司网站,上 Intel 的站点速度也极慢,这是我自己的问题还是电信故意搞鬼实行愚民政策?
- 哪位前辈帮我看一下程序
- ********继续散20分,第10个人进来后结贴,为的是细水长流**********
- 在msscript.ocx中使用window.event对象???急 在线等
下面是代码
#include "stdafx.h"#include <windows.h>
#include <iostream>
#include <mysql.h>
#include <stdio.h>
using namespace std;
int main()
{
//connection params
FILE *file;
char *host = "localhost";
char *user = "root";
char *pass = "123456";
char *db = "movee";
//sock
MYSQL *sock;
MYSQL_RES *results;
MYSQL_ROW record;
sock = mysql_init(0);
if (sock) cout << "sock handle ok!" << endl;
else {
cout << "sock handle failed!" << mysql_error(sock) << endl;
}
//connection
if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
cout << "connection ok!" << endl;
else {
cout << "connection fail: " << mysql_error(sock) << endl;
}
mysql_set_character_set(sock,"gb2312");
//connection character set
cout << "connection character set: " << mysql_character_set_name(sock) << endl;
//wait for posibility to check system/mysql sockets
if(mysql_query(sock,"select * from admin_user"))
{
cout<<"very good!"<<endl;
}
results=mysql_store_result(sock);
printf("用户名\t等级\n");
while(record=mysql_fetch_row(results))
{
printf("%s\t%s \n",record[1],record[3]);
}
mysql_free_result(results);
system("PAUSE");
//closing connection
mysql_close(sock);
return EXIT_SUCCESS;
}
这个在MFC中去掉。
我用过,只需要设置头文件路径,lib路径,包含头文件和lib就可以用了。
是不是直接把 /subsystem:window删除,谢谢。请教下详细的方法。
是用类的还是如上直接建一个新的mysql.cpp文件直接使用
还是在oninitDialog的下面直接使用,谢谢!
#include "mysql.h"
#pragama comment(lib,"libmysql") 最多也再#include <winsock2.h> #pragma comment(lib,"ws2_32.lib")然后一样的用啊
d:\program files\microsoft visual studio\vc98\include\mysql_com.h(183) : error C2501: 'SOCKET' : missing storage-class or type specifiers
d:\program files\microsoft visual studio\vc98\include\mysql_com.h(183) : error C2501: 'fd' : missing storage-class or type specifiers
d:\program files\microsoft visual studio\vc98\include\mysql_com.h(358) : error C2065: 'SOCKET' : undeclared identifier
d:\program files\microsoft visual studio\vc98\include\mysql_com.h(358) : error C2146: syntax error : missing ')' before identifier 's'
d:\program files\microsoft visual studio\vc98\include\mysql_com.h(359) : error C2059: syntax error : ')'\submithelpDlg.cpp(180) : error C2059: syntax error : '<'
\submithelpDlg.cpp(182) : error C2059: syntax error : '<'
\submithelpDlg.cpp(186) : error C2059: syntax error : '<'
\submithelpDlg.cpp(192) : error C2059: syntax error : '<'
submithelpDlg.cpp(197) : error C2059: syntax error : '<'
Error executing cl.exe.调试出错好像是链接库出错,但在控制台是这样设置的这个不论更改与否,都出同样的错。
Options中为/subsystem:console而不是/subsystem:window,否则编译错误
#include"mysql.h"
之前加入
#include"winsock.h"(记不清是不是这个头文件了,有点相似)
加在#include"mysql.h"之前,只不过出错更多。
#include "winsock.h"
#include "mysql.h"
#include <stdio.h>上面的
ubmithelpDlg.cpp(180) : error C2059: syntax error : ' <'
\submithelpDlg.cpp(182) : error C2059: syntax error : ' <'
\submithelpDlg.cpp(186) : error C2059: syntax error : ' <'
\submithelpDlg.cpp(192) : error C2059: syntax error : ' <'
submithelpDlg.cpp(197) : error C2059: syntax error : ' <'
Error executing cl.exe.
这些错误是因为是复制粘贴的,所以cout 与“< <”之间分隔太大
或有全角字符在内,重新编辑下就可以了现在是LINK出错。。即subsystem:window出错。和一个File警告。warning C4101: 'file' : unreferenced local variable
Linking...
LINK : fatal error LNK1117: syntax error in option "subsystem:window"
Error executing link.exe.help.exe - 1 error(s), 2 warning(s)
libcmtd.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/submithelp.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.help.exe - 2 error(s), 0 warning(s)又出错如下
改为subsystem:window即删除/incremental:yes 调试成功,但运行时,提示内存读取错误,真麻烦呢
"0x0000000c"内存,该内存不能为“
"read"要终止程序,请单击“确定”
要调试程序,请单击“取消”检查内存分配是否成功,
int* pInt = new[500];
if( !pInt )
{ AfxmessageBox("内存分配不成功");}
while(record=mysql_fetch_row(results))把整个循环注释掉可以运行。
再改回来while语句也可以
真奇怪
设置mysql的路径后,把dll和lib拷到工程下
在需要的地方添加
#include <winsock2.h>
#include <mysql.h>再在Alt+F7中Library files里添加
ws2_32.lib libmysql.lib
即可至于工程类型是windows还是console都可以的
#include <mysql.h>
#include <winsock.h>
#pragma comment( lib, "libmysql.lib");
//此句话和在附加依赖项中增加libmysql.lib 的功能一样
usingnamespace std;
int main(char **args)
{
MYSQL mysql;
mysql_init(&mysql);
if (mysql_real_connect(&mysql,"localhost","root","","test",3306,0,0))
{
cout<<"ok"<<endl;
return 0;
}
else
{
int i = mysql_errno(&mysql);
constchar * s = mysql_error(&mysql);
cout << s<<endl;
}
}
相关设置:
1、附加包含头文件的目录,include就是mysql-5.0.27-win32\Include文件夹。
2、附加库目录,MySql lib中的文件就是mysql-5.0.27-win32\lib\opt中的文件
3、附加依赖项,名称为libmysql.lib
4、将libmysql.dll拷贝到debug文件夹中,libmysql.dll在lib文件夹中有
作个总结,备忘
#include <windows.h>
#include <iostream>
#include <mysql.h>
#include <stdio.h>
using namespace std;
int main()
{
//connection params
FILE *file;
char *host = "localhost";
char *user = "root";
char *pass = "yourpassword";
char *db = "mysql";
//sock
MYSQL *sock;
MYSQL_RES *results;
MYSQL_ROW record;
sock = mysql_init(0);
if (sock) cout << "sock handle ok!" << endl;
else {
cout << "sock handle failed!" << mysql_error(sock)<< endl;
}
//connection
if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
cout << "connection ok!" << endl;
else {
cout << "connection fail: " << mysql_error(sock)<< endl;
}
mysql_set_character_set(sock,"gb2312");
//connection character set
cout << "connection character set: " << mysql_character_set_name(sock) << endl;
//wait for posibility to check system/mysql sockets
if(mysql_query(sock,"select * from admin_user"))
{
cout <<"very good!" <<endl;
}
results=mysql_store_result(sock);
printf("User\Level\n");
while(record=mysql_fetch_row(results))
{
printf("%s\t%s \n",record[1],record[3]);
}
mysql_free_result(results);
system("PAUSE");
//closing connection
mysql_close(sock);
return EXIT_SUCCESS;
}
具体的编译连接环境如下: 我的mysql安装路径为d:\mysql
所以要在VC中设置include路径和lib的路径。
一、添加MySql的include目录到VC工作台中
Project-> Settings-> C/C++-> Category-> Preprocessor-> Additional include directories中添加:D:\mysql\include。
二、添加lib的路径:Tools-> Options-> Directories中选择Library files,然后添加lib的目录:D:\MYSQL\LIB
然后在Project-> Settings-> Link中,添加libmysql.lib到Object/libray modules中。三、还要确保Project Options中为/subsystem:console或是/subsystem:window,否则编译错误 (这一项不太重要,如果在控制台中用subsystem:console
在MFC中用/subsystem:window
这句中的
printf("%s\t %s\n",record[1],record[3]); %s和\t 连在一起了。下面就完美了#include <windows.h>
#include <iostream>
#include <mysql.h>
#include <stdio.h>
using namespace std;
int main()
{
//connection params
FILE *file;
char *host = "localhost";
char *user = "root";
char *pass = "yourpassword";
char *db = "mysql";
//sock
MYSQL *sock;
MYSQL_RES *results;
MYSQL_ROW record;
sock = mysql_init(0);
if (sock) cout << "sock handle ok!" << endl;
else {
cout << "sock handle failed!" << mysql_error(sock)<< endl;
}
//connection
if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
cout << "connection ok!" << endl;
else {
cout << "connection fail: " << mysql_error(sock)<< endl;
}
mysql_set_character_set(sock,"gb2312");
//connection character set
cout << "connection character set: " << mysql_character_set_name(sock) << endl;
//wait for posibility to check system/mysql sockets
if(mysql_query(sock,"select * from admin_user"))
{
cout <<"very good!" <<endl;
}
results=mysql_store_result(sock);
printf("User\Level\n");
while(record=mysql_fetch_row(results))
{
printf("%s\t %s\n",record[1],record[3]);
}
mysql_free_result(results);
system("PAUSE");
//closing connection
mysql_close(sock);
return EXIT_SUCCESS;
}
具体的编译连接环境如下: 我的mysql安装路径为d:\mysql
所以要在VC中设置include路径和lib的路径。
一、添加MySql的include目录到VC工作台中
Project-> Settings-> C/C++-> Category-> Preprocessor-> Additional include directories中添加:D:\mysql\include。
二、添加lib的路径:Tools-> Options-> Directories中选择Library files,然后添加lib的目录:D:\MYSQL\LIB
然后在Project-> Settings-> Link中,添加libmysql.lib到Object/libray modules中。 三、还要确保Project Options中为/subsystem:console或是/subsystem:window,否则编译错误 (这一项不太重要,如果在控制台中用subsystem:console
在MFC中用/subsystem:window