QSqlQuery query("SELECT id, name, age FROM users"); QSqlRecord record = query.record(); qDebug() << "Field Names: " << record.fieldName(0) << record.fieldName(1) << record.fieldName(2);
QSqlTableModel model; model.setTable("users"); model.select(); for(int i = 0; i < model.rowCount(); i++){ QSqlRecord record = model.record(i); if (record.fieldName(2) == "age"){ model.setData(model.index(i, 2), 25); model.submitAll(); } }In this example, we use a QSqlTableModel to retrieve and manipulate database records. We call select() to retrieve all records from the users table, and loop through each record with a for loop. We then use record() to retrieve the record at the given index and check if the third field (i.e., age) has the correct name. If so, we set the value of that field to 25 using the setData() function of the model, and submit the changes to the database using submitAll(). Both of these examples use the QSqlRecord class and its fieldName() function to access field names of database records. The package library used in these examples is the Qt library, which provides a powerful set of classes and tools for building cross-platform applications.