how does qt use MySQL to connect to remote databases? The code is as follows://Output available database Qdebug () << "Available drivers:"; Qstringlist drivers = qsqldatabase::d rivers (); foreach (QString driver, drivers) qdebug () <<driver; Open Mysqlqsqldatabase data_base = qsqldatabase::adddatabase ("Qmysql");d ata_base.sethostname ("10.0.31.41"); Set the host address Data_base.setport (3309); Set Port Data_base.setdatabasename ("gv_local"); Set the database name Data_base.setusername ("root"); Set User name Data_base.setpassword ("root123"); Set Password if (!data_base.open ()) Qdebug () << "failed to connect to MySQL"; else Qdebug () << "Success"; The following information is indicated:
MySQL driver failed to load, how to solve?
Workaround:Copy the Libmysql.dll from the MySQL installation directory to the bin directory in the QT installation directory. Test: QString select_all_sql = "SELECT * from Gv_alarm_info"; Query all data qsqlquery sql_query; Sql_query.prepare (Select_all_sql); if (!sql_query.exec ()) {Qdebug () <<sql_query.lasterror ();} else{while (Sql_query.next ()) {int id = sql_query.value (0). ToInt (); QString templ_name = Sql_query.value (2). toString (); Qdebug () <<qstring ("id:%1 templ name:%2"). Arg (Id). Arg (templ_name); }} The effect is as follows:
How QT connects to MySQL