http://colorado.javaeye.com/category/48736?show_full=true
C/C++ 连接 MySQL (GCC 版)
1、从 mysql-noinstall-5.1.30-win32.zip 包中解压 include, lib 两个目录到
使用C:\vcprojects\mysql-5.1.30 中
2、参考本博客《Windows XP下MinGW的安装与配置 》建好 Windows 下 GCC 的开发环境。
3、进入目录C:\vcprojects\mysql-5.1.30\lib\opt执行命令:
reimp -d libmysql.lib
dlltool -k -d libmysql.def -l libmysql.a
生成gcc所需链接的mysql库 libmysql
4、在C:\vcprojects\learn目录下创建文件 visitsql.c (网友代码修改版)
5、创建一个批处理文件 build.bat:
set MYSQL_CLIB=C:\vcprojects\mysql-5.1.30
set C_INCLUDE_PATH=%C_INCLUDE_PATH%;%MYSQL_CLIB%\include
set CPLUS_INCLUDE_PATH=%CPLUS_INCLUDE_PATH%;%MYSQL_CLIB%\include
set LIBRARY_PATH=%LIBRARY_PATH%;%MYSQL_CLIB%\lib\opt
g++ -o visitsql visitsql.c -llibmysql
执行该批处理,生成 visitsql.exe 执行该程序结果:
sock handle ok!
connection ok!
connection character set: gb2312
主机 用户名
localhost root
6、比较visitsql 大小:
gcc 版 488KB
vc++ debug 版 40KB
vc++ release 版 10KB
7、另从网上摘抄
如果你的dlltool产生的libmysql.a是空文件的话,如下操作:
cd {mysql}/lib/opt
reimp libmysql.lib
dlltool.exe -d {mysql++}/libmysqlclient.def -e mysqlclient.exp -l mysqlclient.a -S as
备注:
mingw-utils-0.3.tar.gz
binutils-2.17.50-20060824-1.tar.gz
//reimp mysqlclient.lib
//ar rcs libmysqlclient.a *.obj
//rm *.obj
我对 gcc 了解不多,这段不知道何意,先记下来。
使用C:\vcprojects\mysql-5.1.30 中
2、参考本博客《Windows XP下MinGW的安装与配置 》建好 Windows 下 GCC 的开发环境。
3、进入目录C:\vcprojects\mysql-5.1.30\lib\opt执行命令:
reimp -d libmysql.lib
dlltool -k -d libmysql.def -l libmysql.a
生成gcc所需链接的mysql库 libmysql
4、在C:\vcprojects\learn目录下创建文件 visitsql.c (网友代码修改版)
#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 = "12345"; 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 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[0],record[1]); } mysql_free_result(results); //system("PAUSE"); //closing connection mysql_close(sock); return EXIT_SUCCESS; }
5、创建一个批处理文件 build.bat:
set MYSQL_CLIB=C:\vcprojects\mysql-5.1.30
set C_INCLUDE_PATH=%C_INCLUDE_PATH%;%MYSQL_CLIB%\include
set CPLUS_INCLUDE_PATH=%CPLUS_INCLUDE_PATH%;%MYSQL_CLIB%\include
set LIBRARY_PATH=%LIBRARY_PATH%;%MYSQL_CLIB%\lib\opt
g++ -o visitsql visitsql.c -llibmysql
执行该批处理,生成 visitsql.exe 执行该程序结果:
sock handle ok!
connection ok!
connection character set: gb2312
主机 用户名
localhost root
6、比较visitsql 大小:
gcc 版 488KB
vc++ debug 版 40KB
vc++ release 版 10KB
7、另从网上摘抄
如果你的dlltool产生的libmysql.a是空文件的话,如下操作:
cd {mysql}/lib/opt
reimp libmysql.lib
dlltool.exe -d {mysql++}/libmysqlclient.def -e mysqlclient.exp -l mysqlclient.a -S as
备注:
mingw-utils-0.3.tar.gz
binutils-2.17.50-20060824-1.tar.gz
//reimp mysqlclient.lib
//ar rcs libmysqlclient.a *.obj
//rm *.obj
我对 gcc 了解不多,这段不知道何意,先记下来。