无代码无真相,不解释了。 mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QObject::connect(ui->queryButton,SIGNAL(clicked()),this,SLOT(queryDatabaseSlot())); } MainWindow::~MainWindow() { delete ui; } #include <QtDebug> void MainWindow::queryDatabaseSlot() { //#include <QtSql> QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); //db.setHostName("bigblue"); db.setDatabaseName("student.db"); //db.setUserName("acarlson"); // db.setPassword("1uTbSbAs"); bool ok = db.open(); if(ok) { QSqlQuery query(db); query.exec("select *from information"); //store the data in buffer while (query.next()) { QString id = query.value(0).toString(); QString name= query.value(1).toString(); QString address=query.value(2).toString(); QString nickName=query.value(3).toString(); qDebug() << id << name<<address <<nickName; db.close(); } } else { QMessageBox::information(this,"Error Message",db.lastError().text()); } }