Finally, the experiment program was successfully started to enable embedded MySQL Server (C/C ++)

Source: Internet
Author: User

Today, someone happened to ask, saying that the Code server_init is always failed. I don't believe it. I tried it, and it was a great deal of money.

If you are interested, try it on your own. It is quite interesting.

My machine originally had a decompressed version of mysql5.0.9, located in D:/Program/mysql-5.0.9-beta-win32, the default storage engine is InnoDB

1. To facilitate testing, first create a test table and insert several records. The engine is set to MyISAM.

create table t2(id int primary key, col2 varchar(32)) engine=MyISAM;

 

2. Create a configuration file for the embed server,

D:/Program/mysql-5.0.9-beta-win32/Embedded/My. INI, it is worth mentioning that the name of server must be consistent with the name of your server program. Here, embedmysqlserver is the name of the EXE program behind me, otherwise the server will never start.

The content is as follows:

[EmbedMySQLServer]basedir = D:/program/mysql-5.0.9-beta-win32datadir = D:/program/mysql-5.0.9-beta-win32/datalanguage = D:/program/mysql-5.0.9-beta-win32/share/englishskip-innodbport=3306[libmysqld_client]language = D:/program/mysql-5.0.9-beta-win32/share/englishport=3306

 

3. I started writing my own code.

Create embedmysqlserver.exe, source code like:

 

    // #define _WIN32_WINNT 0x0400    #include <windows.h>    #include <stdio.h>    #include <stdlib.h>    #include <stdarg.h>    #include "mysql.h"    MYSQL *mysql;    MYSQL_RES *results;    MYSQL_ROW record;    #pragma comment(lib, "D://program//mysql-5.0.9-beta-win32//Embedded//DLL//debug//libmysqld.lib")    static char *server_options[] = { "mysql_test", "--defaults-file=D:/program/mysql-5.0.9-beta-win32/Embedded/my.ini" };    int num_elements = sizeof(server_options)/ sizeof(char *);    static char *server_groups[] = { "EmbedMySQLServer", "libmysqld_client" };    int main(void)    {       int ret = mysql_server_init(num_elements, server_options, server_groups);       printf("return %ld/n", ret);       mysql = mysql_init(NULL);       mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");       mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);       MYSQL* t = mysql_real_connect(mysql, NULL,"test","test", "test", 0,NULL,0);              mysql_query(mysql, "SELECT id, col2 FROM t2");       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;    }

4. Final running result:

return 01 - test2 - test3 - test4 - test5 - fdasPress any key to continue

 

Like this kind of stuff, MySQL online Doc has not been well said, it is recommended to buy its commercial license, it seems that more hands-on, it is quite interesting.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.