why does this output hex rather than a sentence? connector/c++ - c++

I was expecting to see a short english sentence as output but i see hex value of it instread. I cannot find any information on this getblob function but i hear its what should be used for varchar columns. Before i used getString and it crashes the app, its funny though becuase it crashes after it successfully prints the sentence sometimes. However i cant have it crashing so i need to make it work with getblob, it returns an std::istream which i no nothing about. I experimented with converting istream to string but my lack of understanding of what an isteam is did not get me far.
#include <iostream>
#include <sstream>
#include <memory>
#include <string>
#include <stdexcept>
#include "cppconn\driver.h"
#include "cppconn\connection.h"
#include "cppconn\statement.h"
#include "cppconn\prepared_statement.h"
#include "cppconn\resultset.h"
#include "cppconn\metadata.h"
#include "cppconn\resultset_metadata.h"
#include "cppconn\exception.h"
#include "cppconn\warning.h"
#include "cppconn\datatype.h"
#include "cppconn\parameter_metadata.h"
#include "mysql_connection.h"
#include "mysql_driver.h"
using namespace std;
int main()
{
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
// ...
driver = sql::mysql::get_mysql_driver_instance();
cout<<"Connecting to database...\n";
con = driver->connect("tcp://xx.xxx.xxx.xxx:3306", "xxxxx", "xxxxxx");
sql::ResultSet *res;
// ...
stmt = con->createStatement();
// ...
con->setSchema("mrhowtos_main");
res = stmt->executeQuery("SELECT english FROM `eng` WHERE `ID` = 16;");
while (res->next()) {
istream *stream = res->getBlob(1);
string s;
getline(*stream, s); //crashes here - access violation
cout << s << endl;
}
cout<<"done\n";
delete res;
delete stmt;
delete con;
system("PAUSE");
return 0;
}
UPDATE: crashes on getline
value of stream after getblob:
stream 0x005f3e88 {_Chcount=26806164129143632 } std::basic_istream > *

(This answered the first version of this question, which had a cout << stream << endl; statement.)
You're printing an istream *.
I suppose you would like to read from that istream and print what you read, e.g. with
string s;
while (getline(*stream, s))
cout << s << endl;

Related

Cant create MySQL-Connection - Stuck at compiling

i worked for 4 years on Java. Now im trying to learn C++. I want to create a Teamspeak3-Plugin.
First of all i tried to create a MySQL-Connection and i stuck on compiling.
I worked with the MySQL-Connector for C++ Version 8.0.20 and Boost for C++(for the connector) Version 1.72.0
It gives me the ErrorCode "C3867" which is saying "non standard syntax use & to create a pointer to member"
I dont know why i am getting this error and especially how to solve it.
Here are my classes:
mysql.h:
#ifndef mysql_H
#define mysql_H
#include <iostream>
#include <string>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
class mysql
{
public:
mysql();
int connect;
void createTables();
list<char*> getResult(char* qry);
};
#endif // !mysql_H
mysql.cpp
#include <iostream>
#include <string>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include "mysql.h"
using namespace std;
using namespace sql;
std::string host = "";
std::string port = "";
char* user = "";
char* password = "";
char* database = "";
sql::Driver *driver;
sql::Connection *con;
sql::ResultSet *res;
int connect() {
try {
driver = get_driver_instance();
std::string url = "tcp://" + host + ":" + port;
con = driver->connect(url, user, password);
cout << "MySQL connected!";
con->setSchema(database);
cout << "Database selected!";
return 0;
}
catch (sql::SQLException err) {
cout << "ERROR: " << err.what << endl;
}
}
void createTables() {
sql::Statement *stmt;
try {
stmt = con->createStatement();
stmt->executeQuery("CREATE TABLE IF NOT EXISTS JellyTest(Name VARCHAR(100), COINS INT)");
cout << "TableCreating done";
}
catch (sql::SQLException err) {
cout << "ERROR: " << err.what << endl;
}
}
list<char*> getResult(char* qry) {
sql::Statement *stmt;
sql::ResultSet *res;
list<char*> result;
try {
stmt = con->createStatement();
res = stmt->executeQuery(qry);
while (res->next) {
result.assign(res->next);
}
return result;
}
catch (sql::SQLException err) {
cout << "ERROR: " << err.what << endl;
}
}
Thanks for helping and have a nice day.
(I hope you are able to understand this text 'cause im from Germany)

Problem with MYSQL Connector/C++: crashes before all

#include <stdlib.h>
#include <iostream>
#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 testsql() {
cout << endl;
cout << "Let's have MySQL count from 10 to 1..." << endl;
sql::Driver *driver;
sql::Connection *con;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "37975");
delete con;
cout << endl;
return EXIT_SUCCESS;
}
int main(void)
{
cout << "Hello world!" << endl;
}
The above is the codes I copied from the Internet. It compiles but crashes when running. I use MinGW-W64. When I run the program under Eclipse, the program terminated with exit value -1,073,741,515. The mysql connector is Mysql Connector/C++ 1.1.13. I have no idea. Can some one give me some hint?
Thank you,

Expression invalid null pointer

I am having some issues with my program, what I want to do is generate a md5 password which then save it to a text file and this part is not working for me, ("Expression invalid null pointer") any help would be greatly appreciated.
C++ Visual Studio 2015
main.cpp
#include <iostream>
#include <istream>
#include <string>
#include <sstream>
#include <fstream>
#include <iterator>
#include "s_encrypt.h"
#include "encrypt_copy.h"
using namespace std;
int main(int argc, char *argv[])
{
string password = "";
cout << "Please enter a password to be encrypted\n";
getline(cin, password);
cout << "MD5 Encryption of " << password << " " << "is this" << " " << md5(password);
cout << "Saving MD5 generated password to text file";
std::string p = md5(password);
CopyEncryptedPw(p);
return 0;
}
encrypt_copy.cpp
#include <istream>
#include <iostream>
#include <fstream>
#include <string>
#include "encrypt_copy.h"
using namespace std;
std::string CopyEncryptedPw(std::string pass)
{
fstream outfile;
outfile.open("C:\encrypted_pass.txt", ios::out);
outfile << pass;
return 0;
}
encrypt_copy.h
#pragma once
#ifndef ENCRYPT_H
#define ENCRYPT_H
std::string CopyEncryptedPw(std::string pass);
#endif
There are two issues with your code:
Issue 1:
outfile.open("C:\encrypted_pass.txt", ios::out);
If we assume that your OS is Windows, this should be:
outfile.open("C:\\encrypted_pass.txt", ios::out);
Also, the forward slash can be used for the standard stream functions:
outfile.open("C:/encrypted_pass.txt", ios::out);
Issue 2:
You're returning 0 for a function that is supposed to return a std::string.
std::string CopyEncryptedPw(std::string pass)
{
//...
return 0; // <-- This is bad
}
This code exhibits undefined behavior on return, since what will happen is that a 0 is assigned to the std::string return value, and assigning 0 to a std::string is undefined behavior.
Either return a string type (or a type that is convertible to a std::string), or return int:
int CopyEncryptedPw(std::string pass)
{
fstream outfile;
outfile.open("C:\\encrypted_pass.txt", ios::out);
outfile << pass;
return 0;
}
You could also have a void function that doesn't return anything, but you probably want an int return value for example, to return an error code (or OK indicator).

c++ sql connection "get_driver_instance(); is undefined " error

MyCode is ;
#include<iostream>
using namespace std;
#include <string>
#include "mysql_connection.h"
#include <stdlib.h>
#include <iomanip>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
int main(){
cout<<"sadullah";
string database = "sch1667138";
string ipAdress = "144.122.71.165";
string password = "???";
string userName = "???";
sql::Driver *driver;
sql::Connection *con;
try {
driver =get_driver_instance();
con = driver->connect("tcp://" + ipAdress + ":3306", userName,
password);
con->setSchema("sch1667138");
cout << "successfully connected to db...." << endl;
} catch (sql::SQLException &e) {
cout << e.what() <<endl;
}
return 0;
}
When I run the code,I take the message "reference undefined get_driver_instance();" What should I do
I solved the problem. The reason is that I am working on Ubuntu, I should have run it from terminal, with the code ; g++ -o sado 1667138.cpp -lmysqlcppconn sadu is the name of execute file, and -lmysqlcppconn is adding the mysql library to cpp

Ambiguous function call to itself

I am trying to setup the mysql connector for c++. When I try to compile this code this error appears /home/cjueden/programming projects/mysqlConnect/main.cpp|23|error: call of overloaded ‘get_driver_instance()’ is ambiguous
Please explain how to fix this as I am stumped.
CODE
#include <stdlib.h>
#include <iostream>
#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;
int main(int argc, char const *argv[])
{
/* code */
cout << "STarting the MYSQL STUFF \n";
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://192.168.1.2:3306", "root", "");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("insert into cTest (text) value ('testing and stuff')");
cout << "inserted stuff\n";
res = stmt->executeQuery("SELECT 'cTest' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: \n";
/* Access column data by alias or column name */
cout << res->getString("_message");
cout << "\t... MySQL says it again: \n";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1);
}
return 0;
}
ERROR
/home/cjueden/programming projects/mysqlConnect/main.cpp||In function ‘int main(int, const char**)’:|
/home/cjueden/programming projects/mysqlConnect/main.cpp|23|error: call of overloaded ‘get_driver_instance()’ is ambiguous|
/home/cjueden/programming projects/mysqlConnect/main.cpp|23|note: candidates are:|
/usr/include/cppconn/driver.h|62|note: sql::Driver* get_driver_instance()|
/usr/include/mysql_driver.h|86|note: sql::mysql::MySQL_Driver* sql::mysql::get_driver_instance()
It looks like the version of get_driver_instance() in driver.h is in the global namespace, so here's one fix:
driver = ::get_driver_instance();
The compiler doesn't know which of the two functions to use. Based on what you're doing with the return value, I'm guessing you intended to use the one in driver.h.
Prepending double-colon (::) is a way to be more explicit when using stuff in the global namespace (like this function). It resolves the ambiguity.
Another fix would be to remove the earlier "using namespace sql::mysql;".