Windows下命令行编译链接mysql的C程序

复制mysql安装目录下的include,lib 到当前源代码目录


cl /c /I include main.c

link /LIBPATH:lib main.obj

最后复制libmysql.dll 和可执行文件在同一路径下即可正常运行

@del /f main.obj main.exe
cl /c /I include main.c
link /LIBPATH:lib main.obj

#include <stdio.h>
#include <stdlib.h>
#include <my_global.h>
#include <mysql.h>

#pragma comment(lib, "libmysql")

enum ERROR{ MYSQL_INIT_ERROR = 1};
MYSQL mysql, *sock;

int initMysql(){
	mysql_init(&mysql);
	sock = mysql_real_connect(&mysql,"localhost","root","rootpass","temp",0,NULL,0);
	if( !sock ){
		 return MYSQL_INIT_ERROR;
	}
	return 0;
}

int main( int argc, char **argv[]){
	int iRet = 0;
	iRet = initMysql();
	if( iRet == MYSQL_INIT_ERROR){
		fprintf(stderr, "mysql init error:%s\n", mysql_error(&mysql) );
		exit(0);
	}else{
		printf("Next Step to mysql\n");
    }
    mysql_close(sock);
	return 0;
}
 
author:mooring
date:2012/07/20 

你可能感兴趣的:(c mysql)