Mysql 5.5 LOAD DATA INFILE Permissions - c++

ERROR The used command is not allowed with this MySQL version
I am having problems migrating some mysqlclient C++ code from Mysql 5.1 to 5.5 (using soci). The C++ part is not so relevant - the problem is writing some mysqlclient code which can successfully do a LOAD DATA INFILE on MySQL 5.5.
Here are my notes (LOAD DATA INFILE fails, but normal queries are ok):
The code below works fine on Mysql 5.1, gcc 4.6.1, Oneiric
The same code fails on Mysql 5.5, gcc 4.7.2, Quantal
If I LOAD DATA INFILE from mysql (the command-line client), it works fine (I have updated my.cnf with local-infile=1)
mysql> show variables like '%local_infile%'; results in ON
It would be great if there were a SOCI or a configuration solution to this, but if someone has managed to get this to work with libmysqlclient, that would be great to know, too...
#include <soci.h>
#include <mysql/soci-mysql.h>
#include <string>
#include <iostream>
using soci::use;
using namespace std;
using namespace soci;
main()
{
string val =
"mysql://" +
"host=127.0.0.1" +
" dbname=tmp_db" +
" user=root" +
" password=open_sasame";
int sum;
session sql( val );
sql << "SELECT 1+1", into( sum );
cerr << "RESULT=" << sum << endl; // works fine
// NEXT LINE FAILS WITH:
// The used command is not allowed with this MySQL version
sql << "LOAD DATA LOCAL INFILE '/tmp/junk3.txt' INTO TABLE tmp_db.example_tbl FIELDS TERMINATED BY '|' LINES TERMINATED BY '\\n'";
}

The answer is, we need the following line of code:
mysql_options( &mysql, MYSQL_OPT_LOCAL_INFILE, 0 );
inserted between mysql_init() and mysql_real_connect().
Below is a snippet of C code for reference. Note that SOCI's mysql backend can be patched with this line of code for it to work.
Tested and works on Mysql 5.5, gcc 4.7.2, Quantal.
#include <mysql.h>
#include <stdio.h>
main()
{
MYSQL mysql;
mysql_init( &mysql );
mysql_options( &mysql, MYSQL_OPT_LOCAL_INFILE, 0 );
if ( !mysql_real_connect( &mysql,"127.0.0.1","root","open_sasame","tmp_db",0,NULL,0 ))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error( &mysql ));
}
if ( mysql_query( &mysql, "LOAD DATA LOCAL INFILE '/tmp/junk4.txt' "
"INTO TABLE tmp_db.example_tbl FIELDS TERMINATED BY '|' "
"LINES TERMINATED BY '\\n'" ))
{
fprintf( stderr, "ERROR DURING LOAD DATA LOCAL INFILE\n" );
}
mysql_close( &mysql );
}

Related

how to use mysql-connector-c++ 8.0: Cannot connect to remote Alibaba cloud database hosted

Can anyone tell me the answer, I have been unable to eat for a few days, thank you for being my benefactor
I'm using the mysql-connector-c++ 8.0 to mysql 8.0.x
I want to connect to a remote cloud database. After trying countless times, I have encountered great difficulties. Is there something wrong with my code? I am a newbie to msyql
The strange thing is that mysql - h xxx - root - p can be executed on the linux command
line, but it fails in c++ alone, and the error is always one:
CDK Error: Connection attempt to the server was aborted. Timeout of 10000 milliseconds was exceeded
#include <iostream>
#include <string>
#include <list>
#include <cstdlib>
#include <mysqlx/xdevapi.h>
using namespace mysqlx;
int main() {
try {
Session sess(SessionOption::USER, "root",
SessionOption::PWD, "123456",
SessionOption::HOST, "172.29.207.112",
//SessionOption::HOST, "rm-bp1qp1x588kzb49rf.mysql.rds.aliyuncs.com",
SessionOption::PORT, 33060,
SessionOption::DB, "demo");
auto result = sess.sql("select * from person").execute();
for (auto row : result.fetchAll()) {
std::cout << row[0] << " " << row[1] << "\n";
}
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
}
}
I finally know the answer. The reason is that the cloud database provider does not support 33060 of X Protocol. Currently, Alibaba Cloud does not support it. I learned this from the intelligent problem robot, but it is not mentioned in the document. Alibaba Cloud should update documentation! !

Sqlite database empty when profiling with callgrind

I am attempting to profile a program that requires data loaded from a sqlite database. Running the program normally works as expected, but when I use callgrind the opened database is empty (no tables; a user_version that is set in the database comes back as 0). The database file is found at the correct path, and appears to be correctly opened, but there is nothing in it.
Test program (sqlite_test.cpp):
#include <sqlite3.h>
#include <iostream>
#include <sys/stat.h>
bool dbExists() {
struct stat s;
if (stat("testDB", &s) != 0) {
return false;
}
else {
return true;
}
}
int main()
{
if (dbExists())
std::cout << "db exists\n";
sqlite3 *db;
int open = sqlite3_open_v2("testDB", &db, SQLITE_OPEN_READWRITE, NULL);
if (open == SQLITE_OK) {
std::cout << "db opened\n";
}
else {
std::cout << "Failed to open DB; code: " << open << "\n";
}
sqlite3_stmt *stmt;
sqlite3_prepare_v2(db, "PRAGMA user_version;", -1, &stmt, NULL);
int dbVersion = 0;
int res = sqlite3_step(stmt);
if (res == SQLITE_ROW) {
dbVersion = sqlite3_column_int(stmt, 0);
}
else {
std::cout << "DB version not set: " << res << " " << sqlite3_errstr(res) << "\n";
}
std::cout << "Database version: " << dbVersion << std::endl;
sqlite3_close(db);
}
I created a database ("testDB") that has "pragma user_version = 5;", and is located in the same folder as the executable. The executable is built using
g++ sqlite_test.cpp -lsqlite3 -o sqlite_test
Output:
# ./sqlite_test
db exists
db opened
Database version: 5
# valgrind --tool=callgrind ./sqlite_test
==1184== Callgrind, a call-graph generating cache profiler
==1184== Copyright (C) 2002-2015, and GNU GPL'd, by Josef Weidendorfer et al.
==1184== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==1184== Command: ./sqlite_test
==1184==
==1184== For interactive control, run 'callgrind_control -h'.
db exists
db opened
Database version: 0
==1184==
==1184== Events : Ir
==1184== Collected : 2787290
==1184==
==1184== I refs: 2,787,290
One other thing I have noticed is that it seems to work fine when running with valgrind (memcheck). It is only with callgrind that the problem shows up. This is true for both valgrind 3.12 and 3.14.
Update:
Obviously I should have mentioned what I am running this on, because it appears to be the source of the problem.
My problem is on a system running Yocto 2.2.2 (Morty) on a single core ARM processor. There is no problem if I run the same thing on a different system (which is a virtual machine) running Ubuntu 18.04.
I am not sure which difference between those systems is causing the problem.

How to upload files in MongoDB through mongocxx C++ driver?

I want to use the mongo-cxx-driver to upload files but can't find a way. Tried to use the gridfs feature of mongodb but couldn't integrate. Using current stable version mongodb-cxx-driver (3.1.1).
gridFs throws error when try to store file like this:
gfs.storeFile("filepath", "filename");
Error: store_file: /usr/include/boost/smart_ptr/scoped_ptr.hpp:99: T* boost::scoped_ptr::operator->() const [with T = mongo::AtomicWord]: Assertion `px != 0' failed.
Aborted (core dumped)
Also if mongo client is initialized it provides segmentation fault error.
#include "mongo/client/dbclient.h"
#include <iostream>
#include <cstdlib>
using namespace std;
using namespace mongo;
int main(int argc, const char** argv) {
cout<<"good so far"<<endl;
client::GlobalInstance instance; //everytime producing segmentation fault
if (!instance.initialized()) {
std::cout << "failed to initialize the client driver: " << instance.status() << std::endl;
return EXIT_FAILURE;
}
else
{
std::cout << "Successfully initialized the client driver: " << instance.status() << std::endl;
}
return EXIT_SUCCESS;
}
That looks like the legacy client, not the stable 3.1.1 version.
GridFS is not yet available for the stable client (initial priority was on essential CRUD features), but GridFS is under active development and will be available in the 3.2.0 release expected in the next couple months. If you want to keep an eye on progress, the relevant JIRA ticket is CXX-1130.

OCCI in Oracle database - Couldn't find entry point of procedure OCIPIsDescRebuilt in OCI.dll library

I've decided to connect my C++ game with Oracle database using OCCI (Oracle C++ Call Interface). I've finally managed to compile my program with occi.h included but now, when I run the program, I get the message:
Couldn't find entry point of procedure OCIPIsDescRebuilt in OCI.dll library.
Here is a small part of my code in which I try to connect with database:
#include <occi.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
using namespace oracle::occi;
int main()
{
Environment *env = Environment::createEnvironment(Environment::DEFAULT);
Connection *conn = env->createConnection("system", "password","localhost:1521");
cout << "Podaj nick gracza nr 1: ";
cin >> nick[1];
cout << "Podaj nick gracza nr 2: ";
cin >> nick[2];
Statement *stmt = conn->createStatement();
stmt->executeUpdate("INSERT INTO uzytkownicy VALUES('1','A',nick[1])");
stmt->executeUpdate("INSERT INTO uzytkownicy VALUES('1','B',nick[2])");
ResultSet *rs = stmt->executeQuery("SELECT * FROM basket_tab");
cout << "The basket has:" << endl;
while (rs->next())
{
string fruit = rs->getString(1); // get the first column as string
int quantity = rs->getInt(2); // get the second column as int
cout << quantity << " " << fruit << endl;
}
env->terminateConnection(conn);
Environment::terminateEnvironment(env);
}
I use MS Visual Studio 2010 and Oracle Database Express Edition 11g Release 2 (11.2). Apart from Oracle® C++ Call Interface Programmer's Guide, 11g Release 2 (11.2), I also used materials from these sites:
Mark Williams Blog
This Thread Blog
How can I solve this problem? Maybe there is a simplier way (as for configuration) to connect with database using C++ program than through OCCI?
I was able to get rid of this error by copying the oci.dll file from the Oracle 12c Instant Client to the bin/Debug or bin/Release directory of my application so it would get loaded instead of the oci.dll that is in the default Oracle 11 client that gets installed.

how connect mysql using c++

Editor's Note: Original Text:
how connect mysql using c++
#include <iostream>
#include <fstream>
#include <string>
#include "/usr/local/mysql5/include/mysql.h"
using namespace std;
int main() {
MYSQL *mysql;
MYSQL_RES *result;
MYSQL_ROW row;
string server = "192.168.1.92";
string username = "useradmin";
string password = "useradmin";
string database = "market";
int port = 3306;
mysql = mysql_init(0);
if ( !mysql_real_connect( mysql, server.c_str(), username.c_str(), password.c_str(), database.c_str(), port, NULL, 0 ) ) {
fprintf( stderr, "%s\n", mysql_error( mysql ) );
return 0;
}
if ( !mysql_query( mysql, "SELECT text, prequency FROM ma_dict" ) ) {
fprintf(stderr, "%s\n", mysql_error( mysql ) );
return 0;
}
result = mysql_use_result( mysql );
ofstream SaveFile("/tmp/dict.txt");
while ( ( row = mysql_fetch_row( result ) ) != NULL ) {
//SaveFile << fprintf( stdout, "%s\t%d", row[0], $row[1] ) << endl;
cout << row[0] << endl;
}
mysql_free_result( result );
mysql_close( mysql );
SaveFile.close();
return 1;
}
undefined reference to mysql_init'
undefined reference tomysql_real_connect'
...
Editor's Note: A translation was attempted:
I'm using Eclipse, and I get compilation errors using mysql.h. How do I link to mysql?
#include "mysql.h"
Editor's Note: English questions are required on Stack Overflow. Please do your best at translating your question to English as needed, even if you have to use an automatic translation service like Google Translate.
Editor's Note: Original Text:
信息太少了, 把编译器错误贴出来看看.
可能因为:
mysql.h不在配置的include目录中.
Mysql库文件不在link列表里面
头文件与二进制库文件不配套
Editor's Note: A translation was attempted:
There's too little information in your question. Post the actual compiler error into your question.
That being said, the error may be caused by:
Mysql.h is not in the configuration or in the include directory.
MySql database file is not in the linker's path.
Header and library files are not consistent with each other.
An issue in the sample is that mysql_query returns 0 on SUCCESS – so it always thinks an error occurred on success.
Change it to:
if ( mysql_query( mysql, "SELECT text, prequency FROM ma_dict" ) ) {