Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect mySQL database using C++

Tags:

c++

mysql

I'm trying to connect the database from my website and display some rows using C++. So bascily I'm trying to make an application that does a select query from a table from my site database. Now, this must be possible because I've seen tons of applications doing it.

How do I do this? Can some one make an example and tell me what libraries I should be using?

like image 231
lepel100 Avatar asked May 07 '13 17:05

lepel100


People also ask

Can we connect MySQL with C?

MySQL database is available on most important OS platforms. It runs on BSD Unix, Linux, Windows, or Mac OS. To be able to compile C examples, we need to install the MySQL C development libraries. The above line shows how we can do it on Debian based Linux.

Can I connect database with C?

This IDE is specially designed for C and C++ and easy to use. SQLAPI++ is a C++ library (basically a set of header files) for accessing multiple SQL databases (Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL, SQLite, SQL Anywhere and ODBC). It is easy to implement and simple.

Can we connect MySQL with C ++?

MySQL Connector/C++ 8.0 is a MySQL database connector for C++ applications that connect to MySQL servers. Connector/C++ can be used to access MySQL servers that implement a document store, or in a traditional way using SQL statements.


1 Answers

Found here:

/* 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", "root");   /* Connect to the MySQL test database */   con->setSchema("test");    stmt = con->createStatement();   res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); // replace with your statement   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; } 
like image 168
hd1 Avatar answered Sep 20 '22 04:09

hd1