C program mysql connection
Return Values
A MYSQL* connection handle if the connection was successful, NULL if the connection was unsuccessful. For a successful connection, the return value is the same as the value of the first parameter.
http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html
your connection is not being made
How to connect and insert record to mysql using c language?
You will need to use the MySQL C Connector which your can find on their website: https://dev.mysql.com/downloads/connector/c/
Concerning your request regarding the example, a simple research would have helped you:
Mysql INSERT statement in C
Nothing beats the manual though: https://dev.mysql.com/doc/refman/5.7/en/c-api-function-overview.html
EDIT:
Here is a simple example:
sql.c:
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
int main(void)
{
MYSQL *conn;
if ((conn = mysql_init(NULL)) == NULL)
{
fprintf(stderr, "Could not init DB\n");
return EXIT_FAILURE;
}
if (mysql_real_connect(conn, "localhost", "user", "passwd", "dbname", 0, NULL, 0) == NULL)
{
fprintf(stderr, "DB Connection Error\n");
return EXIT_FAILURE;
}
if (mysql_query(conn, "INSERT INTO table_1 (test) VALUES ('Hello World')") != 0)
{
fprintf(stderr, "Query Failure\n");
return EXIT_FAILURE;
}
mysql_close(conn);
return EXIT_SUCCESS;
}
gcc sql.c -o sql -lmysqlclient
Connecting to mysql database with c program
cannot find -l-lmysqlpp
cannot find -l-lmysqlclient
These should be presented to the linker as:
-lmysqlpp
-lmysqlclient
The repeating -l
switch indicates something wrong in your linker settings. Make sure there are no entries (including spaces or other hidden characters) in both link library
an d Other linker options
boxes. You may need to clear and re-enter everything in each box.
One more think to try, view the actual compile command line that is being used:
Code::Blocks can output a build log. Settings->Compiler and debugger->Global compiler settings->{slide tabs to the right}->Build options tab->Save build log to HTML. Turn this feature on, then view the log after your next attempt. There may be something there pointing to the problem.
How can I connect to my SQL Database through C++?
I have added the comments inside the code, which will help you to understand the code easily.
Credits: Link
/* Includes Standard C++ */
#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 'Successfull' »
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 'Successfull' 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;
}
Accessing database using C
Yes, you can connect your C code to MySQL using a connector (driver) available on MySql home page: http://www.mysql.com/products/connector/
There is a nice tutorial on writing C programs accessing MySQL: MySQL C API programming tutorial
You can use any IDE you want for writing your code, for example Visual Studio. Here you can download a free version: http://www.microsoft.com/visualstudio/11/en-us/products/express
How to connect mySQL database using C++
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;
}
Related Topics
How to Std::Move Objects Out of Functions? (C++11)
How to Get Total CPU Usage in Linux Using C++
Where Are C/C++ Main Function's Parameters
How to Return Numpy.Array from Boost::Python
What Is the Maximum Length in Chars Needed to Represent Any Double Value
Use Channel Hiearchy of Boost.Log for Severity and Sink Filtering
Sorting a Std::Vector<Std::Pair<Std::String,Bool>> by the String
How to Choose Between Map and Unordered_Map
Why Is "!=" Used with Iterators Instead of "<"
Inferring the Call Signature of a Lambda or Arbitrary Callable for "Make_Function"
What Is the Scope of a 'While' and 'For' Loop