日期:2014-05-16  浏览次数:20431 次

linux连接mysql数据库

linux连接mysql数据库,可以参考这个:http://forum.ubuntu.org.cn/viewtopic.php?f=44&t=166604

?

/*
 * test.c
 *
 *  Created on: 2011-8-23
 *      Author: chenhaiyu
 */

#include <stdio.h>
#include <stdlib.h>
#include "mysql/mysql.h"

int main() {
	MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	conn = mysql_init(NULL);
	char *server_host = "localhost";
	char *sql_user_name = "root";
	char *sql_password = "root";
	char *db_name = "test";

	if(!mysql_real_connect(conn, server_host, sql_user_name, sql_password, db_name, 0, NULL, 0)) {
		fprintf(stderr, "%s\n", mysql_error(conn));
		exit(EXIT_FAILURE);
	}

	if(mysql_query(conn, "select *from userinfo")) {
		fprintf(stderr, "%s\n", mysql_error(conn));
		exit(EXIT_FAILURE);
	}

	res = mysql_use_result(conn);
	printf("mysql table in mysql databases: \n");
	while ((row = mysql_fetch_row(res))!=NULL)
		printf("%s %s %s\n", row[0], row[1], row[2]);

	mysql_close(conn);
	exit(EXIT_SUCCESS);
}

?编译注意事项,加上-l mysqlclient,具体原因正在跟进中: