In Ubuntu 10.10, Qt is used to connect to the Mysql database. The detailed steps are described first.
Step 1: Install the qt Development Environment
- www.bkjia.com@bkjia:~$sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demos qt4-designer
Step 2: Install the mysql Development Environment
- www.bkjia.com@bkjia:~$sudo apt-get install mysql-server mysql-client
- www.bkjia.com@bkjia:~$sudo apt-get install libmysqlclient15-dev
- www.bkjia.com@bkjia:~$sudo apt-get install libqt4-sql-mysql
- www.bkjia.com@bkjia:~$sudo /etc/init.d/mysql restart
Step 3: authorize normal users to operate databases
- Www.bkjia.com @ bkjia :~ $ Mysql-u root-p (root login database)
- Mysql>Create database mydb; (create a database)
- Mysql>Grant all privileges on *. * to username @ localhost identified by 'Password ';
- Mysql>Quit;
The format for assigning data operation permissions to a specific user is:
Grant permission 1, permission 2,... Permission n on database name. Table name to user name @ user IP address identified by 'Password ';
Step 4: test the environment
- # Include<QApplication>
- # Include<QtSql>
- # Include<QTableView>
- # Include<Iostream>
-
- Using namespace std;
-
- Int main (int argc, char * argv [])
- {
- QApplication app (argc, argv );
-
- QSqlDatabaseDb=QSqlDatabase: AddDatabase ("QMYSQL ");
- Db. setHostName ("localhost ");
- Db. setDatabaseName ("mydb ");
- Db. setUserName ("username ");
- Db. setPassword ("password ");
- Db. open ();
-
- QSqlQuery query;
- Query.exe c ("create table hello (id bigint not null auto_increment, name varchar (255), age bigint, primary key (id ))");
- /* Query.exe c ("insert into hello (name, age) values ('xiaoxi', 18 )");
- Query.exe c ("insert into hello (name, age) values ('xiaonance', 19 )");
- Query.exe c ("insert into hello (name, age) values ('xiaobei', 20 )");
- Query.exe c ("insert into hello (name, age) values ('xiaodong', 21 )");*/
-
- // ODBC Style
- Query. prepare ("insert into hello (name, age)" "values (?, ?) ");
- Query. addBindValue ("xiaoxi ");
- Query. addBindValue (18 );
- Query.exe c ();
- Query. addBindValue ("xiaonan ");
- Query. addBindValue (19 );
- Query.exe c ();
- Query. addBindValue ("xiaobei ");
- Query. addBindValue (20 );
- Query.exe c ();
- Query. addBindValue ("xiaodong ");
- Query. addBindValue (21 );
- Query.exe c ();
-
- // Oracle Style
- /* Query. prepare ("insert into hello (name, age) values (: name,: age )");
- Query. bindValue (": name", "xiaoxi ");
- Query. bindValue (": age", 18 );
- Query.exe c ();
- Query. bindValue (": name", "xiaonan ");
- Query. bindValue (": age", 19 );
- Query.exe c ();
- Query. bindValue (": name", "xiaobei ");
- Query. bindValue (": age", 20 );
- Query.exe c ();
- Query. bindValue (": name", "xiaodong ");
- Query. bindValue (": age", 21 );
- Query.exe c ();*/
-
- QSqlQueryModel *Model=NewQSqlQueryModel;
- Model->SetQuery ("select * from hello ");
- Model->SetHeaderData (0, Qt: Horizontal, "id ");
- Model->SetHeaderData (1, Qt: Horizontal, "name ");
- Model->SetHeaderData (2, Qt: Horizontal, "age ");
- QTableView *View=NewQTableView;
- View->SetWindowTitle ("QSqlQueryModel ");
- View->SetModel (model );
- View->Show ();
-
- /* QSqlTableModel *Model=NewQSqlTableModel;
- Model->SetTable ("hello ");
- Model->Select ();
- For (intI=0; I< Model->RowCount (); ++ I)
- {
- QSqlRecordRecord=Model->Record (I );
- IntId=Record. Value (0). toInt ();
- QStringName=Record. Value (1). toString ();
- IntAge=Record. Value (2). toInt ();
- Cout<< Id <<""<< QPrintable(Name)<<""<< Age << Endl;
- }*/
- Db. close ();
- Return app.exe c ();
- }
Compile:
- www.bkjia.com@bkjia:~$ qmake -project
- www.bkjia.com@bkjia:~$ qmake
Do not forget to add the following line to the. pro file of the project:
- QT += sql
- www.bkjia.com@bkjia:~$make
- www.bkjia.com@bkjia:~$./mysql
Summary: I have finished the introduction of using Qt to connect to the Mysql database in Ubuntu 10.10. I hope this article will help you!