Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QSqlRecord Class Reference

The QSqlRecord class encapsulates a database record. More...

#include <qsqlrecord.h>

+ Inheritance diagram for QSqlRecord:
+ Collaboration diagram for QSqlRecord:

Public Member Functions

 QSqlRecord ()
 Constructs an empty record.
 
 QSqlRecord (const QSqlRecord &other)
 Constructs a copy of other.
 
 QSqlRecord (QSqlRecord &&other) noexcept=default
 
QSqlRecordoperator= (const QSqlRecord &other)
 Sets the record equal to other.
 
 ~QSqlRecord ()
 Destroys the object and frees any allocated resources.
 
void swap (QSqlRecord &other) noexcept
 
bool operator== (const QSqlRecord &other) const
 Returns true if this object is identical to other (i.e., has the same fields in the same order); otherwise returns false.
 
bool operator!= (const QSqlRecord &other) const
 Returns true if this object is not identical to other; otherwise returns false.
 
QVariant value (int i) const
 Returns the value of the field located at position index in the record.
 
QVariant value (const QString &name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QVariant value (QStringView name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the value of the field called name in the record.
 
void setValue (int i, const QVariant &val)
 Sets the value of the field at position index to val.
 
void setValue (const QString &name, const QVariant &val)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
void setValue (QStringView name, const QVariant &val)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the value of the field called name to val.
 
void setNull (int i)
 Sets the value of field index to null.
 
void setNull (const QString &name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
void setNull (QStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the value of the field called name to null.
 
bool isNull (int i) const
 Returns true if the field index is null or if there is no field at position index; otherwise returns false.
 
bool isNull (const QString &name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
bool isNull (QStringView name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns true if the field called name is null or if there is no field called name; otherwise returns false.
 
int indexOf (const QString &name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
int indexOf (QStringView name) const
 Returns the position of the field called name within the record, or -1 if it cannot be found.
 
QString fieldName (int i) const
 Returns the name of the field at position index.
 
QSqlField field (int i) const
 Returns the field at position index.
 
QSqlField field (const QString &name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QSqlField field (QStringView name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the field called name.
 
bool isGenerated (int i) const
 Returns true if the record has a field at position index and this field is to be generated (the default); otherwise returns false.
 
bool isGenerated (const QString &name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
bool isGenerated (QStringView name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns true if the record has a field called name and this field is to be generated (the default); otherwise returns false.
 
void setGenerated (const QString &name, bool generated)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
void setGenerated (QStringView name, bool generated)
 Sets the generated flag for the field called name to generated.
 
void setGenerated (int i, bool generated)
 Sets the generated flag for the field index to generated.
 
void append (const QSqlField &field)
 Append a copy of field field to the end of the record.
 
void replace (int pos, const QSqlField &field)
 Replaces the field at position pos with the given field.
 
void insert (int pos, const QSqlField &field)
 Inserts the field field at position pos in the record.
 
void remove (int pos)
 Removes the field at position pos.
 
bool isEmpty () const
 Returns true if there are no fields in the record; otherwise returns false.
 
bool contains (const QString &name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
bool contains (QStringView name) const
 Returns true if there is a field in the record called name; otherwise returns false.
 
void clear ()
 Removes all the record's fields.
 
void clearValues ()
 Clears the value of all fields in the record and sets each field to null.
 
int count () const
 Returns the number of fields in the record.
 
QSqlRecord keyValues (const QSqlRecord &keyFields) const
 

Detailed Description

The QSqlRecord class encapsulates a database record.

\inmodule QtSql

The QSqlRecord class encapsulates the functionality and characteristics of a database record (usually a row in a table or view within the database). QSqlRecord supports adding and removing fields as well as setting and retrieving field values.

The values of a record's fields can be set by name or position with setValue(); if you want to set a field to null use setNull(). To find the position of a field by name use indexOf(), and to find the name of a field at a particular position use fieldName(). Use field() to retrieve a QSqlField object for a given field. Use contains() to see if the record contains a particular field name.

When queries are generated to be executed on the database only those fields for which isGenerated() is true are included in the generated SQL.

A record can have fields added with append() or insert(), replaced with replace(), and removed with remove(). All the fields can be removed with clear(). The number of fields is given by count(); all their values can be cleared (to null) using clearValues().

See also
QSqlField, QSqlQuery::record()

Definition at line 19 of file qsqlrecord.h.

Constructor & Destructor Documentation

◆ QSqlRecord() [1/3]

QSqlRecord::QSqlRecord ( )

Constructs an empty record.

See also
isEmpty(), append(), insert()

Definition at line 66 of file qsqlrecord.cpp.

◆ QSqlRecord() [2/3]

QSqlRecord::QSqlRecord ( const QSqlRecord & other)
default

Constructs a copy of other.

QSqlRecord is \l{implicitly shared}. This means you can make copies of a record in \l{constant time}.

◆ QSqlRecord() [3/3]

QSqlRecord::QSqlRecord ( QSqlRecord && other)
defaultnoexcept
Since
6.6

Move-constructs a new QSqlRecord from other.

Note
The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.

◆ ~QSqlRecord()

QSqlRecord::~QSqlRecord ( )
default

Destroys the object and frees any allocated resources.

Member Function Documentation

◆ append()

void QSqlRecord::append ( const QSqlField & field)

Append a copy of field field to the end of the record.

See also
insert(), replace(), remove()

Definition at line 273 of file qsqlrecord.cpp.

References QList< T >::append(), field(), and QSqlRecordPrivate::fields.

Referenced by QOCICols::QOCICols(), QSqlIndex::append(), QIBaseResult::record(), QIBaseDriver::record(), and QMimerSQLDriver::record().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ clear()

void QSqlRecord::clear ( )

Removes all the record's fields.

See also
clearValues(), isEmpty()

Definition at line 328 of file qsqlrecord.cpp.

References QList< T >::clear(), and QSqlRecordPrivate::fields.

Referenced by QSqlTableModelPrivate::clear().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ clearValues()

void QSqlRecord::clearValues ( )

Clears the value of all fields in the record and sets each field to null.

See also
setValue()

Definition at line 371 of file qsqlrecord.cpp.

References QSqlRecordPrivate::fields.

◆ contains() [1/2]

bool QSqlRecord::contains ( const QString & name) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 350 of file qsqlrecord.cpp.

References contains().

Referenced by contains().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ contains() [2/2]

bool QSqlRecord::contains ( QStringView name) const

Returns true if there is a field in the record called name; otherwise returns false.

Definition at line 359 of file qsqlrecord.cpp.

References indexOf().

+ Here is the call graph for this function:

◆ count()

int QSqlRecord::count ( ) const

Returns the number of fields in the record.

See also
isEmpty()

Definition at line 515 of file qsqlrecord.cpp.

References QSqlRecordPrivate::fields, and QList< T >::size().

Referenced by QSqlQueryModelPrivate::columnInQuery(), QSqlTableModelPrivate::exec(), indexOf(), QSqlTableModelPrivate::initRecordAndPrimaryIndex(), QSqlQuery::record(), QSqlQueryModel::record(), QSqlTableModel::record(), selectEmployees(), QSqlRelationalTableModel::selectStatement(), and QSqlDriver::sqlStatement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ field() [1/3]

QSqlField QSqlRecord::field ( const QString & name) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 249 of file qsqlrecord.cpp.

References field().

+ Here is the call graph for this function:

◆ field() [2/3]

QSqlField QSqlRecord::field ( int index) const

Returns the field at position index.

If the index is out of range, function returns a \l{default-constructed value}.

Definition at line 241 of file qsqlrecord.cpp.

References QSqlRecordPrivate::fields, and QList< T >::value().

Referenced by QSqlIndex::append(), append(), QSqlIndex::append(), field(), field(), insert(), QDB2Driver::primaryIndex(), QODBCDriver::primaryIndex(), replace(), QSqlDriver::sqlStatement(), and QSqlRelationalTableModelPrivate::translateFieldNames().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ field() [3/3]

QSqlField QSqlRecord::field ( QStringView name) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the field called name.

If the field called name is not found, function returns a \l{default-constructed value}.

Definition at line 261 of file qsqlrecord.cpp.

References field(), and indexOf().

+ Here is the call graph for this function:

◆ fieldName()

QString QSqlRecord::fieldName ( int index) const

Returns the name of the field at position index.

If the field does not exist, an empty string is returned.

See also
indexOf()

Definition at line 191 of file qsqlrecord.cpp.

References QSqlRecordPrivate::fields, QSqlField::name, and QList< T >::value().

Referenced by indexOf(), QSqlRelationalTableModel::selectStatement(), and QSqlDriver::sqlStatement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ indexOf() [1/2]

int QSqlRecord::indexOf ( const QString & name) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 199 of file qsqlrecord.cpp.

References indexOf().

Referenced by contains(), field(), indexOf(), isGenerated(), isNull(), QSqlTableModelPrivate::nameToIndex(), QSqlRelationalTableModelPrivate::nameToIndex(), selectEmployees(), setGenerated(), setNull(), setValue(), and value().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ indexOf() [2/2]

int QSqlRecord::indexOf ( QStringView name) const

Returns the position of the field called name within the record, or -1 if it cannot be found.

Field names are not case-sensitive. If more than one field matches, the first one is returned.

See also
fieldName()

Definition at line 212 of file qsqlrecord.cpp.

References QList< T >::at(), Qt::CaseInsensitive, count(), fieldName(), QSqlRecordPrivate::fields, i, QString::left(), QString::mid(), and QSqlField::name.

+ Here is the call graph for this function:

◆ insert()

void QSqlRecord::insert ( int pos,
const QSqlField & field )

Inserts the field field at position pos in the record.

See also
append(), replace(), remove()

Definition at line 284 of file qsqlrecord.cpp.

References field(), QSqlRecordPrivate::fields, QList< T >::insert(), and pos.

Referenced by QMimerSQLResult::record().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isEmpty()

bool QSqlRecord::isEmpty ( ) const

Returns true if there are no fields in the record; otherwise returns false.

See also
append(), insert(), clear()

Definition at line 341 of file qsqlrecord.cpp.

References QSqlRecordPrivate::fields, and QList< T >::isEmpty().

Referenced by QSqlTableModel::primaryValues().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isGenerated() [1/3]

bool QSqlRecord::isGenerated ( const QString & name) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 480 of file qsqlrecord.cpp.

References isGenerated().

+ Here is the call graph for this function:

◆ isGenerated() [2/3]

bool QSqlRecord::isGenerated ( int index) const

Returns true if the record has a field at position index and this field is to be generated (the default); otherwise returns false.

See also
setGenerated()

Definition at line 504 of file qsqlrecord.cpp.

References QSqlRecordPrivate::fields, QSqlField::isGenerated(), and QList< T >::value().

Referenced by QSqlQueryModelPrivate::columnInQuery(), QSqlTableModelPrivate::exec(), isGenerated(), isGenerated(), and QSqlDriver::sqlStatement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isGenerated() [3/3]

bool QSqlRecord::isGenerated ( QStringView name) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns true if the record has a field called name and this field is to be generated (the default); otherwise returns false.

See also
setGenerated()

Definition at line 493 of file qsqlrecord.cpp.

References indexOf(), and isGenerated().

+ Here is the call graph for this function:

◆ isNull() [1/3]

bool QSqlRecord::isNull ( const QString & name) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 426 of file qsqlrecord.cpp.

References isNull().

+ Here is the call graph for this function:

◆ isNull() [2/3]

bool QSqlRecord::isNull ( int index) const

Returns true if the field index is null or if there is no field at position index; otherwise returns false.

See also
setNull()

Definition at line 418 of file qsqlrecord.cpp.

References QSqlRecordPrivate::fields, QSqlField::isNull(), and QList< T >::value().

Referenced by isNull(), isNull(), and QSqlDriver::sqlStatement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isNull() [3/3]

bool QSqlRecord::isNull ( QStringView name) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns true if the field called name is null or if there is no field called name; otherwise returns false.

See also
setNull()

Definition at line 439 of file qsqlrecord.cpp.

References indexOf(), and isNull().

+ Here is the call graph for this function:

◆ keyValues()

QSqlRecord QSqlRecord::keyValues ( const QSqlRecord & keyFields) const
Since
5.1 Returns a record containing the fields represented in keyFields set to values that match by field name.

Definition at line 585 of file qsqlrecord.cpp.

References i.

Referenced by QSqlTableModel::primaryValues().

+ Here is the caller graph for this function:

◆ operator!=()

bool QSqlRecord::operator!= ( const QSqlRecord & other) const
inline

Returns true if this object is not identical to other; otherwise returns false.

See also
operator==()

Definition at line 32 of file qsqlrecord.h.

References operator==(), and other().

+ Here is the call graph for this function:

◆ operator=()

QSqlRecord & QSqlRecord::operator= ( const QSqlRecord & other)
default

Sets the record equal to other.

Since
6.6

Move-assigns other to this QSqlRecord instance.

Note
The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.

QSqlRecord is \l{implicitly shared}. This means you can make copies of a record in \l{constant time}.

Referenced by QSqlIndex::operator=().

+ Here is the caller graph for this function:

◆ operator==()

bool QSqlRecord::operator== ( const QSqlRecord & other) const

Returns true if this object is identical to other (i.e., has the same fields in the same order); otherwise returns false.

See also
operator!=()

Definition at line 145 of file qsqlrecord.cpp.

References QSqlRecordPrivate::fields, and other().

+ Here is the call graph for this function:

◆ remove()

void QSqlRecord::remove ( int pos)

Removes the field at position pos.

If pos is out of range, nothing happens.

See also
append(), insert(), replace()

Definition at line 313 of file qsqlrecord.cpp.

References QSqlRecordPrivate::contains(), QSqlRecordPrivate::fields, pos, and QList< T >::remove().

+ Here is the call graph for this function:

◆ replace()

void QSqlRecord::replace ( int pos,
const QSqlField & field )

Replaces the field at position pos with the given field.

If pos is out of range, nothing happens.

See also
append(), insert(), remove()

Definition at line 297 of file qsqlrecord.cpp.

References QSqlRecordPrivate::contains(), field(), QSqlRecordPrivate::fields, and pos.

+ Here is the call graph for this function:

◆ setGenerated() [1/3]

void QSqlRecord::setGenerated ( const QString & name,
bool generated )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 381 of file qsqlrecord.cpp.

References setGenerated().

Referenced by QSqlTableModel::record(), setGenerated(), and setGenerated().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setGenerated() [2/3]

void QSqlRecord::setGenerated ( int index,
bool generated )

Sets the generated flag for the field index to generated.

See also
isGenerated()

Definition at line 404 of file qsqlrecord.cpp.

References QSqlRecordPrivate::contains(), and QSqlRecordPrivate::fields.

+ Here is the call graph for this function:

◆ setGenerated() [3/3]

void QSqlRecord::setGenerated ( QStringView name,
bool generated )

Sets the generated flag for the field called name to generated.

If the field does not exist, nothing happens. Only fields that have generated set to true are included in the SQL that is generated by QSqlQueryModel for example.

See also
isGenerated()

Definition at line 393 of file qsqlrecord.cpp.

References indexOf(), and setGenerated().

+ Here is the call graph for this function:

◆ setNull() [1/3]

void QSqlRecord::setNull ( const QString & name)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 461 of file qsqlrecord.cpp.

References setNull().

+ Here is the call graph for this function:

◆ setNull() [2/3]

void QSqlRecord::setNull ( int index)

Sets the value of field index to null.

If the field does not exist, nothing happens.

See also
setValue()

Definition at line 450 of file qsqlrecord.cpp.

References QList< T >::clear(), QSqlRecordPrivate::contains(), and QSqlRecordPrivate::fields.

Referenced by setNull(), and setNull().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setNull() [3/3]

void QSqlRecord::setNull ( QStringView name)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the value of the field called name to null.

If the field does not exist, nothing happens.

Definition at line 472 of file qsqlrecord.cpp.

References indexOf(), and setNull().

+ Here is the call graph for this function:

◆ setValue() [1/3]

void QSqlRecord::setValue ( const QString & name,
const QVariant & val )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 538 of file qsqlrecord.cpp.

References setValue().

+ Here is the call graph for this function:

◆ setValue() [2/3]

void QSqlRecord::setValue ( int index,
const QVariant & val )

Sets the value of the field at position index to val.

If the field does not exist, nothing happens.

See also
setNull()

Definition at line 527 of file qsqlrecord.cpp.

References QSqlRecordPrivate::contains(), and QSqlRecordPrivate::fields.

Referenced by QSqlQuery::record(), QSqlTableModelPrivate::record(), QSqlQueryModel::record(), setValue(), and setValue().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setValue() [3/3]

void QSqlRecord::setValue ( QStringView name,
const QVariant & val )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the value of the field called name to val.

If the field does not exist, nothing happens.

See also
setNull()

Definition at line 550 of file qsqlrecord.cpp.

References indexOf(), and setValue().

+ Here is the call graph for this function:

◆ swap()

void QSqlRecord::swap ( QSqlRecord & other)
inlinenoexcept
Since
6.6

Swaps SQL record other with this SQL record. This operation is very fast and never fails.

Definition at line 29 of file qsqlrecord.h.

References d, and other().

Referenced by QSqlIndex::swap().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ value() [1/3]

QVariant QSqlRecord::value ( const QString & name) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 166 of file qsqlrecord.cpp.

◆ value() [2/3]

QVariant QSqlRecord::value ( int index) const

Returns the value of the field located at position index in the record.

If index is out of bounds, an invalid QVariant is returned.

See also
fieldName(), isNull()

Definition at line 158 of file qsqlrecord.cpp.

References QSqlRecordPrivate::fields, QList< T >::value(), and QSqlField::value.

Referenced by QSqlTableModelPrivate::exec(), QSQLiteDriverPrivate::getTableInfo(), QSqlQueryModel_snippets(), QSqlTableModel_snippets(), and sql_intro_snippets().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ value() [3/3]

QVariant QSqlRecord::value ( QStringView name) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the value of the field called name in the record.

If field name does not exist an invalid variant is returned.

See also
indexOf(), isNull()

Definition at line 179 of file qsqlrecord.cpp.

References indexOf().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: