The method of using QT to connect Mysql database under Ubuntu Linux _mysql

Source: Internet
Author: User
Environment Description:
Ubuntu 10.04.2
QTSDK (the one in the 1.5G installation package)
mysql5.1
1. Install MySQL
Linux under the full MySQL development needs to install the server side, if the installation of the client is nothing bad. Search MySQL directly in the Software center and select the client and server.
Server will be prompted to set the password for root when installed, set a good.
I am using the mysql5.1 version, the username password is stored in a database called MySQL, only Administrator level to see.
If you enter MySQL directly in the terminal, you may be prompted for error 1045 (28000) because you are accessing the database in your username and there is only one root user in the database. Okay, if necessary, you can add a user in:
Mysql-uroot-p--> log in as root user
Grant usage on *.* to dummy@localhost; --> authorize a user who is named dummy to log in locally, and here you can replace it with your own username.
But this time direct MySQL only ordinary permissions, create a database, operation MySQL is not possible, if you really have to give it to the administrator, you can access the relevant information.
2. Install QT-driven MySQL.
Method one: Direct sudo apt-get install libqt4-sql-mysql, this is the QT4 MySQL driver, you do not have to bother to compile, but may download some additional things.
Copy/usr/lib/qt4/plugins/sqldrivers/libqsqlmysql.so to your qtsdk sqldrivers directory, I was directly installed with normal permissions, the directory is: ~/qtsdk/desktop/qt/ 473/gcc/plugins/sqldrivers
Method Two: In fact, you can also sudo apt-get download libqt4-sql-mysql, the package decompression, and then the inside of so file search directly copy to there.
Method Three: obediently compiled according to the official method, but seemingly not feasible, because the new version of QTSDK and no src directory, the default is not the driver of MySQL.
3. Try a Demo
Remember to add SQL to QT + in pro files, otherwise qmake is not going to find the relevant parts of SQL
Copy Code code 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.exec ();
}

I have already created this database before study it, and if the connection is successful, database connection established will be displayed. I was building the console program ha.
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.