QT SqLite3

前端之家收集整理的这篇文章主要介绍了QT SqLite3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  • 介绍:sqlITE 是一个轻型的数据库系统,他不需要安装和配置,保存在本地,后缀名以db结尾,支持sql语言,可以用sqlite Export 工具来查看数据库文件


  • 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();
    }

  • 显示结果

猜你在找的Sqlite相关文章