Build a connection environment for parsing QT and database

Source: Internet
Author: User

QTAndDatabaseConnection environment setup is the content to be introduced in this article, mainly to understandQTConnectionDatabaseFor more information about how to build the environment, see this article. To DoDatabaseThe experimental requirement is to operate the database on the graphic interface. I am afraid of the complicated things of MFC, so I chose to write with things like QT that don't look complicated.

Problems encountered

1. Various qmake erro

As a rookie I only know make does not know what is qmake guess should be QT make Haha, http://www.kuqin.com/qtdocument/index.html this web site has a qmake link, in details, qmake is very beneficial because many environment variables are not set correctly before QT is installed, so qmake is useless, which causes a lot of trouble for me... Later I found that the environment variable should be set like this.

 
 
  1. User variable name: QMAKESPEC
  2. Value: C: Qt2010.05qtmkspecswin32-msvc2008 (My qt is directly installed on c)

This setting was silly. I was always prompted that I couldn't find it. It turned out that I forgot to write the C: back... I am also disappointed for a moment... Sigh

 
 
  1. User variable name: QTDIR
  2. Value: C: Qt2010.05

Remember to set the path of the mingw installed by qt in the environment variable.

2. For qt include and SQL include, there are also lib settings.

After completing the environment variables, you can use the Internet to find out what is "INCLUDE + = XXXXX" "LIBS + = XXXXX", which is mainly a path problem, each installed MySQL and QT has different include and lib paths. It may take several seconds to find out.

3. After Inlcude and lib are set, go to the pluginssqldriversmysql In the cmd format and click qmake ~ Happy ~~ But I cannot be happy too early.

4. The next problem is to write a program in the main function of qt.

The following is an example:

 
 
  1. #include <QSqlDatabase>   
  2. #include <QSqlQuery>   
  3. #include <QSqlError>   
  4. #include <QApplication>   
  5. #include <QSqlDatabase>   
  6. #include <QStringList>   
  7. #include <QMessageBox>   
  8. #include <QDebug>   
  9. int main(int argc, char* argv[])   
  10. {   
  11.  QApplication app(argc, argv);   
  12.  qDebug() << "Available drivers:";   
  13.  QStringList drivers = QSqlDatabase::drivers();   
  14.  foreach(QString driver, drivers)   
  15.  qDebug() << "/t" << driver;   
  16.  QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");   
  17.  qDebug() << "MYSQL driver valid?" << db.isValid();   
  18.  return 0;   
  19. }  

The compilation result indicates that the QSqlDatabase does not have this path or the solution to the file problem is to find the corresponding file in the folder of the current project. add a QT + = SQL to the end of the pro file ~ Later I found a problemQTIf qdialog is selected first during the project, I still cannot find the qsqldatabase, but I didn't go into the details, so I directly used the qmainwindow to create the project.

Summary: AnalysisQTAndDatabaseThe connection environment has been set up. I hope this article will help you!

Related Article

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.