Эта программа должна работать без любых изменений на Linux или FreeBSD системе. Для других операционных систем будут необходимы маленькие изменения. Этот пример разработан, чтобы дать общее представление о том, как все делать.
Чтобы испытать пример, создайте каталог example в том же самом уровне, где лежит каталог с исходными текстами mysql-4.0. Сохраните в нем файлы example.c и GNUmakefile, после чего выполните GNU make из каталога example.
Файл example.c: /* * A simple example client, using the embedded MySQL server library */ #include <mysql.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h>
enum on_error {E_okay, E_warn, E_fail};
static void die(MYSQL *db, char *fmt, ...); MYSQL *db_connect(const char *dbname); void db_disconnect(MYSQL *db); void db_do_query(MYSQL *db, const char *query, enum on_error on_error);
const char *server_groups[] = { "test_client_SERVER", "server", NULL };
int main(int argc, char **argv) { MYSQL *one, *two;
/* This must be called before any other mysql functions. * * You can use mysql_server_init(0, NULL, NULL), and it will * initialize the server using groups = { "server", NULL }. * * In your $HOME/.my.cnf file, you probably want to put: [test_client_SERVER] language = /path/to/source/of/mysql/sql/share/english
* You could, of course, modify argc and argv before passing * them to this function. Or you could create new ones in any * way you like. But all of the arguments in argv (except for * argv[0], which is the program name) should be valid options * for the MySQL server. * * If you link this client against the normal mysqlclient * library, this function is just a stub that does nothing. */ mysql_server_init(argc, argv, server_groups); one = db_connect("test"); two = db_connect(NULL); db_do_query(one, "show table status", E_fail); db_do_query(two, "show databases", E_fail); mysql_close(two); mysql_close(one);
/* This must be called after all other mysql functions */ mysql_server_end(); exit(EXIT_SUCCESS); }