返回介绍

QSqlField Class

发布于 2019-10-04 15:02:54 字数 5632 浏览 994 评论 0 收藏 0

The QSqlField class manipulates the fields in SQL database tables and views. More...

#include <qsqlfield.h>

List of all member functions.

Public Members

  • QSqlField ( constQString&fieldName = QString::null, QVariant::Typetype = QVariant::Invalid )
  • QSqlField ( constQSqlField&other )
  • QSqlField & operator= ( constQSqlField&other )
  • bool operator== ( constQSqlField&other ) const
  • virtual ~QSqlField ()
  • virtual void setValue ( constQVariant&value )
  • virtual QVariant value () const
  • virtual void setName ( constQString&name )
  • QString name () const
  • virtual void setNull ()
  • bool isNull () const
  • virtual void setReadOnly ( boolreadOnly )
  • bool isReadOnly () const
  • void clear ( boolnullify = TRUE )
  • QVariant::Type type () const

Detailed Description

The QSqlField class manipulates the fields in SQL database tables and views.

QSqlField represents the characteristics of a single column in a database table or view, such as the data type and column name. A field also contains the value of the database column, which can be viewed or changed.

Field data values are stored as QVariants. Using an incompatible type is not permitted. For example:

    QSqlField f( "myfield", QVariant::Int );
    f.setValue( QPixmap() );  // will not work
    

However, the field will attempt to cast certain data types to the field data type where possible:

    QSqlField f( "myfield", QVariant::Int );
    f.setValue( QString("123") ); // casts QString to int
    

QSqlField objects are rarely created explicitly in application code. They are usually accessed indirectly through QSqlRecord or QSqlCursor which already contain a list of fields. For example:

    QSqlCursor cur( "Employee" );        // create cursor using the 'Employee' table
    QSqlField* f = cur.field( "name" );  // use the 'name' field
    f->setValue( "Dave" );               // set field value
    ...
    

In practice we rarely need to extract a pointer to a field at all. The previous example would normally be written:

    QSqlCursor cur( "Employee" );
    cur.setValue( "name", "Dave" );
    ...
    

See also Database Classes.


Member Function Documentation

QSqlField::QSqlField ( constQString&fieldName = QString::null, QVariant::Typetype = QVariant::Invalid )

Constructs an empty field called fieldName of type type.

QSqlField::QSqlField ( constQSqlField&other )

Constructs a copy of other.

QSqlField::~QSqlField () [virtual]

Destroys the object and frees any allocated resources.

void QSqlField::clear ( boolnullify = TRUE )

Clears the value of the field. If the field is read-only, nothing happens. If nullify is TRUE (the default), the field is set to NULL.

bool QSqlField::isNull () const

Returns TRUE if the field is currently null, otherwise returns FALSE.

bool QSqlField::isReadOnly () const

Returns TRUE if the field's value is read only, otherwise FALSE.

QString QSqlField::name () const

Returns the name of the field.

Example: sql/overview/table4/main.cpp.

QSqlField& QSqlField::operator= ( constQSqlField&other )

Sets the field equal to other.

bool QSqlField::operator== ( constQSqlField&other ) const

Returns TRUE if the field is equal to other, otherwise returns FALSE. Fields are considered equal when the following field properties are the same:

  • name()
  • isNull()
  • value()
  • isReadOnly()

void QSqlField::setName ( constQString&name ) [virtual]

Sets the name of the field to name.

void QSqlField::setNull () [virtual]

Sets the field to NULL and clears the value using clear(). If the field is read-only, nothing happens.

See also isReadOnly() and clear().

void QSqlField::setReadOnly ( boolreadOnly ) [virtual]

Sets the read only flag of the field's value to readOnly.

See also setValue().

void QSqlField::setValue ( constQVariant&value ) [virtual]

Sets the value of the field to value. If the field is read-only (isReadOnly() returns TRUE), nothing happens. If the data type of value differs from the field's current data type, an attempt is made to cast it to the proper type. This preserves the data type of the field in the case of assignment, e.g. a QString to an integer data type. For example:

  QSqlCursor cur( "Employee" );                 // 'Employee' table
  QSqlField* f = cur.field( "student_count" );  // an integer field
  ...
  f->setValue( myLineEdit->text() );            // cast the line edit text to an integer
  

See also isReadOnly().

QVariant::Type QSqlField::type () const

Returns the field's type.

QVariant QSqlField::value () const [virtual]

Returns the internal value of the field as a QVariant.

Example: sql/overview/table4/main.cpp.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文