Created
April 11, 2013 02:36
-
-
Save anonymous/5360210 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtGui/QApplication> | |
#include <QMainWindow> | |
#include <QtSQL> | |
#include <QTableWidget> | |
#include <QMessageBox> | |
int main(int argc, char *argv[]) | |
{ | |
QApplication app(argc,argv); | |
QTableWidget* table = new QTableWidget(); | |
table->setWindowTitle("Connect to Mysql Database Example"); | |
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); | |
db.setHostName("localhost"); | |
db.setDatabaseName("example"); | |
db.setUserName("example"); | |
db.setPassword("password"); | |
if (!db.open()) | |
{ | |
QMessageBox::critical(0, QObject::tr("Database Error"), | |
db.lastError().text()); | |
} | |
QSqlQuery query("SELECT forename, surename FROM test"); | |
table->setColumnCount(query.record().count()); | |
table->setRowCount(query.size()); | |
int index=0; | |
while (query.next()) | |
{ | |
table->setItem(index,0,new QTableWidgetItem(query.value(0).toString())); | |
table->setItem(index,1,new QTableWidgetItem(query.value(1).toString())); | |
index++; | |
} | |
table->show(); | |
return app.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment