PDA

View Full Version : How to load Mysql driver?



adutzu89
16th December 2013, 14:20
I am sorry for posting this question but can someone explain me how can I load the QMYSQL driver into my application?

QSqlDatabase db =QSqlDatabase::addDatabase("QMYSQL") gives me the following errors:


QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7

wysota
16th December 2013, 14:39
Please post a minimal compilable example reproducing the problem.

adutzu89
16th December 2013, 15:10
I used a simple structure just to test if it is working with QMainWindow.
Added menu items on menu, and on "triggered" open the database connection


#include "default.h"
#include "ui_default.h"
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QLayout>

default::default(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::default)
{
ui->setupUi(this);
}

default::~default()
{
delete ui;
}
void default::on_actionSetari_Baza_de_Date_triggered()
{
QSqlDatabase db =QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("db");
db.setUserName("user");
db.setPassword("password");
db.open();
}

wysota
16th December 2013, 15:19
This is not a compilable example. Please post a minimal compilable example reproducing the problem.

adutzu89
16th December 2013, 20:22
I am sorry but what do you want?Project,Header,source files and UI-form?

untitled.pro file:

QT += core gui\
sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase db =QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("papadopoulos");
db.setUserName("root");
db.setPassword("");
db.open();
}

MainWindow::~MainWindow()
{
delete ui;
}


form xml version

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

wysota
17th December 2013, 07:23
Are the dependencies of the qmysql driver met? In particular do you have mysqlclient library available where your dynamic linker can find it?

adutzu89
17th December 2013, 08:52
I have installed Qt 5.2.0 using offline installer,did not build it from source and I have installed mysql client using the software center and mysql server using tasksel.
I am new to Qt and extremely new in QtSql, so can you be more specific?
Thank you for taking your time to help.
Forgot to say that I am on Ubuntu

Update:
I found out from this (http://www.qtcentre.org/threads/55365-QSqlDatabase-QMYSQL-driver-not-loaded-but-available) thread that I am missing libmysqlclient_r.so.16.

I managed to fix it using
adi@adi-5732Z:/usr/lib$ sudo ln -s x86_64-linux-gnu/libmysqlclient_r.so libmysqlclient_r.so.16

prkhr4u
21st December 2013, 04:14
I am using Qt 4.8 and faced the same problem.Here is what I did:
//for development work

apt-get install libmysqlclient-dev
add this to your .pro file

INCLUDEPATH += /usr/include/mysql
finally

apt-get install libqt4-sql-mysql