PDA

View Full Version : QSqlite problem



dragon
21st March 2007, 17:05
Hello anyone,

I using Qt-4.2.3 on Windows XP
I have made a database called freesdb and insert the records through a dialog and works fine.
The problem is i can insert into the database up to 256 records and then in the same direktory where the freesdb is staying a another database pops up called freesdb-journal.
These freesdb-journal is blockking further inserting records in my freesdb.
I read over the net there no limitation of records in Sqlite database.

How is possible to get more records in my freesdb. ( more then 256 records).

Here my code for the connection for my database.
connection.h


#ifndef CONNECTION_H
#define CONNECTION_H

#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>


static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("frezen");
db.setHostName("localhost");
db.setUserName("jan");
db.setPassword("asdfgh");
if (!db.open()) {
QMessageBox::warning(0, ("Cannot open database"),
("Unable to establish a database connection.\n") + db.lastError().text());
return false;
}
QSqlQuery query;
query.exec("create table frezen (materiaal varchar(20), treksterkte varchar(20),"
"freestype varchar(20), freesdiameter varchar(20), aantaltanden varchar(20),"
"koeling varchar(10), freesbewerking varchar(20), snijsnelheid varchar(20),"
"voedingpertand varchar(20), snedediepte varchar(10), snedebreedte varchar(10))");

return true;
}

#endif


Here my code for inserting records
frezen.cpp


#include <QtGui>
#include <QtSql>
#include <QSqlError>
#include <QSqlRecord>

#include "frezen.h"
#include "frees.h"


Frezen::Frezen(const QString &frezen, QWidget *parent)
: QDialog(parent)
{
model = new QSqlTableModel(this);
model->setTable(frezen);
model->setSort(1, Qt::DescendingOrder);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();

model->setHeaderData(0, Qt::Horizontal, tr("Materiaal"));
model->setHeaderData(1, Qt::Horizontal, tr("Treksterkte"));
model->setHeaderData(2, Qt::Horizontal, tr("Frees type"));
model->setHeaderData(3, Qt::Horizontal, tr("Frees diameter"));
model->setHeaderData(4, Qt::Horizontal, tr("Aantal tanden"));
model->setHeaderData(5, Qt::Horizontal, tr("Koeling"));
model->setHeaderData(6, Qt::Horizontal, tr("Frees bewerking"));
model->setHeaderData(7, Qt::Horizontal, tr("Snijsnelheid"));
model->setHeaderData(8, Qt::Horizontal, tr("Voeding per tand"));
model->setHeaderData(9, Qt::Horizontal, tr("Snedediepte"));
model->setHeaderData(10, Qt::Horizontal, tr("Snedebreedte"));



QTableView *view = new QTableView;
view->setModel(model);


addButton = new QPushButton("Toevoegen");
removeButton = new QPushButton(" Verwijderen");
submitButton = new QPushButton(tr("Submit"));
submitButton->setDefault(true);
quitButton = new QPushButton("Afsluiten");

connect(submitButton, SIGNAL(clicked()), this, SLOT(submit()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(addButton, SIGNAL(clicked()), this, SLOT(setData()));
connect(removeButton, SIGNAL(clicked()), this, SLOT(deleteRow()));


QVBoxLayout *buttonLayout = new QVBoxLayout;
buttonLayout->addWidget(addButton);
buttonLayout->addWidget(removeButton);
buttonLayout->addWidget(submitButton);
buttonLayout->addStretch(1);
buttonLayout->addWidget(quitButton);

QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(view);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);

setWindowTitle(tr("Frezen"));
resize (400, 400);
}

void Frezen::submit()
{
model->database().transaction();
if (model->submitAll()) {
model->database().commit();
} else {
model->database().rollback();
QMessageBox::warning(this, tr("frezen"),
tr("The database reported an error: %1")
.arg(model->lastError().text()));
}
}



void Frezen::setData()

{
freesDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) {

QString materiaal = dlg.matComboBox->currentText();
QString treksterkte = dlg.trekComboBox->currentText();
QString freestype = dlg.freesComboBox->currentText();
QString freesdiameter = dlg.diaComboBox->currentText();
QString aantaltanden= dlg.tandComboBox->currentText();
QString koeling = dlg.koelComboBox->currentText();
QString freesbewerking = dlg.freesbComboBox->currentText();
QString snijsnelheid = dlg.snijComboBox->currentText();
QString voedingpertand = dlg.voedingComboBox->currentText();
QString snedediepte = dlg.snedeComboBox->currentText();
QString snedebreedte = dlg.snedebComboBox->currentText();




model->insertRows(0, 1);
model->setData(model->index(0, 0), materiaal);
model->setData(model->index(0, 1), treksterkte);
model->setData(model->index(0, 2), freestype);
model->setData(model->index(0, 3), freesdiameter);
model->setData(model->index(0, 4), aantaltanden);
model->setData(model->index(0, 5), koeling);
model->setData(model->index(0, 6), freesbewerking);
model->setData(model->index(0, 7), snijsnelheid);
model->setData(model->index(0, 8), voedingpertand);
model->setData(model->index(0, 9), snedediepte);
model->setData(model->index(0, 10), snedebreedte);

return;
}

}

void Frezen::deleteRow()
{


}




Thanks in advance

josbosmans
10th April 2007, 20:25
I also had some trouble with this in the past. I don't know all the details, but for some reason QT only seems to fetch 256 rows from the database, if more are present the behaviour you described arises.
I do the following (after line 17 of your 2nd code block, with table instead of model)


while (table->canFetchMore) {
table->fetchMore();
}

This makes sure that the entire table is correctly loaded.

croland
11th April 2007, 02:02
Josbosmans is correct. It's because you are using a sqlite database with the QSqlTableModel. See the following for the solution Josbosmans described:

http://doc.trolltech.com/4.2/qsqlquerymodel.html#fetchMore

Also take a look at this post, go down to the bottom of message 6 in the thread:

http://lists.trolltech.com/qt4-preview-feedback/2004-07/thread00161-0.html