Oracle初步使用
navicat软件和驱动连接:
链接:https://pan.baidu.com/s/1quC47b0SPzTIs7IvjgWKxQ
提取码:9ab0
一、 navicat连接
- 首先配置一下或者查看一下 工具->选项->环境中OCI环境路径是否正确
- 查看Oracle服务是否打开
- 然后创建Oracle连接
- 测试连接,是否成功,如果成功即可继续
- 如果配到账户被锁定的情况,需要登录到system账户当中,解锁权限
– 使用sqlplus 命令:sqlplus 和sys/密码 as sysdba;
– 其次 alter user scottaccount unlock;修改权限
- 可以对scott进行操作
二、VS+Qt+Oracle - 需要Oracle的驱动,将其放到E:\QT\5.9.1\mingw53_32\plugins\sqldrivers
驱动如果自己编译失败,可以网上去下载别人的,切记版本需要一样
我的Oracle :11
QT:5.9.1
- 代码展示
oracle.h #pragma once #include <QtWidgets/QWidget> #include "ui_oracle.h" #include <QSqlDatabase> #include <QDebug> #include <QSqlError> #include "QMessageBox" #include "QPushButton" #include "QTextEdit" #include "QLabel" #include "QHBoxLayout" #include "QVBoxLayout" #include "QLineEdit" struct m_database { int m_port; QString m_hosetname; QString m_databasename; QString m_username; QString m_password; }; class Oracle : public QWidget { Q_OBJECT public: Oracle(QWidget *parent = Q_NULLPTR); private: Ui::OracleClass ui; void Oracle::createConnection(); void createForm(); m_database m_db; QLineEdit *duankou, *databasename, *username, *mima, *zhuji; QPushButton *connect_db; QTextEdit *show_result; };
oracle.cpp #include "oracle.h" Oracle::Oracle(QWidget *parent) : QWidget(parent) { ui.setupUi(this); createForm(); connect(connect_db, &QPushButton::clicked, this, [&]() { m_db.m_port = duankou->text().toInt(); m_db.m_hosetname = zhuji->text(); m_db.m_databasename = databasename->text(); m_db.m_username = username->text(); m_db.m_password = mima->text(); createConnection(); }); } void Oracle::createForm() { /*QWidget *oracle = new QWidget(this);*/ QVBoxLayout *voracle = new QVBoxLayout(this); QWidget *horacle = new QWidget(this); QHBoxLayout *hhoracle = new QHBoxLayout(horacle); duankou = new QLineEdit(this); zhuji = new QLineEdit(this); databasename = new QLineEdit(this); username = new QLineEdit(this); mima = new QLineEdit(this); mima->setEchoMode(QLineEdit::Password);//输入的时候就显示圆点 show_result = new QTextEdit(this); connect_db = new QPushButton(QString::fromLocal8Bit("连接")); hhoracle->addWidget(new QLabel(QString::fromLocal8Bit("端口"))); hhoracle->addWidget(duankou); hhoracle->addWidget(new QLabel(QString::fromLocal8Bit("主机号"))); hhoracle->addWidget(zhuji); hhoracle->addWidget(new QLabel(QString::fromLocal8Bit("数据库名字"))); hhoracle->addWidget(databasename); hhoracle->addWidget(new QLabel(QString::fromLocal8Bit("用户名"))); hhoracle->addWidget(username); hhoracle->addWidget(new QLabel(QString::fromLocal8Bit("密码"))); hhoracle->addWidget(mima); hhoracle->addWidget(connect_db); voracle->addWidget(horacle); voracle->addWidget(show_result); } void Oracle::createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QOCI");//QOCI db.setPort(m_db.m_port); db.setHostName(m_db.m_hosetname); db.setDatabaseName(m_db.m_databasename); db.setUserName(m_db.m_username); db.setPassword(m_db.m_password); if (!db.open()) { // qDebug("Oracle connect failed."); // // qDebug() << "123" << db.lastError().text(); // // QMessageBox::warning(nullptr, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("无法连接数据库")); show_result->setText("Oracle connect failed"); } else { // QMessageBox::warning(nullptr, "TIP", "OK"); show_result->setText("Oracle connect ok"); } } }
- 效果演示,点击连接按钮,转到槽函数,连接数据库
- 有待改进
并没有对输入的进行判断,有可能输入非法;
界面比较丑陋;