4#include <QCoreApplication>
15 db.setHostName(
"acidalia");
16 db.setDatabaseName(
"customdb");
17 db.setUserName(
"mojito");
18 db.setPassword(
"J0a1m8");
36 QSqlField field(
"age", QMetaType::fromType<int>());
44 QSqlField field(
"age", QMetaType::fromType<int>());
70 while (
query.next()) {
81 int fieldNo =
query.record().indexOf(
"country");
82 while (
query.next()) {
93 query.prepare(
"INSERT INTO person (id, forename, surname) "
94 "VALUES (:id, :forename, :surname)");
95 query.bindValue(
":id", 1001);
96 query.bindValue(
":forename",
"Bart");
97 query.bindValue(
":surname",
"Simpson");
106 query.prepare(
"INSERT INTO person (id, forename, surname) "
107 "VALUES (:id, :forename, :surname)");
108 query.bindValue(0, 1001);
109 query.bindValue(1,
"Bart");
110 query.bindValue(2,
"Simpson");
119 query.prepare(
"INSERT INTO person (id, forename, surname) "
121 query.bindValue(0, 1001);
122 query.bindValue(1,
"Bart");
123 query.bindValue(2,
"Simpson");
132 query.prepare(
"INSERT INTO person (id, forename, surname) "
134 query.addBindValue(1001);
135 query.addBindValue(
"Bart");
136 query.addBindValue(
"Simpson");
145 query.prepare(
"CALL AsciiToInt(?, ?)");
146 query.bindValue(0,
"A");
149 int i =
query.boundValue(1).toInt();
214 model.setTable(
"employee");
227 db.setHostName(
"bigblue");
228 db.setDatabaseName(
"flightdb");
229 db.setUserName(
"acarlson");
230 db.setPassword(
"1uTbSbAs");
257 query.exec(
"SELECT name, salary FROM employee WHERE salary > 50000");
261 while (
query.next()) {
274 query.exec(
"SELECT name, salary FROM employee WHERE salary > 50000");
278 numRows =
query.size();
282 numRows =
query.at() + 1;
292 query.exec(
"INSERT INTO employee (id, name, salary) "
293 "VALUES (1001, 'Thad Beaumont', 65000)");
301 query.prepare(
"INSERT INTO employee (id, name, salary) "
302 "VALUES (:id, :name, :salary)");
303 query.bindValue(
":id", 1001);
304 query.bindValue(
":name",
"Thad Beaumont");
305 query.bindValue(
":salary", 65000);
314 query.prepare(
"INSERT INTO employee (id, name, salary) "
316 query.addBindValue(1001);
317 query.addBindValue(
"Thad Beaumont");
318 query.addBindValue(65000);
327 query.exec(
"UPDATE employee SET salary = 70000 WHERE id = 1003");
335 query.exec(
"DELETE FROM employee WHERE id = 1007");
344 query.exec(
"SELECT id FROM employee WHERE name = 'Torild Halvorsen'");
346 int employeeId =
query.value(0).toInt();
347 query.exec(
"INSERT INTO project (id, name, ownerid) "
348 "VALUES (201, 'Manhattan Project', "
373 model.setTable(
"employee");
374 model.setFilter(
"salary > 50000");
389 model.setTable(
"employee");
437 bool isNull(
int )
override {
return false; }
439 bool fetch(
int )
override {
return false; }
442 int size()
override {
return 0; }
458 int ,
const QString & )
override
QVariant data(const QModelIndex &item, int role) const override
[15]
virtual Q_INVOKABLE bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the role data for the item at index to value.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the data in row and column with parent.
qsizetype size() const noexcept
const_reference at(qsizetype i) const noexcept
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
The QSqlDatabase class handles a connection to a database.
QSqlDriver * driver() const
Returns the database driver used to access the database connection.
bool transaction()
Begins a transaction on the database if the driver supports transactions.
static QSqlDatabase addDatabase(const QString &type, const QString &connectionName=QLatin1StringView(defaultConnection))
\threadsafe
static QSqlDatabase database(const QString &connectionName=QLatin1StringView(defaultConnection), bool open=true)
\threadsafe
bool commit()
Commits a transaction to the database if the driver supports transactions and a transaction() has bee...
The QSqlDriver class is an abstract base class for accessing specific SQL databases.
DriverFeature
This enum contains a list of features a driver might support.
virtual bool hasFeature(DriverFeature f) const =0
Returns true if the driver supports feature feature; otherwise returns false.
The QSqlField class manipulates the fields in SQL database tables and views.
void setValue(const QVariant &value)
Sets the value of the field to value.
The QSqlQueryModel class provides a read-only data model for SQL result sets.
QVariant data(const QModelIndex &item, int role=Qt::DisplayRole) const override
Returns the value for the specified item and role.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setQuery(QSqlQuery &&query)
Resets the model and sets the data provider to be the given query.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
\reimp
QSqlRecord record(int row) const
Returns the record containing information about the fields of the current query.
The QSqlQuery class provides a means of executing and manipulating SQL statements.
The QSqlRecord class encapsulates a database record.
QVariant value(int i) const
Returns the value of the field located at position index in the record.
The QSqlResult class provides an abstract interface for accessing data from specific SQL databases.
const QSqlDriver * driver() const
Returns the driver associated with the result.
The QSqlTableModel class provides an editable data model for a single database table.
\macro QT_RESTRICTED_CAST_FROM_ASCII
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
void close() override
Derived classes must reimplement this pure virtual function in order to close the database connection...
QSqlResult * createResult() const override
Creates an empty SQL result on the database.
bool open(const QString &, const QString &, const QString &, const QString &, int, const QString &) override
Derived classes must reimplement this pure virtual function to open a database connection on database...
bool hasFeature(DriverFeature) const override
Returns true if the driver supports feature feature; otherwise returns false.
bool fetch(int) override
Positions the result to an arbitrary (zero-based) row index.
int numRowsAffected() override
Returns the number of rows affected by the last query executed, or -1 if it cannot be determined or i...
QVariant data(int) override
Returns the data for field index in the current row as a QVariant.
bool fetchLast() override
Positions the result to the last record (last row) in the result.
bool fetchFirst() override
Positions the result to the first record (row 0) in the result.
bool reset(const QString &) override
Sets the result to use the SQL statement query for subsequent data retrieval.
QSqlRecord record() const override
Returns the current record if the query is active; otherwise returns an empty QSqlRecord.
XyzResult(const QSqlDriver *driver)
bool isNull(int) override
Returns true if the field at position index in the current row is null; otherwise returns false.
int size() override
Returns the size of the SELECT result, or -1 if it cannot be determined or if the query is not a SELE...
myCustomObject doSomething()
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLenum GLsizei void GLsizei void * column
GLenum GLenum GLsizei void * row
void QSqlField_snippets()
void sql_intro_snippets()
void QSqlDatabase_snippets()
void QSqlTableModel_snippets()
[23]
void QSqlQueryModel_snippets()
void QSqlQuery_snippets()
QSqlQueryModel * model
[16]
QApplication app(argc, argv)
[0]