LinuxQTPlatformMysql databaseDevelopment Environment configuration is the content to be introduced in this article, mainly to understandQT DatabaseIn the Linux development environment, the RedHat is automatically installedMysql databaseAnd QTdesigner, while using it for a whileDatabaseThe connection encountered a lot of difficulties.
Problem:
First, test the database connection: select Projects --> databaseConnections. In the displayed EditDatabaseconnections dialog box, click Newconnection. An editable option is displayed on the right. The configuration is as follows:
- Name:default
Driver: Click the drop-down box and there is no response, so I will fill in QMYSQL3 according to QTAssistant.
My database name uses the built-in mysql first
- DatabaseName: mysql
- Username: root
- Passworld: ********* fill in your own password)
- Hostname: mscl
Then click connect.
- can'tnot connect to the database,Pleaseen sure that the database server is running and that all connection in form ation is correct,
- [Drivernotloaded】
You can use it after extracting it, start the server, and re-connect according to the above settings, but the same problem still exists, so I thought that if QT really supports the MySQL driver, I should see it in the driver drop-down box, so I suspected the problem to the Qt driver.
So I found the installation directory of QT3 in/usr/lib/qt3.3/plugins), but I did not have the src/sqldrivers/mysql directory or the source code of the mysql driver, there was no way to compile the MySql driver, so I went to google to check if there was any source code for the mysql driver in Qt. I searched for a night and didn't see the edges, so I went to bed very late, is there any other way? Zhou gongxun dream )...
Get up at AM. Continue!
Suddenly I had the idea of re-installing a QT, but I had to try it again. I got a QT3.1 version in IT68, 12 to M, and then I started to install it:
1. Select the installation path
- shell>cd/usr/local
2. decompress the downloaded installation package (the following compilation process may be different if the version is different. Refer to the INSTALL file in the source directory)
- shell>tar-jxvfqt-x11-free-3.1.2.tar.bz2
3. Set environment variables (this part is not needed later, because it has been set in later. login)
Add the following lines to my/etc/bashrc:
- QTDIR =/usr/local/qt: Installed root directory
- PATH = $ QTDIR/bin: $ PATH: executable file directory
- MANPATH = $ QTDIR/doc/man: $ MANPATH: man document directory
- LD_LIBRARY_PATH = $ QTDIR/lib: $ LD_LIBRARY_PATH: directory of the Library File
If your installation path is not the same as mine, you must set it accordingly.
4. configure the configure script
I use the default configuration here, so I only enter
- shell>./configure
If you want to change the installation path or select the location of the installed library file and header file, you can use the following options
- ./configure-libdir/usr/local/lib-bindir/usr/local/bin-headerdir/usr/local/include/qt
In this way, the header file directory of your QT library file is under the corresponding directory, but remember that if you set it here, you must launch it in the environment variable in step 3. There is also a -- prefix option that sets the destination directory for qt installation.
5. Compile
- shell>make
This takes a long time. My sub-configuration is cpu x-Dragon 1.8G, and 1G memory is used for about 20 minutes.
An error occurred during the compilation process. I forgot to write it down, which is probably in fbm. in c, noise3 is static, but in fbm. h. add static at the beginning of floatnoise3 (floatvec []) in h to save. Continue typing make.
6. Installation
- shell>makeinstall
- thisprocessalsotakeservalminutues,soyoushouldbepatient.
7. After the installation is complete, I find the bin directory and run the designer. The TMD really works. A QT interface is slowly displayed in front of me.
After QT is installed, it is found that the newly installed QT has several more directories than the ones installed in the original system: config. tests, doc, examples, extensions, pics, qmake, src, tools, tutorial, more than 10 files, of course, the most important thing is that the directory sqldrivers is left empty before plugins is added. Only after compilation is there is a binary file: libqsqlmysql. so), and the src directory.
Now we have the mysql driver source code in qt, and then compile it according to the method described in QtAssistant:
- shell>cdusr/local/qt/plugins/src/sqldrivers/mysql
- shell>qmake-oMakefile"INCLUDEPATH+=/usr/local/mysql/include""LIBS+=-L/usr/local/mysql/lib-lmysqlclient"mysql.pro
The 2nd Command generates the Makefile file and adds the directory of the header file and library file to the options LIBS and INCLUDEPATH. Here, my mysql is installed under/usr/local/mysql and compiled, or in the original directory
- shell>make
After some drivers are compiled, I will go to QTdesigner to see if I can identify the driver.
1. Start designer
- [root@msclbin]#/usr/local/qt/bin/designer
2. Follow the previous settings to set databaseconnections.
Name: connect_test (I named this connection)
Driver: Click the drop-down box. The difference is that I can see QMYSQL3 ^_^)
Database Name or use mysql
- DatabaseName: mysql
- Username: root
- Passworld: ********* fill in your own password)
- Hostname: mscl
Then, click the connect button to view the connection name connect_test in the list on the left. It took more than two days to connect the connection.
The following code is written to test the database. The code is basically copied from QtAssistant:
1. Some code
- voidForm1::init()
- {
- createConnection();
- }
-
-
- boolForm1::createConnection()
- {
- //createthedefaultdatabaseconnection
- QSqlDatabase*defaultDB=QSqlDatabase::addDatabase("QMYSQL3");
- if(!defaultDB){
- qWarning("Failedtoconnecttodriver");
- returnFALSE;
- }
- defaultDB->setDatabaseName("mysql");
- defaultDB->setHostName("mscl");
- defaultDB->setUserName("root");
- defaultDB->setPassword("39323286");
- //defaultDB->setHostName("bookhost");
- if(!defaultDB->open()){
- qWarning("Failedtoopendatabase:"+
- defaultDB->lastError().driverText());
- qWarning(defaultDB->lastError().databaseText());
- returnFALSE;
- }
- returnTRUE;
- }
2. Compile
- [root@msclmscl]#qmake-oMakefiledbcon.pro(myprojectname)
- [root@msclmscl]#make
3. Run
- [root@msclmscl]#./dbcon
The error message "can 'tloadlibrary' libqt" cannot be found. so.3 ', which is recorded in the installation instructions of the QT source file. Under the installation directory lib of Qt, This is a symbolic link, I still want to copy it to/usr/lib to solve the problem, but it is useless to copy the connection. I may not find the real library in the lib directory ).
Solution: create a file named (. login) in the/root/directory, and add the following variables to the file:
- setenvQTDIR/usr/local/qt
- setenvPATH$QTDIR/bin:$PATH
- setenvMANPATH$QTDIR/doc/man:$MANPATH
- setenvLD_LIBRARY_PATH$QTDIR/lib:$LD_LIBRARY_PATH
After adding the instance, you must restart the computer. After the restart, I went to the/root directory and checked that the original. login file name had changed to another file qt. log ~, It is also hidden, but the content is the same.
Finally, I re-run the executable file without the above problems. But there are new problems.
- [root@msclmscl]#./dbcon
- ./dbcon:errorwhileloadingsharedlibraries:/usr/lib/libmysqlclient.so.15:cannotrestoresegmentprotafterreloc:Permissiondenied
It is explained that the library file libmysqlclient cannot be found during the running process. so.15, I found the file under/usr/local/mysql/lib. Since it is not a link, I copied it directly to the/usr/lib directory, then there is no problem, but now there is a new problem. So I thought whether the access permission was not enough. Right-click it and grant it the read/write permission, but it is still an old problem.
At this point, I noticed a star in the upper right corner of my computer. Haha, based on similar experiences, I know that there must be a solution in clicking it. As expected, SELinux originally prevented the program from loading libmysqlclient. so.15, because code relocation is required. The AllowAccess tag contains the following:
- Thefollowingcommandwillallowthisaccess:
- chcon-ttextrel_shlib_t/usr/lib/libmysqlclient.so.15
Do as follows:
- [root@msclmscl]#chcon-ttextrel_shlib_t/usr/lib/libmysqlclient.so.15
- [root@msclmscl]#./dbcon
Finally, the test window pops up normally, so far the database of the previous QT has basically done a good job.
Some issues to be solved: How can I find libmysqlclient directly under/usr/local/mysql/lib. so.15, so that I don't have to copy it to the/usr/lib directory, just libqt. same as so.3? Come on.
On the 12th and 6th, the above problem is solved. solution:
In the/etc/ld. so. conf file, we can see that its content is:
- includeld.so.conf.d/*.conf
Therefore, it will be based on/etc/ld. so. conf. d directory to find the dynamic library suffix. so), so we can create a conf file here to tell the system the path of our mysql dynamic link library, the content is:
- /usr/local/mysql/lib
Save and run the ldconfig command. Therefore, the system requires that the dynamic library be a link symbol, and libsqlclient under my/usr/local/mysql/lib. so.15 is not a link. SELinux stops the application from loading the dynamic library. To solve this problem, run the following command:
- chcon-ttextrel_shlib_t/usr/local/mysql-5.0.45-linux-i686/lib/libmysqlclient.so.15
Finally, OK. I almost forgot about it. After the restart, the Qt of Qt3.3.6 I installed was replaced by the newly compiled Qt3.1 under the Application menu ).
Summary: details about LinuxQTPlatformMysql databaseThe Development Environment configuration is complete. I hope this article will help you!