Qt中SQLite数据库编程
Qt支持轻量级数据库SQLite,接下来测试这个数据库的基本操作
环境:Ubuntu 10.04 + Qt 4.7.0
要支持数据库编程,首先在工程文件中增加:QT += sql
头文件:#include <QtSql>
下面新建一个数据库test,然后新建一个表test_table,在表中新建两个字段:id,name。然后查询表,并显示。
代码:
- 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;
- }
此程序移植到嵌入式linux中一样可以运行。
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20