I've seen some similar questions but most are based around PHP, there was one based around C but the only answer was go to the SQLConnect() docs, which I've already done.
https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlconnect-function?redirectedfrom=MSDN&view=sql-server-ver15
Seems to be little content online, also I'm a beginner so excuse any silly issues.
I set up the SQL database using the MYSQL Workbench and it's sitting on my localhost:3306. I was able to connect it to the ODBC Data Source Admin program successfully.
So I believe I'm using the right function and have the right SQL back end set up, I just don't know where to go from here.
The end goal is to be able to 'insert' records into my database with the program and also 'select' from it too.
What am I doing wrong, or what am I missing?
# include <stdio.h>
# include <stdlib.h>
# include <sql.h>
# include <sqlext.h>
# include <cstdio>
# include <cstdint>
using namespace std;
int main(){
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
/* Initialize the ODBC environment handle. */
SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv);
/* Set the ODBC version to version 3 (the highest version) */
SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(void*)SQL_OV_ODBC3, 0);
/* Allocate the connection handle. */
SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
SQLConnectW(hdbc, (SQLWCHAR*)"localhost", (SQLSMALLINT) 9,
(SQLWCHAR*)"root", (SQLSMALLINT)4,
(SQLWCHAR*)"password", (SQLSMALLINT)8);
return(0);
}
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;
}
Related
I have this simple C++ program:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: marek
*
* Created on 1. listopadu 2017, 22:33
*/
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
/*
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 <mysql_driver.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
cout << endl;
cout << "running select as a messege" << 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://10.0.0.6:3306", "root", "*****");
/* Connect to the MySQL test database */
con->setSchema("mysql");
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 data 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;
std::cout << "hello world pc 5 mysql\n";
#ifdef __linux__
std::cout << "__linux__\n";
#elif defined(__unix__)
std::cout << "__unix__\n";
#elif defined(_WIN64)
std::cout << "_WIN64\n";
#elif defined(_WIN32)
std::cout << "_WIN32\n";
#endif
#if __WORDSIZE == 64
std::cout << "64 bit\n";
#else
std::cout << "32 bit\n";
#endif
return 0;
}
Its important to note:
MySQL server (MariaDB) is running on Raspi Pi 3 on local subnet.
I am able to connect to DB for example from HeidiSQL
The same script is build build successfully on Raspi pi 3
My IDE is NetBeans with following settings:
Tools > options > C/C++ compilers is MiniGW QT with everything store in default installation folder. For me C:\Qt\Qt5.9.2\, and make command is C:\msys\1.0\bin\make.exe.
To this compiller I am also including directories Mysql connector C++ 1.1.9. also in the default installation directory. For me it would be c:\Program Files\MySQL\MySQL Connector C++ 1.1.9
My Windows path is set to:
C:\ProgramData\Oracle\Java\javapath;
%SystemRoot%\system32;
%SystemRoot%;
%SystemRoot%\System32\Wbem;
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;
C:\Program Files\PuTTY\;
c:\msys\1.0\bin\;
C:\MinGW\bin;
c:\Qt\Qt5.9.2\5.9.2\mingw53_32\bin\;
c:\Qt\Qt5.9.2\5.9.2\;
c:\Qt\Qt5.9.2\5.9.2\mingw53_32\bin\designer.exe;
c:\Qt\Qt5.9.2\Tools\QtCreator\bin\;
c:\Qt\Qt5.9.2\Tools\QtCreator\bin\qtcreator.exe;
C:\Program Files\MariaDB\MariaDB Connector C\lib\;
C:\Program Files\MariaDB\MariaDB Connector C\lib\plugin\;
C:\Program Files\MySQL\MySQL Utilities 1.6\;
c:\Program Files\MySQL\MySQL Connector C++ 1.1.9\include\;
After a long struggle I've been able to make a build on NetBeans Windows. Inside of the same folder, where my executable is being stored I copied in a mysqlcppconn.dll, which was originally missing.
If I run this version of my program from Netbeans I get:
running select as a messege
RUN FAILED (exit value 255, total time: 2s)
Windows CMD I get:
nothing.
just windows dialog of program crashes
I am trying to establish connection with MySQL database in C++ project.And I followed the instructions given in link. After installing Cmake, I was facing problem in Build & Install MySQL Connector/C++.Because CmakeLists.txt does not exists in directory.After some search on web, I have found this answer and followed the steps given, but it didn't work in my case.
Frustrated will all this, I copied all the files(Extracted from dowload link of MySQLConnector C++) into my project folder, and trying to establish connection using this piece of code.
/* Standard C++ includes */
#include <cstdlib>
#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 "mysql_driver.h"
#include "cppconn/driver.h"
#include "cppconn/exception.h"
#include "cppconn/resultset.h"
#include "cppconn/statement.h"
using namespace std;
using namespace sql::mysql;
void db_connection() {
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;
}
But it says, undefined reference toget_driver_instance', but it is defined indriver.h`.
Finally, i have found the solution :)
First I completely un-installed MySQL from system (MySQL Client, MYSQL Server and libmysqlcppconn-dev).
After that, install all these again again.
Install MySQL Client and Server.
sudo apt-get install libmysqlcppconn-dev
Now ensure you have all dependencies installed
sudo apt-get build-dep libmysqlcppconn-dev
And now build the package using -lmysqlcppconn.
I want to connect my Raspberry Pi 2 to an external MySQL database on 000webhost.com with the help of C++.
#include <stdlib.h>
#include <iostream>
#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)
{
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("<000webhost mysql address>", "<username>", "<password>");
/* Connect to the MySQL test database */
con->setSchema("<database>");
stmt = con->createStatement();
res = stmt->executeQuery("<sql statement>"); // 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;
}
I got an error saying that the mysql_connection.h file was missing. I don't know what i did wrong or if there is an easier or simpler way. Please help me with this matter. Thanks.
I got an error saying that the mysql_connection.h file was missing.
Going by the error message you describe and the response to my question "Do you have the source installed for mysql_connection.h?," which was:
I didn't install mysql on the raspberry pi since I was trying to access an external database.
(Which is not what I asked.) it appears the reason #include "mysql_connection.h doesn't work is because the library isn't there.
The Connector/C library does not require the installation of MySQL, but the Connector/C source must be installed to include headers from the library, according to the documentation.
MySQL's Connector/C library requires boost; however Raspian already has boost installed, though that may come with it's own set of issue, which are discussed and resolved here if you have trouble with boost. (If your Raspberry Pi has NOOBS, I recommend switching to Raspian.)
Though the link is in my comments, instructions for installing the Connector/C library are available from MySQL's documentation.
I've collected some resources and links which should provide some additional help:
How to Install Third Party Libraries
How to #include third party libraries
Installing Connector/C++ from Source on Unix and Unix-Like Systems
I know there's a lot of suggestions of how to fix this issue, here in this forum, but none of them solved my problem.
It's always the same: code compiles fine and I can use almost all of the getXX members offered by ResultSet, except for getString, that also compiles without problem, but always crash at runtime.
I did some weird tries, like getting a char field using getInt("label") or getBlob("label"), the results weren't good as I previouslly had known, but at least I didn't experience crash.
I tried versions 1.0.5 and 1.1.3+boost. In case of 1.1.3, I also compiled from source, using CMake, but I supose that my knowlegment about the utility is not sufficient and it would last a long time before I have CMake under control. So I'd like to get a more straight solution.
My goal is to create some Win32_Console and WinForms codes, from MSVC 2010, that read and write data in MySQL 5.6 that runs in a XUbuntu machine of my network. And getString() is trapping me a lot.
Thanks in advance
I've added the code bellow:
#include "stdafx.h"
#include <iostream>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <exception.h>
#include <statement.h>
#include <resultset.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"Testando uma conexao...\n";
cout<<"Connector 1.1.3:\n";
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = sql::mysql::get_mysql_driver_instance();
try
{
con = driver->connect("tcp://192.168.1.75:3306", "site_rw", "");
if (con->isClosed()==0)
cout<<"Sucesso!\n\n";
cout<<"Inserindo statement...\n";
stmt = con->createStatement();
stmt->execute("USE test_area");
stmt->execute("DROP TABLE IF EXISTS test");
stmt->execute("CREATE TABLE test(id INT, label CHAR(1))");
cout<<"Criada a tabela...\n";
for (int k=0; k<30;k++)
stmt->execute("INSERT INTO test(id, label) VALUES (1, 'a')");
cout<<"Linhas inseridas...\n";
cout<<"Preparando-se para listar os inteiros:\n";
res = stmt->executeQuery("SELECT * FROM test LIMIT 0,5");
while (res->next())
cout<<"id = " << res->getInt("id") << "\n";
cout<<"\n\nAgora tentando o char(1):\n";
res->beforeFirst();
while (res->next())
cout<<"label = " << res->getString("label");
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() << endl;
cout << " (MySQL error code: " << e.getErrorCode() << endl;
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout<<"Terminado...\n";
return 0;
}
Aluísio
São Paulo
Recently, I've been trying to make a little test program to communicate with a MySQL server in C++. I'm currently using MySQL Connector/C++ as my API to connect to my database server. It took me a very long time to get it running, because oracle/mysql has little to no documentation of how to use connector/c++ with Visual Studio 10+.
Finally after getting everything working, there seems to be some issue when the application tries to exit. It throws the following unhandled exception:
Unhandled exception at 0x00C62291 in mysql2.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.
After researching about the error, I figured out it was due to the "security check" option (/gs compiler option). When I disable this compiler option, the application exits gracefully.
I have a feeling that I should not turn it off, as it is the default option in Visual Studio 2012 (and possibly other versions?)
My questions are:
Why is this /gs compiler option causing the unhandled exception?
Is it safe or okay to turn off the /gs compiler option?
Here is the piece of code the unhandled exception points to (inside a file, called: gs_report.c):
#if defined (_M_IX86) || defined (_M_X64)
if (IsProcessorFeaturePresent(PF_FASTFAIL_AVAILABLE))
#endif /* defined (_M_IX86) || defined (_M_X64) */
__fastfail(FAST_FAIL_STACK_COOKIE_CHECK_FAILURE);
Here is my application code:
/* 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 "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Let's have MySQL count from 10 to 1..." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;
/* Create a connection */
driver = sql::mysql::get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "MSxa5y");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
stmt->execute("DROP TABLE IF EXISTS test");
stmt->execute("CREATE TABLE test(id INT)");
delete stmt;
/* '?' is the supported placeholder syntax */
pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");
for (int i = 1; i <= 10; i++) {
pstmt->setInt(1, i);
pstmt->executeUpdate();
}
delete pstmt;
/* Select in ascending order */
pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
res = pstmt->executeQuery();
/* Fetch in reverse = descending order! */
res->afterLast();
while (res->previous())
cout << "\t... MySQL counts: " << res->getInt("id") << endl;
delete res;
delete pstmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "() on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
The /gs option is allowing the runtime to detect an error in your program. Removing the option allows the program to exit without detecting it, but the error is still there. Somewhere in your code or the libraries, memory on the stack is being overwritten. I'd try a binary search on the code to see which statement is leading to the error. You may also want to always check the return value from all of your database calls.
You definitely should not disable this option. Doing so will hide real errors and may leave your program open to hacking.
Make sure you check the result codes of your method calls. This sounds similar to this question: Buffer Overrun using Mysql Connector c++ as Visual Studio's /gs flag is warning about buffer overflows.