delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2007/03/28/15:55:04

X-Spam-Check-By: sourceware.org
To: cygwin AT cygwin DOT com
From: Eric Lilja <mindcooler AT gmail DOT com>
Subject: Using mysql 5.1.16 beta client libraries on cygwin
Date: Wed, 28 Mar 2007 22:54:05 +0200
Lines: 219
Message-ID: <eueklc$4r9$1@sea.gmane.org>
Mime-Version: 1.0
User-Agent: Thunderbird 1.5.0.10 (Windows/20070221)
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

--------------090903080007080806020105
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi!

As many of you may know, the binary distribution of MySQL for Windows 
only ships with MSVC++ libraries. They have no binary distribution for 
Cygwin. So what do you do if you want to develop c or c++ programs 
talking to a native Windows MySQL server using the cygwin tools?

Well, you can compile the client libraries yourself. Here's how I did it:
$ tar xvzf mysql-5.1.16-beta.tar.gz
$ cd mysql-5.1.16-beta
$ ./configure --prefix=/usr/local/mysql-5.1.16-beta 
--exec-prefix=/usr/local/mysql-5.1.16-beta --without-server 
--without-docs --without-man
$ make
$ make install

Keep the source directory so you can uninstall with:
$ make uninstall

Then I launched the mysql server (native windows version), created a 
database named dbase. This particular server doesn't require a password 
to login but you have to be on localhost.

Then I wrote this simple testprogram using my newly-compiled client 
libraries:
#include <cstdlib>
#include <iostream>
#include <sstream> /* Gives us <string> too. */

#include <mysql.h>

using namespace std;

void execute_query(MYSQL *, const string&, const string&);

int
main()
{
    MYSQL m;
    const char *host = "127.0.0.1"; /* "localhost" instead of 127.0.0.1 
doesn't work. */
    const char *user = "root";
    const char *password = 0;
    const char *database = "dbase";
    const char *table_name = "wizard_spells";

    mysql_init(&m);

    if (!mysql_real_connect(&m, host, user, password, database, 3306, 
NULL, 0))
    {
       cerr << mysql_error(&m) << endl;

       return EXIT_FAILURE;
    }

    cout << "Connection successful." << endl;

    stringstream query;

    query << "CREATE TABLE " << table_name << " (name VARCHAR(64) 
PRIMARY KEY, mana INT)";

    execute_query(&m, query.str(), "Succesfully created new table 
wizard_spells.");

    query.str(""); /* Empty stringstream. */

    cout << query.str() << endl;

    query << "INSERT INTO " << table_name << " VALUES ('solar strike', 50)";

    execute_query(&m, query.str(), "Successfully inserted one row into 
table wizard_spells.");

    mysql_close(&m);

    return EXIT_SUCCESS;
}

void
execute_query(MYSQL *m, const string& query, const string& success_string)
{
    if (mysql_query(m, query.c_str()) == 0)
    {
       cout << success_string << endl;
    }
    else
    {
       cerr << "Query failed: "<<  mysql_error(m) << endl;;
    }
}

Corresponding makefile:
CXX = g++
# Cannot use -pedantic or it will fail with:
# usr/local/mysql-5.1.16-beta/include/mysql/mysql.h:125: error: ISO C++ 
does not support `long long'
CXXFLAGS = -Wall -Wextra -std=c++98 -g -I 
/usr/local/mysql-5.1.16-beta/include/mysql -c
LDFLAGS = -L /usr/local/mysql-5.1.16-beta/lib/mysql -lmysqlclient -lz -o 
$(EXEC)
EXEC = cygwintest.exe
OBJECTS = cygwintest.o

all: $(OBJECTS)
	$(CXX) $^ $(LDFLAGS)

cygwintest.o: cygwintest.cpp
	$(CXX) $(CXXFLAGS) $<

clean:
	rm -f $(OBJECTS) $(EXEC) *.stackdump


Hope this helps someone. I've attached source and Makefile.

- Eric

--------------090903080007080806020105
Content-Type: text/plain;
 name="Makefile"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="Makefile"

CXX = g++
# Cannot use -pedantic or it will fail with:
# usr/local/mysql-5.1.16-beta/include/mysql/mysql.h:125: error: ISO C++ does not support `long long'
CXXFLAGS = -Wall -Wextra -std=c++98 -g -I /usr/local/mysql-5.1.16-beta/include/mysql -c
LDFLAGS = -L /usr/local/mysql-5.1.16-beta/lib/mysql -lmysqlclient -lz -o $(EXEC)
EXEC = cygwintest.exe
OBJECTS = cygwintest.o

all: $(OBJECTS)
	$(CXX) $^ $(LDFLAGS)

cygwintest.o: cygwintest.cpp
	$(CXX) $(CXXFLAGS) $<

clean:
	rm -f $(OBJECTS) $(EXEC) *.stackdump

--------------090903080007080806020105
Content-Type: text/x-c++src;
 name="cygwintest.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="cygwintest.cpp"

#include <cstdlib>
#include <iostream>
#include <sstream> /* Gives us <string> too. */

#include <mysql.h>

using namespace std;

void execute_query(MYSQL *, const string&, const string&);

int
main()
{
   MYSQL m;
   const char *host = "127.0.0.1"; /* "localhost" instead of 127.0.0.1 doesn't work. */
   const char *user = "root";
   const char *password = 0;
   const char *database = "dbase";
   const char *table_name = "wizard_spells";
   
   mysql_init(&m);

   if (!mysql_real_connect(&m, host, user, password, database, 3306, NULL, 0))
   {
      cerr << mysql_error(&m) << endl;

      return EXIT_FAILURE;
   }

   cout << "Connection successful." << endl;

   stringstream query;

   query << "CREATE TABLE " << table_name << " (name VARCHAR(64) PRIMARY KEY, mana INT)";

   execute_query(&m, query.str(), "Succesfully created new table wizard_spells.");

   query.str(""); /* Empty stringstream. */

   cout << query.str() << endl;

   query << "INSERT INTO " << table_name << " VALUES ('solar strike', 50)";

   execute_query(&m, query.str(), "Successfully inserted one row into table wizard_spells.");

   mysql_close(&m);
   
   return EXIT_SUCCESS;
}

void
execute_query(MYSQL *m, const string& query, const string& success_string)
{
   if (mysql_query(m, query.c_str()) == 0)
   {
      cout << success_string << endl;
   }
   else
   {
      cerr << "Query failed: "<<  mysql_error(m) << endl;;
   }
}


--------------090903080007080806020105
Content-Type: text/plain; charset=us-ascii

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/
--------------090903080007080806020105--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019