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
Related
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)
#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,
So I finally got VS 2013 C++ to connect with mySQL using the Connector C and C++ and I know this because I am able to add a table into my DB using the stmt->execute() function.
Now, I am trying to get the rows using the ResultSet to retrieve from function executeQuery, but this error shows up:
Here is my code:
#include <iostream>
#include <string>
#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>
#define dbHOST "tcp://127.0.0.1:3306"
#define dbUSER "root"
#define dbPASS "root"
#define dbDB "db_test"
using namespace std;
using namespace sql;
int main()
{
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = sql::mysql::get_driver_instance();
con = driver->connect(dbHOST, dbUSER, dbPASS);
stmt = con->createStatement();
string query = "SELECT * FROM test";
res = stmt->executeQuery(query.c_str());
while (res->next()) {
cout << "id = " << res->getInt(1);
cout << ", label = '" << res->getString("label") << "'" << endl;
}
delete res;
delete stmt;
delete con;
system("pause");
return 0;
}
Under the output pane, it shows this as well:
First-chance exception at 0x77661D4D in MySQLConnect.exe: Microsoft C++ exception: sql::SQLException at memory location 0x0088FB5C.
Unhandled exception at 0x77661D4D in MySQLConnect.exe: Microsoft C++ exception: sql::SQLException at memory location 0x0088FB5C.
Under the call stack after I hit F5 and it breaks, it shows this:
MySQLConnect.exe!main() Line 35 C++
Line 35 is the line with res = stmt->executeQuery(query.c_str());
Let me know if you need more information on my code / settings.
Thanks in advance!
Silly me, I found out what is the problem.
I have to add in a line:
stmt->execute("USE " dbDB);
before I can do any further query for that database. The snippet is now like this:
stmt = con->createStatement();
stmt->execute("USE " dbDB); // <-- This line, change dbDB to your own
string query = "SELECT * FROM test"; // <- before all of these below
res = stmt->executeQuery(query.c_str());
I hope this helps!
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;
I am new to Thrift.
I am trying to create a table ("sample") in Hbase using the following Thrift program on ubuntu 10.10 and can anyone tell me whether this is correct or not.
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <poll.h>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>
#include "Hbase.h"
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::hadoop::hbase::thrift;
using namespace std;
int main()
{
boost::shared_ptr<TTransport> socket(new TSocket("localhost", 60010));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
HbaseClient client1(protocol);
try
{
transport->open();
string tableName("Sample");
vector<ColumnDescriptor> columns;
columns.push_back(ColumnDescriptor());
columns.back().name = "cf:";
columns.back().maxVersions = 10;
columns.push_back(ColumnDescriptor());
columns.back().name = "test";
try {
client1.createTable(tableName, columns);
} catch (const AlreadyExists &ae) {
std::cerr << "WARN: " << ae.message << std::endl;
}
}
catch (const TException &tx) {
std::cerr << "ERROR: " << tx.what() << std::endl;
}
return 0;
}
But i am getting the following exception at this place client1.createTable(tableName, columns);
ERROR: No more data to read.
Please help in resolving this.
Got it,
Need to start thrift server on hbase by .<hbaseinstallationpath>/bin/hbase thrift -threadpool start