直接贴代码,使用QOCI库,做简单的数据库增删改的操作,没有处理细节,编译QOCI可以参考
http://blog.csdn.net/u014491932/article/details/70992401
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainWindows.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void init();
QTableView *view;
QSqlTableModel *model;
QSqlDatabase db;
QSqlQueryModel query2;//用于执行SQL语句,本身也是model
int id;
private:
QHBoxLayout *HboxLayout1;
QVBoxLayout *VboxLayout1;
QVBoxLayout *VboxLayout2;
QPushButton *buttons1;
QPushButton *buttons2;
QPushButton *buttons3;
QPushButton *buttons4;
QPushButton *buttons5;
QPushButton *buttons6;
QTextEdit *textedit;
QWidget *widget;
private slots:
void ShowSqlType();
void ConnectORACLE();
void AddData();
void DeleteData();
void ChangeData();
void ClearScreen();
};
#endif // MAINWINDOW_H
mainwindows.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
resize(1024,768);
init();
}
MainWindow::~MainWindow()
{
}
void MainWindow::init()
{
widget = new QWidget(this);
widget->setGeometry(QRect(10,10,1000,700));
HboxLayout1 = new QHBoxLayout();
VboxLayout1 = new QVBoxLayout();
VboxLayout2 = new QVBoxLayout();
buttons1 = new QPushButton(widget);
buttons1->setText("显示可连接数据库驱动");
buttons2 = new QPushButton(widget);
buttons2->setText("连接到ORacle");
buttons3 = new QPushButton(widget);
buttons3->setText("清屏");
buttons4 = new QPushButton(widget);
buttons4->setText("添加记录");
buttons5 = new QPushButton(widget);
buttons5->setText("删除记录");
buttons6 = new QPushButton(widget);
buttons6->setText("修改记录");
textedit = new QTextEdit(widget);
view = new QTableView(widget);
VboxLayout1->addWidget(buttons1);
VboxLayout1->addWidget(buttons2);
VboxLayout1->addWidget(buttons3);
VboxLayout1->addWidget(buttons4);
VboxLayout1->addWidget(buttons5);
VboxLayout1->addWidget(buttons6);
VboxLayout2->addWidget(textedit);
VboxLayout2->addWidget(view);
HboxLayout1->addLayout(VboxLayout2);
HboxLayout1->addLayout(VboxLayout1);
widget->setLayout(HboxLayout1);
connect(buttons1,SIGNAL(clicked()),this,SLOT(ShowSqlType()));
connect(buttons2,SIGNAL(clicked()),this,SLOT(ConnectORACLE()));
connect(buttons3,SIGNAL(clicked()),this,SLOT(ClearScreen()));
connect(buttons4,SIGNAL(clicked()),this,SLOT(AddData()));
connect(buttons5,SIGNAL(clicked()),this,SLOT(DeleteData()));
connect(buttons6,SIGNAL(clicked()),this,SLOT(ChangeData()));
}
void MainWindow::ShowSqlType()
{
QStringList drivers = QSqlDatabase::drivers();
textedit->setText("可连接数据库驱动:");
foreach(QString driver,drivers)
{
textedit->append(driver);
}
}
void MainWindow::ChangeData()
{
model->database().transaction(); //开始事务操作
if (model->submitAll()) {
model->database().commit(); //提交
} else {
model->database().rollback(); //回滚
textedit->append("提交失败");
}
}
void MainWindow::DeleteData()
{
int currentRow = view->currentIndex().row();
//获取选中的行
model->removeRow(currentRow);
//删除该行
int ok = QMessageBox::warning(this,"删除当前行!","你确定删除当前行吗?",QMessageBox::Yes,QMessageBox::No);
if(ok == QMessageBox::No)
{
model->revertAll(); //如果不删除,则撤销
}
else
model->submitAll(); //否则提交,在数据库中删除该行
model->select();
}
void MainWindow::AddData()
{
if(db.isOpen())
{
//这里只能用用model来插入,不能执行数据库语句,不然后面的操作都会错乱
int rowNum = model->rowCount(); //获得表的行数
id++;
model->insertRow(rowNum); //添加一行
model->setData(model->index(rowNum,0),id);
model->setData(model->index(rowNum,1),"波塞冬");
model->setData(model->index(rowNum,2),QDateTime::currentDateTime().toString("yyyyMMdd hh:mm:ss"));
model->setData(model->index(rowNum,3),"F.01.01.01");
model->submitAll();
//间接将数据库表装入QTableView
model->setTable("log_request"); //表名
model->setSort(0,Qt::AscendingOrder);//按第0行进行排序DescendingOrder升序 AscendingOrder降序
model->setHeaderData(0, Qt::Horizontal, tr("终端号"));
model->setHeaderData(1, Qt::Horizontal, tr("终端名称"));
model->setHeaderData(2, Qt::Horizontal, tr("请求时间"));
model->setHeaderData(3, Qt::Horizontal, tr("请求功能点"));
model->select();
}
else
{
textedit->append("请先点击连接数据库");
}
}
void MainWindow::ConnectORACLE()
{
if(!db.isOpen())
{
db=QSqlDatabase::addDatabase("QOCI");
db.setHostName("127.0.0.1");
db.setUserName("bjggfw");
db.setPassword("bjggfw");//本地创建的oracle用户名和密码
db.setDatabaseName("orcl");//实例名
db.setPort(1521);//端口号
if(db.open())
{
textedit->append("连接成功");
model = new QSqlTableModel;//间接将数据库表装入QTableView
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
/*************************/
/***OnFieldChange 对于模块的所有改变都将会被立即向数据库提出申请**/
/***OnRowChange 当用户选择一个不同的行时,行的改变将会想数据库提出申请**/
/***OnManualSubmit 数据的所有改变都将会在模块中缓存,直到submitAll() 或者 revertAll() 之一被调用**/
/*************************/
model->setTable("log_request"); //表名
model->setHeaderData(0, Qt::Horizontal, tr("终端号"));
model->setHeaderData(1, Qt::Horizontal, tr("终端名称"));
model->setHeaderData(2, Qt::Horizontal, tr("请求时间"));
model->setHeaderData(3, Qt::Horizontal, tr("请求功能点"));
model->setSort(0,Qt::AscendingOrder);//按第0行进行排序DescendingOrder升序 AscendingOrder降序
model->select();
view->setModel(model);
id = 0;
}
else
{
textedit->append("连接失败");
textedit->append(db.lastError().text());
}
}
else
{
textedit->append("数据库已经连接成功,请勿重复连接!");
}
}
void MainWindow::ClearScreen()
{
textedit->clear();
}
执行结果如下