Selecting data from a database without using SQL statements in Qt
Jump to navigation
Jump to search
Overview[edit | edit source]
The table 'person' has the following columns:
- 76ytuiytuityutyutututyutyutyu, this is an auto-increment field
- 76ytuiytuityutyutututyutyutyu
- 76ytuiytuityutyutututyutyutyu
- 76ytuiytuityutyutututyutyutyu
Preconditions[edit | edit source]
Download and install the Qt SDK
For Maemo SQLite development, the following packages must be installed:
- libqt4-sql
- libqt4-sql-sqlite
- libsqlite3-0
- libsqlite3-dev
Header[edit | edit source]
- include <QSqlTableModel>
- include <QSqlRecord>
- include <QString>
Source[edit | edit source]
Select person data. The person ID is given as a filter for the selection.
QString MyWidget::searchPersonName(int personId)
{
QString name;
QSqlTableModel *model = new QSqlTableModel;
// Set used table
model->setTable("person");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
// Set where clause
model->setFilter(QString("id=%1").arg(personId));
model->select();
//The index of the record we are interested in
int index = 0;
// Read result
QSqlRecord record = model->record(index);
if (!record.isEmpty())
name = record.value(2).toString();
return name;
}
Postconditions[edit | edit source]
The person's data is selected from the database without using SQL statements.
See also[edit | edit source]
- Creating an SQLite database in Qt
- Creating a database table in Qt
- Inserting a row into a database in Qt
- Searching for data in a database in Qt
- Deleting data from a database in Qt
- Using QDataWidgetMapper to show data from a database in Qt
- QtSql module
pt:Como selecionar informações do banco de dados sem o uso de declaração SQL em Qt