Analysis of Some implementation cases in the QT Database

Source: Internet
Author: User

AboutQT DatabaseSome implementation cases are described in this article ,. I want to know more about it.QTMediumDatabaseIs also the implementation of some small cases. For details, see the details.

1. Solutions for QT4 to read garbled Chinese strings from the database

In the c: \ qt \ 4.6.2 \ src \ SQL \ drivers \ mysql \ qsql_mysql.cpp file

 
 
  1. mysql_set_character_set(d->mysql, "utf8"); 

Change

 
 
  1. mysql_set_character_set(d->mysql, "gbk"); 

Re-compile the driver and solve the problem.

2. How to connect to the Mysql database in Qt

It is not complex to connect to the mysql database in Qt. The code examples in Qt help are as follows:

 
 
  1.  QSqlDatabase m_defDB = QSqlDatabase::addDatabase( "QMYSQL" );  
  2.  m_defDB.setHostName( "localhost" );  
  3.  m_defDB.setDatabaseName( "MyDB" );  
  4.  m_defDB.setUserName( "root" );  
  5.  m_defDB.setPassword( "123456" );  
  6. if ( ! m_defDB.open() )  
  7. {  
  8.    QMessageBox::critical(NULL, "Error Information", m_defDB.lastError().text(), MessageBox::Ok ,QMessageBox::Cancel );  
  9. }  
  10. return true; 

In fact, you cannot connect to mysql by following this code. You only get an error message.

 
 
  1. “Can't create TCP/IP socket(10093 QMYSQL)”, 

This may be a bug in qt. You can try to connect to another database. For example, if "QPSQL" is successful or not, connect to mysql again. You will find that the connection is successful!

As shown in the error message: the TCP/IP socket has an error. Before you connect to the database, initialize it. The Code is as follows: # include "winsock2.h"

 
 
  1. #include "windows.h"  
  2. WSAData wsadata;  
  3. if (WSAStartup(MAKEWORD(2,0), &wsadata) != 0)  
  4. {  
  5.   qFatal("WSA Startup failed");  

When the program is compiled, add the dependency Ws2_32.lib OK to the project. I believe you can easily connect to mysqlDatabaseNow.

Summary: AboutQT DatabaseThe content of some implementation cases has been introduced. I hope this article will help you!

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.