本文介绍了MySQL C API:在* Embedded *模式下启动DB的需要示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C 工具,在该工具中,我需要集中处理和查询过程内部数据.

I work on a C tool, in which I need to manipulate and query process-internal data intensively.

因此,我决定在嵌入式模式下使用 MySQL C API ,因此我可以为我的工具的每个运行过程使用一个嵌入式数据库,并使用上面的所有SQL功能.

Hence, I've decided to use the MySQL C API on Embedded mode, so I can have an embedded DB for each running process of my tool, and use all the SQL features on it.

我在网上找到的所有 MySQL C API 教程都是关于连接到正在运行的服务器的,这不是我的情况.

All the MySQL C API tutorials I've found over the web, deal with connecting to a running server, which is not my case.

在Oracle的MySQL网站中有一些使用 Embedded 模式的示例,但是它们并不简单,很难使它们工作.

There are some examples for working with Embedded mode in Oracle's MySQL website, but they are not simple, and it's hard to make them work.

问:有人可以指点我还是给我写一个简短的示例,说明如何在嵌入式模式下使用 MySQL C API 初始化数据库?

Q: Can someone please point me or write me a short example for initiating a DB using MySQL C API on Embedded mode?

谢谢!

推荐答案

经过多次尝试,终于对自己回答并与您分享:

After a lot of tries, finally answering to myself and sharing it with you:

  • 创建一个用于存储数据库的中央数据目录.在此示例中,我将使用/tmp/mysql_embedded_data

> mkdir /tmp/mysql_embedded_data

  • 创建一个C文件,如以下示例所示:
#include <my_global.h>
#include <mysql.h>

int main(int argc, char **argv) {  
    static char *server_options[] = {
                                   "mysql_test", // An unused string 
                                    "--datadir=/tmp/mysql_embedded_data", // Your data dir
                                    NULL };
    int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;

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


    // Init MySQL lib and connection    
    mysql_library_init(num_elements, server_options, server_groups);
    MYSQL *con = mysql_init(NULL);

    if (con == NULL) {
        fprintf(stderr, "%s\n", mysql_error(con));
        exit(1);
    }

    mysql_options(con, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
    mysql_options(con, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);

    // Connect to no host/port -> Embedded mode
    if (mysql_real_connect(con, NULL, NULL, NULL, NULL, 0, NULL, 0) == NULL) {
        fprintf(stderr, "%s\n", mysql_error(con));
        mysql_close(con);
        exit(1);
    }

    // Create a sample empty DB, named "aNewDatabase"
    if (mysql_query(con, "CREATE DATABASE aNewDatabase")) {
        fprintf(stderr, "%s\n", mysql_error(con));
        mysql_close(con);
        exit(1);
    }

    // Close connection
    mysql_close(con);
    exit(0);
}

  • 编译您的文件(在本示例中,我假设我的C文件名是/tmp/mysql_try.c)
    • Compile your file (I assume in this example that my C filename is /tmp/mysql_try.c)
    • gcc /tmp/mysql_try.c -o /tmp/mysql_try -lz `mysql_config --include --libmysqld-libs`

      • 运行已编译的程序:

      > /tmp/mysql_try

      > /tmp/mysql_try

      • 程序返回时,请确保成功创建了数据库.我们将样本数据库命名为aNewDatabase,因此我们将检查它是否在我们创建的数据目录中现在有目录->我们将检查目录/tmp/mysql_embedded_data/aNewDatabase是否已创建:
      • When your program returnes, ensure that the DB creation succeeded. We named our sample DB aNewDatabase, so we'll check if it now has a directory inside the data directory we created -> We'll check if the directory /tmp/mysql_embedded_data/aNewDatabase was created:

      > ls /tmp/mysql_embedded_data/aNewDatabasedb.opt

      > ls /tmp/mysql_embedded_data/aNewDatabasedb.opt

      • 享受!

      这篇关于MySQL C API:在* Embedded *模式下启动DB的需要示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 01:25