How to connect to the MySQL database using Qt in ubuntu linux

Source: Internet
Author: User

Environment Description:
Ubuntu 10.04.2
QtSDK (the one for the 1.5g installation package)
Mysql5.1
1. Install MySQL
In Linux, the complete MySQL development requires the installation of the server side, if the installation of the client is nothing bad. Search for mysql directly in the Software Center and select the client and server.
The server prompts you to set a password for the root user during installation.
I am using mysql, and the user name and password are stored in a database named mysql, which can only be seen at the Administrator level.
If you enter mysql directly in the terminal, the ERROR 1045 (28000) may be displayed. This is because you access the database with your own user name, and currently the database has only one root user. It doesn't matter. If necessary, you can add a user:
Mysql-uroot-p --> Log On As A root user
Grant usage on *. * to dummy @ localhost; --> authorize a user named dummy to log on locally. Replace the user name with your own user name.
However, at this time, mysql only has common permissions, and it is not possible to create a database or operate on mysql. If you really need to grant it administrator permissions, you can view the relevant information on your own.
2. Install the MySQL driver of Qt.
Method 1: directly sudo apt-get install libqt4-sql-mysql, which is the mysql driver of Qt4, you do not have to compile it yourself, but may download some additional things.
Copy/usr/lib/qt4/plugins/sqldrivers/libqsqlmysql. so to your QtSDK sqldrivers directory. At that time, I directly installed it with normal permissions. The directory is :~ /QtSDK/Desktop/Qt/473/gcc/plugins/sqldrivers
Method 2: in fact, you can also sudo apt-get download libqt4-sql-mysql, extract the package, and then search out the so file to directly copy it there.
Method 3: Compile according to the official method, but it does not seem feasible because the new version of QtSDK does not have the src directory and does not have the mysql driver by default.
3. Try a demo.
Remember to add SQL in QT + = in the pro file; otherwise, qmake will not find the relevant SQL part.
Copy codeThe Code is as follows:
# Include <QtCore/QCoreApplication>
# Include <QSqlDatabase>
# Include <QDebug>
Int main (int argc, char * argv [])
{
QCoreApplication a (argc, argv );
QSqlDatabase db = QSqlDatabase: addDatabase ("QMYSQL ");
Db. setHostName ("localhost ");
Db. setDatabaseName ("study ");
Db. setUserName ("root ");
Db. setPassword ("tyh ");
If (! Db. open ()){
QDebug () <"Unable to open database ";
} Else {
QDebug () <"Database connection established ";
}
Return a.exe c ();
}

I have already created the study Database. If the connection is successful, the Database connection established will be displayed. I am creating a console program.

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.