http://colorado.javaeye.com/category/48736?show_full=trueC/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 (網友代碼修改版)
#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 瞭解不多,這段不知道何意,先記下來。