void widget::slotShowRecord(){ QSqlTableModel* model = new QSqlTableModel(); model->setTable(QObject::tr("film")); model->select(); m_DbView->setModel(model);}
qDebug()<<model->lastError().text();
#include "widget.h"/////////////////////////////////////////////////////////////////////////widget::widget(QWidget* pWgt): QWidget(pWgt){ QTextCodec::setCodecForTr(QTextCodec::codecForName("cp1251")); QPushButton* pCrButton = new QPushButton(QObject::tr("Создать")); QPushButton* pDpButton = new QPushButton(QObject::tr("Показать")); connect(pCrButton, SIGNAL(clicked()), this, SLOT(slotCreateRecord())); connect(pDpButton, SIGNAL(clicked()), this, SLOT(slotShowRecord())); m_nameFilm = new QLineEdit; m_nameCarr = new QComboBox; m_nameGenre = new QComboBox; m_DbView = new QTableView; QLabel* pTextFilm = new QLabel(QObject::tr("Название")); QLabel* pTextCarr = new QLabel(QObject::tr("Носитель")); QLabel* pTextGenre = new QLabel(QObject::tr("Жанр")); pTextFilm->setAlignment(Qt::AlignCenter); pTextCarr->setAlignment(Qt::AlignCenter); pTextGenre->setAlignment(Qt::AlignCenter); QVBoxLayout* pVMain = new QVBoxLayout(); QVBoxLayout* pVLtFilm = new QVBoxLayout(); QVBoxLayout* pVLtGenre = new QVBoxLayout(); QVBoxLayout* pVLtCarr = new QVBoxLayout(); QHBoxLayout* pHLtData = new QHBoxLayout(); QHBoxLayout* pHLtButt = new QHBoxLayout(); pVLtFilm->addWidget(pTextFilm); pVLtFilm->addWidget(m_nameFilm); pVLtGenre->addWidget(pTextGenre); pVLtGenre->addWidget(m_nameGenre); pVLtCarr->addWidget(pTextCarr); pVLtCarr->addWidget(m_nameCarr); pHLtData->addLayout(pVLtFilm); pHLtData->addLayout(pVLtGenre); pHLtData->addLayout(pVLtCarr); pHLtButt->addWidget(pCrButton); pHLtButt->addWidget(pDpButton); pVMain->addLayout(pHLtData); pVMain->addLayout(pHLtButt); pVMain->addWidget(m_DbView); setLayout(pVMain); readTextFile(QObject::tr("настройки/жанры.txt"), m_nameGenre); readTextFile(QObject::tr("настройки/носители.txt"), m_nameCarr); createConnection(); createTable();}//--------------------------------------------------------------------bool widget::createConnection(){ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "DiskData"); db.setDatabaseName(QObject::tr("DiskDb")); if(!db.open()) { qDebug() << "Can't a database"; return false; } else qDebug() << "Create a DB"; return true;}//--------------------------------------------------------------------void widget::createTable(){ QSqlDatabase db = QSqlDatabase::database("DiskData"); QSqlQuery query(db); QString str = "CREATE TABLE film(id INTEGER PRIMARY KEY NOT NULL, " "name VARCHAR(50) UNIQUE," "genre VARCHAR(20)," "carrier VARCHAR(15))"; if(!query.exec(str)) qDebug() << "Can't create a table"; else qDebug() << "create a table";}//--------------------------------------------------------------------void widget::slotCreateRecord(){ QSqlDatabase db = QSqlDatabase::database("DiskData"); QSqlQuery query(db); QString strF = "INSERT INTO film(name, genre, carrier) " "VALUES('%1', '%2', '%3')"; QString str = strF.arg(m_nameFilm->text()) .arg(m_nameGenre->currentText()) .arg(m_nameCarr->currentText()); if(!query.exec(str)) qDebug() << "Data not record in a table"; else qDebug() << "Data record in a table";}//--------------------------------------------------------------------void widget::slotShowRecord(){ QSqlTableModel* model = new QSqlTableModel(); model->setTable(QObject::tr("film")); model->select(); qDebug()<<model->lastError().text(); m_DbView->setModel(model); m_DbView->show();}//--------------------------------------------------------------------void widget::readTextFile(QString nameFile, QComboBox* pComboBox){ QFile file(nameFile); if(!file.open(QIODevice::ReadOnly|QIODevice::Text)) qDebug() << QObject::tr("Невозможно открыть файл"); else { qDebug() << QObject::tr("Файл открыт"); QTextStream in(&file); QString line = ""; while(!in.atEnd()) { line = in.readLine(); pComboBox->addItem(line); } file.close(); }}
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "DiskData");