Quantcast
Channel: MySQL Forums - Connector/C++
Viewing all articles
Browse latest Browse all 527

Unable to Connect to MySQL (no replies)

$
0
0
I'm using NetBeans 6.9.1 and cygwin C++ on Windows Vista (wampserver, mysql 5.1.36).

I'm unable to get a basic mysql connection to work and would really appreciate any advice!

This compiles fine. When I run it fails on driver->connect(). It gives me a segmentation fault. In debug mode, NetBeans says "Signal received: ? (Unknown Signal)" on the driver->connect line..

The mysql username and password are the correct ones. I'm not sure about whether I should be using "tcp:127.0.0.1:3306" - I just copied that from the example. I am running an apache server on 127.0.0.1.

I added all the mysql C++/Connector directories to my project properties libraries.

This is my example:

/*
* File: main.cpp
* Author: Aaron
*
* Created on October 11, 2010, 3:48 PM
*/

#include <stdio.h>
#include <time.h>
#include <math.h>


/*
examples/standalone_example_docs1.cpp
* From: http://blog.ulf-wendel.de/?p=216
*/

/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>

/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace std;

int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;

try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;

/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "");
/* Connect to the MySQL test database */
con->setSchema("test");

stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
delete con;

} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}

cout << endl;

return EXIT_SUCCESS;
}

Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>