How QT connects to MySQL

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.