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
- mysql_set_character_set(d->mysql, "utf8");
Change
- 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:
- QSqlDatabase m_defDB = QSqlDatabase::addDatabase( "QMYSQL" );
- m_defDB.setHostName( "localhost" );
- m_defDB.setDatabaseName( "MyDB" );
- m_defDB.setUserName( "root" );
- m_defDB.setPassword( "123456" );
- if ( ! m_defDB.open() )
- {
- QMessageBox::critical(NULL, "Error Information", m_defDB.lastError().text(), MessageBox::Ok ,QMessageBox::Cancel );
- }
- return true;
In fact, you cannot connect to mysql by following this code. You only get an error message.
- “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"
- #include "windows.h"
- WSAData wsadata;
- if (WSAStartup(MAKEWORD(2,0), &wsadata) != 0)
- {
- 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!