Connect to the MySQL database using Qt in Ubuntu 10.10

Source: Internet
Author: User

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

 
 
  1. 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

 
 
  1. www.bkjia.com@bkjia:~$sudo apt-get install mysql-server mysql-client  
  2. www.bkjia.com@bkjia:~$sudo apt-get install libmysqlclient15-dev   
  3. www.bkjia.com@bkjia:~$sudo apt-get install libqt4-sql-mysql  
  4. www.bkjia.com@bkjia:~$sudo /etc/init.d/mysql restart 

Step 3: authorize normal users to operate databases

 
 
  1. Www.bkjia.com @ bkjia :~ $ Mysql-u root-p (root login database)
  2. Mysql>Create database mydb; (create a database)
  3. Mysql>Grant all privileges on *. * to username @ localhost identified by 'Password ';
  4. 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

 
 
  1. # Include<QApplication> 
  2. # Include<QtSql> 
  3. # Include<QTableView> 
  4. # Include<Iostream> 
  5.  
  6. Using namespace std;
  7.  
  8. Int main (int argc, char * argv [])
  9. {
  10. QApplication app (argc, argv );
  11.  
  12. QSqlDatabaseDb=QSqlDatabase: AddDatabase ("QMYSQL ");
  13. Db. setHostName ("localhost ");
  14. Db. setDatabaseName ("mydb ");
  15. Db. setUserName ("username ");
  16. Db. setPassword ("password ");
  17. Db. open ();
  18.  
  19. QSqlQuery query;
  20. Query.exe c ("create table hello (id bigint not null auto_increment, name varchar (255), age bigint, primary key (id ))");
  21. /* Query.exe c ("insert into hello (name, age) values ('xiaoxi', 18 )");
  22. Query.exe c ("insert into hello (name, age) values ('xiaonance', 19 )");
  23. Query.exe c ("insert into hello (name, age) values ('xiaobei', 20 )");
  24. Query.exe c ("insert into hello (name, age) values ('xiaodong', 21 )");*/
  25.  
  26. // ODBC Style
  27. Query. prepare ("insert into hello (name, age)" "values (?, ?) ");
  28. Query. addBindValue ("xiaoxi ");
  29. Query. addBindValue (18 );
  30. Query.exe c ();
  31. Query. addBindValue ("xiaonan ");
  32. Query. addBindValue (19 );
  33. Query.exe c ();
  34. Query. addBindValue ("xiaobei ");
  35. Query. addBindValue (20 );
  36. Query.exe c ();
  37. Query. addBindValue ("xiaodong ");
  38. Query. addBindValue (21 );
  39. Query.exe c ();
  40.  
  41. // Oracle Style
  42. /* Query. prepare ("insert into hello (name, age) values (: name,: age )");
  43. Query. bindValue (": name", "xiaoxi ");
  44. Query. bindValue (": age", 18 );
  45. Query.exe c ();
  46. Query. bindValue (": name", "xiaonan ");
  47. Query. bindValue (": age", 19 );
  48. Query.exe c ();
  49. Query. bindValue (": name", "xiaobei ");
  50. Query. bindValue (": age", 20 );
  51. Query.exe c ();
  52. Query. bindValue (": name", "xiaodong ");
  53. Query. bindValue (": age", 21 );
  54. Query.exe c ();*/
  55.  
  56. QSqlQueryModel *Model=NewQSqlQueryModel;
  57. Model->SetQuery ("select * from hello ");
  58. Model->SetHeaderData (0, Qt: Horizontal, "id ");
  59. Model->SetHeaderData (1, Qt: Horizontal, "name ");
  60. Model->SetHeaderData (2, Qt: Horizontal, "age ");
  61. QTableView *View=NewQTableView;
  62. View->SetWindowTitle ("QSqlQueryModel ");
  63. View->SetModel (model );
  64. View->Show ();
  65.  
  66. /* QSqlTableModel *Model=NewQSqlTableModel;
  67. Model->SetTable ("hello ");
  68. Model->Select ();
  69. For (intI=0; I< Model->RowCount (); ++ I)
  70. {
  71. QSqlRecordRecord=Model->Record (I );
  72. IntId=Record. Value (0). toInt ();
  73. QStringName=Record. Value (1). toString ();
  74. IntAge=Record. Value (2). toInt ();
  75. Cout<< Id <<""<< QPrintable(Name)<<""<< Age << Endl;
  76. }*/
  77. Db. close ();
  78. Return app.exe c ();
  79. }

Compile:

 
 
  1. www.bkjia.com@bkjia:~$ qmake -project  
  2. www.bkjia.com@bkjia:~$ qmake 

Do not forget to add the following line to the. pro file of the project:

 
 
  1. QT += sql  
  2. www.bkjia.com@bkjia:~$make  
  3. 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!

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.