嵌入式mysql_libmysqld,嵌入式MySQLserver库

25.1.6. 嵌入式server演示样例

在Linux或FreeBSD系统上,无需更改就能使用以下这两个演示样例程序。对于其它操作系统,须要进行小的改动,主要是文件路径。设计这两个演示样例的目的在于,为你提供足够的细节信息。以便理解问题,它们是实际应用程序的必要组成部份。第1个演示样例十分直观。第2个演示样例採用了一些错误检查功能。略为复杂。在第1个演示样例的后面,给出了用于编译程序的命令行条目。

在第2个演示样例的后面,给出了GNUmake文件,该文件可用于编译。

演示样例:1

test1_libmysqld.c

#include

#include

#include

#include "mysql.h"

MYSQL *mysql;

MYSQL_RES *results;

MYSQL_ROW record;

static char *server_options[] = { "mysql_test", "--defaults-file=my.cnf" };

int num_elements = sizeof(server_options)/ sizeof(char *);

static char *server_groups[] = { "libmysqld_server", "libmysqld_client" };

int main(void)

{

mysql_server_init(num_elements, server_options, server_groups);

mysql = mysql_init(NULL);

mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");

mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);

mysql_real_connect(mysql, NULL,NULL,NULL, "database1", 0,NULL,0);

mysql_query(mysql, "SELECT column1, column2 FROM table1");

results = mysql_store_result(mysql);

while((record = mysql_fetch_row(results))) {

printf("%s - %s \n", record[0], record[1]);

}

mysql_free_result(results);

mysql_close(mysql);

mysql_server_end();

return 0;

}

以下给出了编译上述程序的命令行命令:

gcc test1_libmysqld.c -o test1_libmysqld -lz \

`/usr/local/mysql/bin/mysql_config --include --libmysqld-libs`

演示样例:2

要想检验该演示样例。创建一个与MySQL源文件夹同级的test2_libmysqld文件夹。

将test2_libmysqld.c源文件和GNUmakefile保存到该文件夹。并在test2_libmysqld文件夹下执行GNUmake。

test2_libmysqld.c

/*

* A simple example client, using the embedded MySQL server library

*/

#include

#include

#include

#include

MYSQL *db_connect(const char *dbname);

void db_disconnect(MYSQL *db);

void db_do_query(MYSQL *db, const char *query);

const char *server_groups[] = {

"test2_libmysqld_SERVER", "embedded", "server", NULL

};

int

main(int argc, char **argv)

{

MYSQL *one, *two;

/* mysql_server_init() must be called before any other mysql

* functions.

*

* You can use mysql_server_init(0, NULL, NULL), and it

* initializes the server using groups = {

*   "server", "embedded", NULL

*  }.

*

* In your $HOME/.my.cnf file, you probably want to put:

[test2_libmysqld_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, (char **)server_groups);

one = db_connect("test");

two = db_connect(NULL);

db_do_query(one, "SHOW TABLE STATUS");

db_do_query(two, "SHOW DATABASES");

mysql_close(two);

mysql_close(one);

/* This must be called after all other mysql functions */

mysql_server_end();

exit(EXIT_SUCCESS);

}

static void

die(MYSQL *db, char *fmt, ...)

{

va_list ap;

va_start(ap, fmt);

vfprintf(stderr, fmt, ap);

va_end(ap);

(void)putc('\n', stderr);

if (db)

db_disconnect(db);

exit(EXIT_FAILURE);

}

MYSQL *

db_connect(const char *dbname)

{

MYSQL *db = mysql_init(NULL);

if (!db)

die(db, "mysql_init failed: no memory");

/*

* Notice that the client and server use separate group names.

* This is critical, because the server does not accept the

* client's options, and vice versa.

*/

mysql_options(db, MYSQL_READ_DEFAULT_GROUP, "test2_libmysqld_CLIENT");

if (!mysql_real_connect(db, NULL, NULL, NULL, dbname, 0, NULL, 0))

die(db, "mysql_real_connect failed: %s", mysql_error(db));

return db;

}

void

db_disconnect(MYSQL *db)

{

mysql_close(db);

}

void

db_do_query(MYSQL *db, const char *query)

{

if (mysql_query(db, query) != 0)

goto err;

if (mysql_field_count(db) > 0)

{

MYSQL_RES   *res;

MYSQL_ROW    row, end_row;

int num_fields;

if (!(res = mysql_store_result(db)))

goto err;

num_fields = mysql_num_fields(res);

while ((row = mysql_fetch_row(res)))

{

(void)fputs(">> ", stdout);

for (end_row = row + num_fields; row < end_row; ++row)

(void)printf("%s\t", row ? (char*)*row : "NULL");

(void)fputc('\n', stdout);

}

(void)fputc('\n', stdout);

mysql_free_result(res);

}

else

(void)printf("Affected rows: %lld\n", mysql_affected_rows(db));

return;

err:

die(db, "db_do_query failed: %s [%s]", mysql_error(db), query);

}

GNUmakefile

# This assumes the MySQL software is installed in /usr/local/mysql

inc      := /usr/local/mysql/include/mysql

lib      := /usr/local/mysql/lib

# If you have not installed the MySQL software yet, try this instead

#inc      := $(HOME)/mysql-5.1/include

#lib      := $(HOME)/mysql-5.1/libmysqld

CC       := gcc

CPPFLAGS := -I$(inc) -D_THREAD_SAFE -D_REENTRANT

CFLAGS   := -g -W -Wall

LDFLAGS  := -static

# You can change -lmysqld to -lmysqlclient to use the

# client/server library

LDLIBS    = -L$(lib) -lmysqld -lz -lm -lcrypt

ifneq (,$(shell grep FreeBSD /COPYRIGHT 2>/dev/null))

# FreeBSD

LDFLAGS += -pthread

else

# Assume Linux

LDLIBS += -lpthread

endif

# This works for simple one-file test programs

sources := $(wildcard *.c)

objects := $(patsubst %c,%o,$(sources))

targets := $(basename $(sources))

all: $(targets)

clean:

rm -f $(targets) $(objects) *.core

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值