QTMediumSQLite DatabaseProgramming is the content to be introduced in this article, mainly to learnQTMediumDatabaseFor more information, see the detailed description.QTLightweightDatabase SQLite, Then testDatabaseBasic operations
Environment: Ubuntu10.04 + Qt4.7.0
To support database programming, add the following in the project file:
- QT + = SQL
-
- Header file: # include <QtSql>
Create a database test, create a table test_table, and create two fields in the Table: id and name. Query the table and display it.
Code:
- QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
- //db.setHostName("fedora12");
- db.setDatabaseName("test");
- //db.setUserName("jdh");
- //db.setPassword("123456");
- if (!db.open())
- {
- cout << "shu ju ku dabukai!!!!!!!" << endl;
- }
-
- cout << "shu ju ku cao zuo---------------!!!!!!!" << endl;
- QSqlQuery q;
- if (q.exec("CREATE TABLE test_table (id INT PRIMARY KEY,name VARCHAR)") == false)
- {
- cout << "shu ju ku dabukai22222222!!!!!!!" << endl;
- }
- q.exec("insert into test_table values (8,'LiLei')");
- q.exec("insert into test_table values (46,'HanMeiMei')");
- q.exec("select * from test_table");
- while (q.next())
- {
- cout << "caozuo!!" << endl;
- int id = q.value(0).toInt();
- QString name = q.value(1).toString();
- qDebug() << id << name << endl;
- }
- QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
- //db.setHostName("fedora12");
- db.setDatabaseName("test");
- //db.setUserName("jdh");
- //db.setPassword("123456");
- if (!db.open())
- {
- cout << "shu ju ku dabukai!!!!!!!" << endl;
- }
-
- cout << "shu ju ku cao zuo---------------!!!!!!!" << endl;
- QSqlQuery q;
- if (q.exec("CREATE TABLE test_table (id INT PRIMARY KEY,name VARCHAR)") == false)
- {
- cout << "shu ju ku dabukai22222222!!!!!!!" << endl;
- }
- q.exec("insert into test_table values (8,'LiLei')");
- q.exec("insert into test_table values (46,'HanMeiMei')");
- q.exec("select * from test_table");
- while (q.next())
- {
- cout << "caozuo!!" << endl;
- int id = q.value(0).toInt();
- QString name = q.value(1).toString();
- qDebug() << id << name << endl;
- }
NOTE: IfDatabaseThe name is memory:. Only tables are created in the memory.
This program can be run just as it is transplanted to embedded linux.
Summary: AboutQTMediumSQLite DatabaseI hope this article will help you with programming!