Hello,
I was doing some tests on alternative ways organize my Model/View approach, concerning access to a MySQL database.
To create the model e used *tabelaPessoa().
Then i tried to do the same with the connection, using *connection(QString newHostName, QString newDatabase, QString newUserName, QString newPassword).
When i tried to connect i get the message:
QSqlDatabasePrivate::database: unable to open database: "Access denied for user 'ODBC'@'localhost' (using password: NO) QMYSQL: Unable to connect"
QSqlDatabasePrivate::database: unable to open database: "Access denied for user 'ODBC'@'localhost' (using password: NO) QMYSQL: Unable to connect"
To copy to clipboard, switch view to plain text mode
Why is the message refering to ODBC .... ?
This is the header file:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QSqlTableModel>
#include <QTableView>
#include <QItemSelectionModel>
#include <QMessageBox>
#include <QDebug>
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private slots:
void on_connectDisconnect_clicked();
void on_inserirUmRegisto_clicked();
void on_eliminarRegisto_clicked();
void on_atualizar_clicked();
private:
Ui::Dialog *ui;
//Tabela Pessoa
{
model->setTable("pessoa");
model->select();
model->setHeaderData(0, Qt::Horizontal, "ID");
model->setHeaderData(1, Qt::Horizontal, "Nome");
model->setHeaderData(2, Qt::Horizontal, "Data Nasc.");
model->setHeaderData(3, Qt::Horizontal, "Peso");
model->setHeaderData(4, Qt::Horizontal, "Observações");
return model;
}
{
db->addDatabase("QMYSQL");
db->setHostName(newHostName);
db->setDatabaseName(newDatabase);
db->setUserName(newUserName);
db->setPassword(newPassword);
return db;
}
};
#endif // DIALOG_H
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QSqlTableModel>
#include <QTableView>
#include <QItemSelectionModel>
#include <QMessageBox>
#include <QDebug>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void on_connectDisconnect_clicked();
void on_inserirUmRegisto_clicked();
void on_eliminarRegisto_clicked();
void on_atualizar_clicked();
private:
Ui::Dialog *ui;
QSqlDatabase *db;
QSqlTableModel *pessoa;
QTableView *vista;
//Tabela Pessoa
QSqlTableModel *tabelaPessoa()
{
QSqlTableModel *model = new QSqlTableModel;
model->setTable("pessoa");
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->select();
model->setHeaderData(0, Qt::Horizontal, "ID");
model->setHeaderData(1, Qt::Horizontal, "Nome");
model->setHeaderData(2, Qt::Horizontal, "Data Nasc.");
model->setHeaderData(3, Qt::Horizontal, "Peso");
model->setHeaderData(4, Qt::Horizontal, "Observações");
return model;
}
QSqlDatabase *connection(QString newHostName, QString newDatabase, QString newUserName, QString newPassword)
{
QSqlDatabase *db = new QSqlDatabase();
db->addDatabase("QMYSQL");
db->setHostName(newHostName);
db->setDatabaseName(newDatabase);
db->setUserName(newUserName);
db->setPassword(newPassword);
return db;
}
};
#endif // DIALOG_H
To copy to clipboard, switch view to plain text mode
Ahhh ... i get it
QSqlDatabase *db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));
To copy to clipboard, switch view to plain text mode
Bookmarks