#include <QtGui>#include <QtSql>// ----------------------------------------------------------------------bool createConnection_1(){ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName(""); db.setUserName(""); db.setPassword(""); if (!db.open()) { qDebug() << "Cannot open database:" << db.lastError(); return false; } return true;}// ----------------------------------------------------------------------bool createConnection_2(){ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "test"); db.setHostName("localhost"); db.setDatabaseName(""); db.setUserName(""); db.setPassword(""); if (!db.open()) { qDebug() << "Cannot open database:" << db.lastError(); return false; } return true;}// ----------------------------------------------------------------------int main(int argc, char** argv){ QApplication app(argc, argv);/* if (!createConnection_1()) { return -1; }*/ if (!createConnection_2()) { return -1; } QWidget Widget; QLabel* pLabel = new QLabel(&Widget); pLabel->move(10, 40); QSqlQueryModel model; model.setQuery("SHOW DATABASES;"); if (model.lastError().isValid()) { qDebug() << model.lastError(); } QComboBox* pComboBox = new QComboBox(&Widget); pComboBox->setGeometry(10, 10, 280, 25); pComboBox->setModel(&model); int Count = pComboBox->count(); QString String = "Count base(s) = " + QString::number(Count); pLabel->setText(String); Widget.resize(300, 100); Widget.show(); return app.exec();}
#include <QtGui>#include <QtSql>// ----------------------------------------------------------------------bool createConnection_1(){ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName(""); db.setUserName(""); db.setPassword(""); if (!db.open()) { qDebug() << "Cannot open database:" << db.lastError(); return false; } return true;}// ----------------------------------------------------------------------bool createConnection_2(){ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "test"); db.setHostName("localhost"); db.setDatabaseName(""); db.setUserName(""); db.setPassword(""); if (!db.open()) { qDebug() << "Cannot open database:" << db.lastError(); return false; } return true;}// ----------------------------------------------------------------------int main(int argc, char** argv){ QApplication app(argc, argv); QSqlQueryModel model;/* if (!createConnection_1()) { return -1; } else { model.setQuery("SHOW DATABASES;"); }*/ if (!createConnection_2()) { return -1; } else { model.setQuery("SHOW DATABASES;", QSqlDatabase::database("test")); } QWidget Widget; QLabel* pLabel = new QLabel(&Widget); pLabel->move(10, 40); if (model.lastError().isValid()) { qDebug() << model.lastError(); } QComboBox* pComboBox = new QComboBox(&Widget); pComboBox->setGeometry(10, 10, 280, 25); pComboBox->setModel(&model); int Count = pComboBox->count(); QString String = "Count base(s) = " + QString::number(Count); pLabel->setText(String); Widget.resize(300, 100); Widget.show(); return app.exec();}
model.setQuery("SHOW DATABASES;");
model.setQuery("SHOW DATABASES;", QSqlDatabase::database("test"));