Bootstrap

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

  • 显示结果

;