MySQL數(shù)據(jù)庫創(chuàng)建.修改和刪除表操作實(shí)例介紹
2023-11-23
更新時(shí)間:2023-11-23 00:12:28作者:未知
今天,碰巧有人問,說寫的代碼server_init,老是失敗,我不信,試了一番,大費(fèi)周折,總算成功。
有興趣的,不妨自己動(dòng)手一試,蠻有意思的。
我機(jī)器上原本有一個(gè)解壓縮版的MySQL5.0.9,位于D:/program/mysql-5.0.9-beta-win32,默認(rèn)存儲(chǔ)引擎是InnoDB
1. 為便于測(cè)試,首先建立一個(gè)測(cè)試表,并插入幾條記錄,引擎定為MyISAM
create table t2(id int primary key, col2 varchar(32)) engine=MyISAM;
2. 創(chuàng)建EmbedServer的配置文件,
D:/program/mysql-5.0.9-beta-win32/Embedded/my.ini,值得一提的是,差點(diǎn)把我害慘了,就是Server那一項(xiàng)的名字必須與你的Server程序的名字保持一致。這里,EmbedMySQLServer就是我后邊的exe程序的名,否則server永遠(yuǎn)也啟不來。
內(nèi)容如下:
[EmbedMySQLServer]
basedir = D:/program/mysql-5.0.9-beta-win32
datadir = D:/program/mysql-5.0.9-beta-win32/data
language = D:/program/mysql-5.0.9-beta-win32/share/english
skip-innodb
port=3306
[libmysqld_client]
language = D:/program/mysql-5.0.9-beta-win32/share/english
port=3306
3. 開始寫自己的代碼了
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. 最終運(yùn)行結(jié)果:
return 0
1 - test
2 - test
3 - test
4 - test
5 - fdas
Press any key to continue
像這類東東,MySQL Online Doc都沒好好說,它都推薦買它的商業(yè)license,看來自己多動(dòng)動(dòng)手,也蠻有意思的。