PDA

View Full Version : How to show connected sqlite database in Tableview



Clandeik
18th May 2014, 14:20
So hello,I have an existing database in my project.I connected my database to my prog.I can't understand how to show my database in Tableview.Please look at my project, what I'm doing wrong?

10369

mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

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

QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("C:/GLOSSARIY/Ядерная медицина.sqlite");
mydb.open();

if (!mydb.open())
ui->label->setText("Failed to open your database");
else
ui->label->setText("Connected succesfully...");

}


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

{
QSqlQueryModel * model = new QSqlQueryModel();
QSqlQuery query;
query.exec("SELECT * FROM Глоссарий по радиобиологи ¸");
model->setQuery(query);
ui->tableView->setModel(model);
}


mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtSql>
#include <QtDebug>
#include <QFileInfo>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

//public:
//QSqlDatabase mydb;

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

private slots:
void on_pushButton_clicked();

private:
Ui::MainWindow *ui;

QSqlQueryModel *model;
QSqlDatabase mydb;
};

#endif // MAINWINDOW_H

Lesiok
18th May 2014, 15:26
I think that SQL query is NOT executed (QSqlQuery::exec returns false) because You have spaces in name of table.
It should looks like :

query.exec("SELECT * FROM \"Глоссарий по радиобиологи ¸\"");

Clandeik
18th May 2014, 18:03
Thank you Lesiok!....interesting, how I can stretch the field in the table to see all the word in it, ....I tried use
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); but it does not show all the text!
solved this problem never mind.