1、文件->新建项目->qt widgets application->qwidget->完成
2、在.pro文件中增加
QT +=sql
3、代码
#include "widget.h" #include <QApplication> #include <QtSql/QSqlDatabase> #include <QtSql/QSqlQuery> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); int index=0; //连接数据库 QSqlDatabase db = QSqlDatabase::addDatabase("QOCI"); db.setPort(1521); db.setHostName("ip"); db.setDatabaseName("dbname"); db.setUserName("username"); db.setPassword("pwd"); if (db.open()) { qDebug()<< "RIGHT"; }else{ qDebug()<< "ERROR"; return 0; } QVariantList* pVarList = new QVariantList[3]; for ( index = 0; index < 3; index++ ) pVarList[index].clear(); QSqlQuery query("SELECT id,name from user where rownum<=6"); while (query.next()) { for ( index = 0; index < 3; index++ ) pVarList[index] <<query.value(index); } qDebug()<< pVarList[2].value(1).toString(); qDebug()<< "read over"; return a.exec(); }