//添加sql模块
//打开数据库
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
DB=QSqlDatabase::addDatabase("QSQLITE");
QString dbpath = QCoreApplication::applicationDirPath() + "/wsddata.db";
DB.setDatabaseName(dbpath);
if (!DB.open())
{
QMessageBox::critical(this, "信息",
"open data error\n",
QMessageBox::Ok,QMessageBox::NoButton);
}
}
//插入数据
void MainWindow::on_insert_clicked()
{
QSqlQuery query;
query.prepare("INSERT INTO wsd VALUES('1','2','3','4')");
if (!query.exec())
{
QMessageBox::critical(this, "信息",
"insert error\n",
QMessageBox::Ok,QMessageBox::NoButton);
}
}
//查询数据
void MainWindow::on_select_clicked()
{
QString cmd;
QSqlQueryModel *model;
model=new QSqlQueryModel(this);
cmd+="select * from wsd";
model->setQuery(cmd);
ui->tableView->setModel(model);
}
//更新数据
void MainWindow::on_updata_clicked()
{
QSqlQuery query;
query.prepare("update wsd set 存储时='5' where 存储时='4'");
if (!query.exec())
{
QMessageBox::critical(this, "信息",
"error\n",
QMessageBox::Ok,QMessageBox::NoButton);
}
}
//删除数据
void MainWindow::on_delete_2_clicked()
{
QSqlQuery query;
query.prepare("delete from wsd");
if (!query.exec())
{
QMessageBox::critical(this, "信息",
"error\n",
QMessageBox::Ok,QMessageBox::NoButton);
}
}