Home Database Mysql Tutorial MySQL嵌入式版本的小程序例子_MySQL

MySQL嵌入式版本的小程序例子_MySQL

Jun 01, 2016 pm 01:26 PM
Embedded program Record

bitsCN.com MySQL嵌入式版本越来越式微了。碰巧有个小应用用到了它,顺便记录一下。
文档可以参考http://dev.mysql.com/doc/refman/5.6/en/libmysqld-example.html,看起来年久失修的样子,没法一看就懂。
希望下面这个简化的步骤可以起到点作用。
1. 下载代码并且进行版本编译。这个还比较简单,文档说的也很靠谱。
$ cmake . -DCMAKE_INSTALL_PREFIX=/home/mysql -DWITH_EMBEDDED_SERVER=1
$ make clean$ make -j 8
$ make install

2. 写个小程序
$ ls
GNUmakefile main.cc my.cnf
具体代码就看附件好了,其实很简单。
$ make

3. 安装好MySQL,初始化一下
$ cd /home/mysql
$ scripts/mysql_install_db --defaults-file=/home/mysql/my.cnf

4. 启动小程序
$ ./main
附件:

# 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 insteadtopdir   := /home/mysql/mysql-5.5.35inc      := $(topdir)/includelib      := $(topdir)/libmysqldCXX      := g++CPPFLAGS := -I$(inc) -D_THREAD_SAFE -D_REENTRANTCXXFLAGS := -g -WallLDFLAGS  := # You can change -lmysqld to -lmysqlclient to use the# client/server libraryLDLIBS    = -L$(lib) -lmysqld -lm -lcrypt -ldl -lz -lrtifneq (,$(shell grep FreeBSD /COPYRIGHT 2>/dev/null))# FreeBSDLDFLAGS += -pthreadelse# Assume LinuxLDLIBS += -pthreadendif# This works for simple one-file test programssources := $(wildcard *.cc)objects := $(patsubst %cc,%o,$(sources))targets := $(basename $(sources))all: $(targets)clean:	rm -f $(targets) $(objects) *.core---#include <mysql.h>#include <iostream>#include <cassert>using namespace std;const char *server_options[] = /       { "mysql_test", "--defaults-file=my.cnf", NULL };int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;const char *server_groups[]= { "libmysqld_server",                               "libmysqld_client",                               NULL};bool is_server_started= false;MYSQL *MySQL= NULL;const char *db= NULL;void start_server(){  cout << "enter start_server()" << endl;  if (mysql_library_init(num_elements, (char **) server_options, (char **) server_groups)) {    is_server_started= false;    cout << "ERROR: start server failed." << endl;  }  else {    is_server_started= true;    cout << "INFO: start server OK." << endl;  }}void stop_server(){  cout << "enter stop_server()" << endl;  mysql_server_end();}bool connect_server(){  cout << "enter connect_server()" << endl;  bool rc = true;  MySQL = mysql_init(NULL);  if (!MySQL)    return rc;  if (mysql_real_connect(MySQL, NULL, NULL, NULL, db, 0, NULL, 0))    rc = false;   MySQL->reconnect= 1;  return rc;}void output_rows(MYSQL_RES *res){  MYSQL_ROW row;  unsigned long n = 0;  while ((row= mysql_fetch_row(res)) != 0)  {    mysql_field_seek(res, 0);    for (unsigned int i= 0 ; i < mysql_num_fields(res); i++)      cout << "row[" << n++ << "]:"<< row[i] << endl;   }}bool get_dbs(){  MYSQL_RES *res;  if (!is_server_started)    return true;  if (!(res= mysql_list_dbs(MySQL, "%")))    return true;  output_rows(res);  mysql_free_result(res);  return false;}int main(int argc, char *argv[]){  start_server();  if (is_server_started) {    cout << "server started." << endl;    if (!connect_server()) {      get_dbs();    }    stop_server();  }  return 0;}---[libmysqld_client][libmysqld_server]basedir=/home/mysql/datadir=/home/mysql/datatmpdir=/home/mysql/tmplog-error=/home/mysql/alert.loglc_messages_dir=/home/mysql/shareinnodb_data_home_dir=/home/mysql/datainnodb_log_group_home_dir=/home/mysql/data
Copy after login


bitsCN.com

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Mar 12, 2024 pm 07:20 PM

Pinduoduo software provides a lot of good products, you can buy them anytime and anywhere, and the quality of each product is strictly controlled, every product is genuine, and there are many preferential shopping discounts, allowing everyone to shop online Simply can not stop. Enter your mobile phone number to log in online, add multiple delivery addresses and contact information online, and check the latest logistics trends at any time. Product sections of different categories are open, search and swipe up and down to purchase and place orders, and experience convenience without leaving home. With the online shopping service, you can also view all purchase records, including the goods you have purchased, and receive dozens of shopping red envelopes and coupons for free. Now the editor has provided Pinduoduo users with a detailed online way to view purchased product records. method. 1. Open your phone and click on the Pinduoduo icon.

How to write a simple countdown program in C++? How to write a simple countdown program in C++? Nov 03, 2023 pm 01:39 PM

C++ is a widely used programming language that is very convenient and practical in writing countdown programs. Countdown program is a common application that can provide us with very precise time calculation and countdown functions. This article will introduce how to use C++ to write a simple countdown program. The key to implementing a countdown program is to use a timer to calculate the passage of time. In C++, we can use the functions in the time.h header file to implement the timer function. The following is the code for a simple countdown program

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

How to open a website using Task Scheduler How to open a website using Task Scheduler Oct 02, 2023 pm 11:13 PM

Do you frequently visit the same website at about the same time every day? This can lead to spending a lot of time with multiple browser tabs open and cluttering the browser while performing daily tasks. Well, how about opening it without having to launch the browser manually? It's very simple and doesn't require you to download any third-party apps, as shown below. How do I set up Task Scheduler to open a website? Press the key, type Task Scheduler in the search box, and then click Open. Windows On the right sidebar, click on the Create Basic Task option. In the Name field, enter the name of the website you want to open and click Next. Next, under Triggers, click Time Frequency and click Next. Select how long you want the event to repeat and click Next. Select enable

iOS 17: How to organize iMessage apps in Messages iOS 17: How to organize iMessage apps in Messages Sep 18, 2023 pm 05:25 PM

In iOS 17, Apple not only added several new messaging features, but also tweaked the design of the Messages app to give it a cleaner look. All iMessage apps and tools, such as the camera and photo options, can now be accessed by tapping the "+" button above the keyboard and to the left of the text input field. Clicking the "+" button brings up a menu column with a default order of options. Starting from the top, there's camera, photos, stickers, cash (if available), audio, and location. At the very bottom is a "More" button, which when tapped will reveal any other installed messaging apps (you can also swipe up to reveal this hidden list). How to reorganize your iMessage app You can do this below

Can't allow access to camera and microphone in iPhone Can't allow access to camera and microphone in iPhone Apr 23, 2024 am 11:13 AM

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

Fix: System driver locked to manufacturer specifications Fix: System driver locked to manufacturer specifications Sep 23, 2023 pm 07:25 PM

Graphics driver is one of the most important drivers on your PC, directly affecting performance and gaming experience. However, many people encounter the "Your system has drivers locked to manufacturer specifications" error when updating drivers through the installer. This issue occurs because the manufacturer restricts updates unless they are specifically pushed from their end. This ensures stability, but may be an issue for many people. So, let’s find out how to solve the problem now! How to fix your system with drivers locked to manufacturer specifications on Windows 11? Before we move on to slightly more complicated solutions, try these quick fixes: Make sure your computer and operating system meet the driver's system requirements. Boot your computer into safe mode, then

See all articles