Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Qt to connect to MySQL database in Ubuntu

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to use Qt to connect to MySQL database in Ubuntu. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Environment description:

Ubuntu 10.04.2

QtSDK (the one with the 1.5G installation package)

Mysql5.1

1. Install MySQL

Complete MySQL development under Linux requires installation of the server side, and there is nothing wrong with installing the client side. Search for mysql directly in the software center and select client and server.

Server will be prompted to set a password for root users during installation. Set one.

I am using the mysql5.1 version, and the username and password are stored in a database called mysql and can only be seen at the administrator level.

If you type mysql directly into the terminal, you may prompt ERROR 1045 (28000) because you are accessing the database under your own user name, and there is currently only one root user in the database. It doesn't matter, if necessary, you can add a user to it:

Mysql-uroot-p-- > Log in as root user

Grant usage on *. * to dummy@localhost;-- > authorize a user named dummy to log in locally, and change to your own user name here

However, at this time, direct mysql only has ordinary permissions, and it is not possible to create a database or operate mysql. If you really need to give it administrator permission, you can consult the relevant information yourself.

two。 Install the MySQL driver for Qt.

Method 1: sudo apt-get install libqt4-sql-mysql directly, which is the mysql driver of Qt4, so you don't have to compile it yourself, but you may download something extra.

Copy / usr/lib/qt4/plugins/sqldrivers/libqsqlmysql.so to your QtSDK sqldrivers directory. I installed it directly with normal permissions at that time. The directory is ~ / QtSDK/Desktop/Qt/473/gcc/plugins / sqldrivers.

Method 2: in fact, you can also sudo apt-get download libqt4-sql-mysql, unzip the package, and then search out the so file inside and copy it directly there.

Method 3: obediently compile according to the official method, but it does not seem to be feasible, because the new version of QtSDK does not have a src directory, and there is no mysql driver by default.

3. Try to make a demo.

Remember to add sql to QT + = in the pro file, otherwise qmake will not look for the relevant part of sql:

Double-click the code to select all 12 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 23 24 25 27 28 29 30 31 33 34 35#include # include # include 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 ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report