Ubuntu10.10 integrates MySQL and QT

Source: Internet
Author: User

Install MySQL

Sudo apt-Get install mysql-Server

This should be very simple, and I don't think there is much problem with the installation, so I will not talk about it much. Let's talk about the configuration below.

Configure MySQL

Note: In ubuntu, MySQL only allows local access by default. If you want access from other machines, you need to change the/etc/MySQL/My. CNF configuration file! Next we will step by step:

After the default MySQL installation, the root user does not have a password. Therefore, use the root user to enter:

$ Mysql-u Root

-U root is used here because I am a general user (firehare). If-u is not added
If it is root, MySQL will assume that firehare is logged on. Note: I have not entered the root user mode here because it is unnecessary. Generally, you can operate databases in MySQL,
There is no need to enter the root user mode. This is only possible when it is set.

After entering MySQL, the most important thing is to set the root user password in MySQL. Otherwise, the MySQL service is no longer secure.

Mysql> grant all privileges on *. * to root @ localhost identified by "1 ";

Log on again, terminal: mysql-u root-P

Enter the password, enter 1, and press Enter;

Create Database enroll; // create a database

Use enroll; // use the database

Create Table 'student '(// create a table
'Sno' char (9) Not null,
'Sname' char (20) default null,
'Ssex 'Char (1) default null,
'Age' int (11) default null,
'Scome' char (20) default null,
'Classno' char (5) not null,
Unique key 'sno' ('sno ')
) Engine = InnoDB default charset = Latin1

Insert several groups of data.

Create a project in QT

# Include <qtsql> # include <qmessagebox> # include <qtextstream> qsqldatabase DB = qsqldatabase: adddatabase ("qmysql ");
DB. sethostname ("localhost ");
DB. setdatabasename ("enroll ");
DB. setusername ("root ");
DB. setpassword ("1 ");
If (! DB. open ())
{
Qmessagebox: Critical (0, qapp-> tr ("cannot open database "),
Qapp-> tr ("unable to establish a database connection ."
), Qmessagebox: Cancel );
Return false;

}
// Test the connection
Qsqlquery query;

Query.exe C ("select classno from Class ");
Qdebug () <"query. Size ():" <query. Size ();
While (query. Next ())
{
Qdebug () <"query. Value (0)" <query. Value (0). tostring ();
Classes. append (query. Value (0). tostring ());

}
Qdebug () <"classes. Size ()" <classes. Size ();
Query.exe C ("select a from student ");
Qdebug () <query. Size ();
While (query. Next ())
{
Qstring id = query. Value (0). tostring ();
Qstring type = query. Value (1). tostring ();
Qstring DATA = query. Value (2). tostring ();
Qdebug () <id <"," <type <"," <data <Endl;
Qdebug () <query. Size ();
}
For a brief description, qt4.7 directly carries the MySQL driver, just declare it as above. You must also write QT + = SQL in. Pro.

 

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.