- QT中使用:
void MainWindow::on_pushButton_clicked() { QStringList strlist = QsqlDatabase::drivers(); for(int i = 0; i < strlist.size();i++) { qDebug()<<strlist.at(i); } QsqlDatabase db = QsqlDatabase::addDatabase("QsqlITE"); db.setDatabaseName("mytest.db"); //在工程目录新建一个mytest.db的文件 if(!db.open()) { qDebug()<<"open fail!"; return ; } QsqlQuery query(db); bool bsuccess = query.exec("create table person (id int primary key," "firstname varchar(20),lastname varchar(20))"); //创建一个表 query.exec("insert into person values(101,'Danny','Young')"); query.exec("insert into person values(102,'Christine','Holand')"); query.exec("insert into person values(103,'Lars','Gordon')"); query.exec("insert into person values(104,'Roberto','Robitaille')"); query.exec("insert into person values(105,'Maria','Papadopoulos')"); if(!bsuccess) { qDebug()<< "table is error"; } else { qDebug()<<"table is ok"; } QsqlTableModel *model = new QsqlTableModel; model->setTable("person"); model->setEditStrategy(QsqlTableModel::OnManualSubmit); model->select(); ui->tableView->setModel(model); ui->tableView->show(); }
- 显示结果