Driver not found error in Qt - mysql

I was trying to connect mysql with Qt 5.2.1. I ran a program but got the error
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
I ran the following command on terminal to solve the problem.
sudo apt-get install libqt5sql5-mysql
But problem remained unsolved.
The code is as follows.
#include <QtGui>
#include <QtSql/QSql>
#include <QTableWidget>
#include <QApplication>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QtSql/QSqlDriver>
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
QTableWidget* table = new QTableWidget();
table->setWindowTitle("Connect to Mysql Database Example");
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
qDebug()<<"abc";
db.setHostName("192.168.11.3");
db.setDatabaseName("menudb");
db.setUserName("root");
db.setPassword("test");
if (!db.open())
{
QMessageBox::critical(0, QObject::tr("Database Error"),
db.lastError().text());
}
QSqlQuery query("SELECT * FROM test");
table->setColumnCount(query.record().count());
table->setRowCount(query.size());
int index=0;
while (query.next())
{
table->setItem(index,0,new QTableWidgetItem(query.value(0).toString()));
table->setItem(index,1,new QTableWidgetItem(query.value(1).toString()));
index++;
}
table->show();
qDebug()<<"charu";
return app.exec();
}
Please help me to solve the problem.

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
That means that you have the MySql plugin driver for Qt, but probably you don't have the MySql client lib (libmysqlclient.so).
So, make sure that you have libmysqlclient.so in your library path.
To check the dependencies of the plugin use:
objdump -p /PathToQt/plugins/sqldrivers/libqsqlmysql.so | grep NEEDED

Related

error while connecting mariadb with c : undefined reference to `mysql_init#4'

I am trying to connect to mariadb database using c program. Initially it was showing error for #include <mysql.h> as no such file or directory.
But after including directory name, that problem is solved now, but it is showing another error.
Following is the code I was trying to run:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include "C:/Program Files/MariaDB 10.11/include/mysql/my_global.h"
#include "mysql/mysql.h"
int main (int argc, char* argv[])
{
// Initialize Connection
MYSQL *conn;
if (!(conn = mysql_init(0)))
{
fprintf(stderr, "unable to initialize connection struct\n");
exit(1);
}
// Connect to the database
if (!mysql_real_connect(
conn, // Connection
"mariadb.example.net", // Host
"db_user", // User account
"db_user_password", // User password
"test", // Default database
3306, // Port number
NULL, // Path to socket file
0 // Additional options
));
{
// Report the failed-connection error & close the handle
fprintf(stderr, "Error connecting to Server: %s\n", mysql_error(conn));
mysql_close(conn);
exit(1);
}
// Use the Connection
// ...
// Close the Connection
mysql_close(conn);
return 0;
}
I am getting following error in output:
PS C:\Dev\Win> gcc Db_con.c -o Db_con
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0x1e): undefined reference to `mysql_init#4'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xa1): undefined reference to `mysql_real_connect#32'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xaf): undefined reference to `mysql_error#4'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xd9): undefined reference to `mysql_close#4'
collect2.exe: error: ld returned 1 exit status
Can anyone explain what is the problem and how to solve it?
You have to link against the MariaDB Connector/C libraries.
From MariaDB Connector/C documentation:
Linking your application against MariaDB Connector/C
Windows
For static linking the library libmariadb.lib is required, for dynamic linking use libmariadb.dll. Using the MSI installer, these libraries can be found in the lib directory of your MariaDB Connector/C installation.
Unless you use the experimental plugin remote_io (which requires the curl library) there are no dependencies to other libraries than the Windows system libraries.

Error when connect my C script to a MySQL online Database

I'm trying to write a simple C script that can connect to an online MySQL Database. I have tried to use Connector/C (libmysqlclient) downloaded by https://dev.mysql.com/downloads/connector/c/ but don't work.
Library is recognized but when I'm compiling from terminal this is the result: Undefined symbols for architecture x86_64: "_mysql_init", referenced from: _main in test.o ld: symbol(s) not found for architecture x86_64
I tried to correct the error described above, but I can not solve the problem with any help.
This is my code test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#define SIZE 30
int main(int argc, char** argv) {
/* connect to database */
MYSQL mysql;
if(mysql_init(&mysql) == NULL){
printf("Error\n");
return 0;
}
/* other code */
return 0;
}

Qt 5.5 missing MYSQL driver

Today I started working with Qt 5.5 and my first project is to get GPS related data form a MYSQL database and send to another server. The problem is that I have a linking problem with the libqmysql.so driver file. I looked after how to solve the problem and I did the following steps so far:
I copied all the files (libqmysql.so and other drivers) into /usr/lib/i386-linux-gnu/qt5/plugins/sqldrivers and /home/magyarg/Qt5.5.1/5.5/gcc/plugins/sqldrivers
I ran ldd libqmysql.so to check what dependencies are needed; I got the following result:
According this result, I installed libssl and libmysqlclient18.
The problem:
After these steps Qt Creator still throws me the the error:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
The environment:
Ubuntu Linux 15.04(x86)
Qt5.5
The corresponding code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QtSql>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setDatabaseName("ugyfelhivo");
db.setUserName("root");
db.setPassword("pass");
bool ok = db.open();
if (ok == true) {
QLabel label;
label.setText("Macska");
}
}
Often this solution works to get the QMYSQL loaded:
cd /usr/lib/i386-linux-gnu/
sudo ln -s libmysqlclient_r.so.18 libmysqlclient_r.so.16

qt5.1.1 mysql ubuntu QMYSQL driver not loaded

I'm trying to access mysql using Qt5.1.1 but i am getting the error the error below. I also searched a lot on google but unable to fix it.Please suggest me a solution so that i am able to resolve this error.
error:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3
QSqlError(-1, “ driver not loaded”, “ driver not loaded”)
code:
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QtSql>
#include <QSqlDriver>
#include <qsqldatabase.h>
#include <QSqlError>
#include <QPluginLoader>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db= QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("NNF");
db.setUserName("root");
db.setPassword("root123");
if( !db.open() )
{
qDebug() << db.lastError();
qFatal( "Failed to connect." );
}
qDebug( "Connected!" );
return a.exec();
}
Well, for Qt5 you need install MySQL, using the next command on the terminal, you resolve the problem:
sudo apt-get install libqt5sql5-mysql
If you are Ubuntu linux OS, you can install library:
mic#ubt: ~$ apt-cache search libqt4-sql-mysql
libqt4-sql-mysql - Qt 4 MySQL database driver
mic#ubt: ~$ sudo apt-get install libqt4-sql-mysql

Possible to use thrift in a mysql plugin?

I'm using Mysql 5.5 and the plugin need to query a thrift interfaced server for some information. I created the thrift client which basically opens a connection to the server, gets a status, and then closes the connection:
#include "../../xxxx/gen-cpp/Xxxx.h"
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace ::za::co::xxxx;
int main(int argc, char **argv) {
boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
XxxxServiceClient client(protocol);
transport->open();
client.getStatus();
transport->close();
return 0;
}
I then changed main() to a function name and added it into the plugin code file and called it from the main function.
The plugin code builds fine but the map now contains a whole lot of thrift references and on trying to load the plugin, I get this error:
ERROR 1126 (HY000): Can't open shared library '/usr/lib/mysql/plugin/libxxxx.so' (errno: 13 undefined symbol: _ZTVN6apache6thrift9transport18TBufferedTransportE)
Is there any way to get these new thrift references resolved on installing the plugin? It installs and runs fine without the above code.
Using the Thrift cpp tutorial code I was able to create a simple hello world MySQL daemon plugin which made a client call to CppServer process:
#include <mysql/plugin.h>
#include <mysql_version.h>
#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>
#include "gen-cpp/Calculator.h"
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace tutorial;
using namespace shared;
using namespace boost;
static int hello_world_plugin_init(void *p) {
shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
shared_ptr<TTransport> transport(new TBufferedTransport(socket));
shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
CalculatorClient client(protocol);
transport->open();
client.ping();
transport->close();
return 0;
}
And this Makefile:
BOOST_DIR = /usr/include/boost
MYSQL_DIR = /usr/include/mysql
THRIFT_DIR = /usr/local/include/thrift
LIB_DIR = /usr/local/lib
GEN_SRC = gen-cpp/SharedService.cpp gen-cpp/shared_types.cpp gen-cpp/tutorial_types.cpp gen-cpp/Calculator.cpp
DEFS = -DMYSQL_DYNAMIC_PLUGIN -DHAVE_NETINET_IN_H
default: hello_thrift.cc
g++ ${DEFS} -fPIC -shared -o libhellothrift.so -I${MYSQL_DIR} -I${THRIFT_DIR} -I${BOOST_DIR} -Igen-cpp -L${LIB_DIR} hello_thrift.cc ${GEN_SRC} -lthrift
This is just example code and will crash your MySQL server if Thrift CppServer isn't running.
I tested this on Ubuntu 12.04 LTS using gcc 4.6.3, MySQL 5.5.24, Thrift 0.8.0, Boost 1.46