linux-mysql 连接1

//simple_mysql_api.c
#include 
#include 
#include 

int main(int argc, char *argv[]){

	MYSQL* conn_ptr;
	conn_ptr = mysql_init(NULL);

	if(!conn_ptr){
		fprintf(stderr, "mysql init failed");
		return EXIT_FAILURE;
	}

	const char* host = "localhost";
	const char* user = "root";
	const char* password = "pswd";
	const char* dbname = "chat";
	conn_ptr = mysql_real_connect(conn_ptr,host, user, password,dbname,0,NULL,0);

	if(conn_ptr){
		printf("Connect mysql succeed!\n");
	}else{
		printf("Connect mysql failed!\n");
	}
	mysql_close(conn_ptr);
	return 0;

}

编译:

$ g++ -o mysql-test simple_mysql_api.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient

$./mysql-test



你可能感兴趣的:(数据库,Linux)