Linux系统下Qt5无法连接MySQL数据库的解决方法(2)

1 #include <QCoreApplication> 2 #include <QSqlDatabase> 3 #include <QSqlQuery> 4 #include <QSqlTableModel> 5 #include <QSqlError> 6 #include <QDebug> 7 8 int main(int argc, char *argv[]) 9 { 10 QCoreApplication app(argc, argv); 11 QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); //database driver 12 db.setHostName("localhost"); //database ip address 13 db.setUserName("username"); //database username 14 db.setPassword("password"); //database password 15 db.setDatabaseName("HC"); //database table name 16 if (true == db.open()) 17 { 18 qDebug() << "succ"; 19 } 20 else 21 { 22 qDebug() << "failed"; 23 exit(0); 24 } 25 return app.exec(); 26 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/9dcd0344afc899e6dab3611fe9e82b65.html