Qt: 数据库操作

Qt: 数据库操作
     QSqlQuery query("SELECT country FROM artist");
     while (query.next()) {
         QString country = query.value(0).toString();
         doSomething(country);
     }
     QSqlQuery query("SELECT * FROM artist");
     int fieldNo = query.record().indexOf("country");
     while (query.next()) {
         QString country = query.value(fieldNo).toString();
         doSomething(country);
     }

     QSqlQuery query;
     query.prepare("INSERT INTO person (id, forename, surname) "
                   "VALUES (:id, :forename, :surname)");
     query.bindValue(":id", 1001);
     query.bindValue(":forename", "Bart");
     query.bindValue(":surname", "Simpson");
     query.exec();

     QSqlQuery query;
     query.prepare("INSERT INTO person (id, forename, surname) "
                   "VALUES (:id, :forename, :surname)");
     query.bindValue(0, 1001);
     query.bindValue(1, "Bart");
     query.bindValue(2, "Simpson");
     query.exec();

     QSqlQuery query;
     query.prepare("INSERT INTO person (id, forename, surname) "
                   "VALUES (?, ?, ?)");
     query.addBindValue(1001);
     query.addBindValue("Bart");
     query.addBindValue("Simpson");
     query.exec();

     QSqlQuery query;
     query.exec("INSERT INTO employee (id, name, salary) "
                "VALUES (1001, 'Thad Beaumont', 65000)");

 QSqlQuery q("select * from employees");
 QSqlRecord rec = q.record();

 qDebug() << "Number of columns: " << rec.count();

 int nameCol = rec.indexOf("name"); // index of the field "name"
 while (q.next())
     qDebug() << q.value(nameCol).toString(); // output all names

你可能感兴趣的:(Qt: 数据库操作)