QT使用数据库和proC数据库

一,QT使用数据库

        数据库就是保存数据的文件。可以存储大量数据,包括插入数据、更新数据、截取数据等。用专业术语来说,数据库是“按照数据结构来组织、存储和管理数据的仓库”。
        什么时候需要数据库?在嵌入式里,存储大量数据,或者记录数据,就需要用到数据库。比如手机的闹钟就使用到了数据库,我们设置的闹钟数据将会保存到数据库里,闹钟程序运行时会从数据库里读取出上次保存的闹钟数据。如果没有数据库,则闹钟程序关机了数据不保存在物理储存设备里,下次运行闹钟时就没有上次设置的闹钟数据,这显然是不合理的。所以我们需要用到数据库。想要在项目中使用Qt SQL模块,需要在项目配置文件里添加QT += core gui sql

        Qt SQL模块为数据库提供了编程支持,Qt支持很多种常见的数据库,如MySQL、Oracle、MS SQL Server、SQLite等。Qt SQL模块里包含了很多个类,可以轻松实现数据库的连接、执行SQL语句,获取数据库里的数据与界面显示等功能,一般数据与界面之间会采用Model/View架构,很方便的显示数据界面和操作数据库。在嵌入式里,一般常用的数据库就是Sqlite3。SQLite是非常小轻量级的,完全配置时小于400KB,省略可选功能配置时小于250KB。SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的SQL数据库引擎。它是一个零配置的数据库,这意味着与其他数据库不一样,您不需要在系统中配置。就像其他数据库SQLite引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite可以直接访问其存储文件。

.pro文件

QT       += core gui sqlgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mainwindow.cpp \numberpicker.cpp \switchbutton.cppHEADERS += \mainwindow.h \numberpicker.h \switchbutton.h# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetRESOURCES += \res.qrc

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
int main(int argc, char *argv[])
{QApplication a(argc, argv);/* 指定文件 */QFile file(":/style.qss");/* 判断文件是否存在 */if (file.exists() ) {/* 以只读的方式打开 */file.open(QFile::ReadOnly);/* 以字符串的方式保存读出的结果 */QString styleSheet = QLatin1String(file.readAll());/* 设置全局样式 */qApp->setStyleSheet(styleSheet);/* 关闭文件 */file.close();}MainWindow w;w.show();return a.exec();
}

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMainWindow>
#include <QDialog>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QListWidget>
#include <QLabel>
#include <QTime>
#include <QSqlTableModel>
#include "numberpicker.h"
#include "switchbutton.h"class NumberPicker;
class SwitchButton;/* ListWiget项结构体 */
struct ItemObjectInfo {/* 闹钟开关 */SwitchButton *switchButton;/* Widget容器 */QWidget *widget;/* 水平布局 */QHBoxLayout *hBoxLayout;
};class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:/* 数据库连接类 */QSqlDatabase sqlDatabase;/* 数据库操作模型 */QSqlTableModel *model;/* 时针选择器 */NumberPicker *hourPicker;/* 分钟选择器 */NumberPicker *minutePicker;/* 弹出选择时间对话框 */QDialog *alarmDialog;/* 水平布局 */QHBoxLayout *hBoxLayout[3];/* 垂直布局 */QVBoxLayout *vBoxLayout[2];/* 显示闹钟列表 */QListWidget *listWidget;/* 主Widget */QWidget *mainWidget;/* 底部Wiget */QWidget *bottomWidget;/* 弹出对话框布局窗口选择时间容器 */QWidget *timeWidget;/* 弹出对话框布局窗口按钮容器 */QWidget *btWidget;/* 添加闹钟按钮 */QPushButton *addAlarm;/* 确认按钮 */QPushButton *yesButton;/* 取消按钮 */QPushButton *cancelButton;/* listWiget项信息存储 */QVector<ItemObjectInfo> itemObjectInfo;private slots:/* 添加闹钟按钮被点击 */void addAlarmClicked();/* 列表被点击 */void listWidgetItemClicked(QListWidgetItem *);/* 确认按钮被点击 */void yesButtonClicked();/* 取消按钮被点击 */void cancelButtonClicked();/* 开关按钮点击 */void switchButtonClicked(bool);
};
#endif // MAINWINDOW_H

 MainWindow.cpp

#include "mainwindow.h"
#include <QDebug>
#include <QSqlError>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{/* 设置主窗体的显示位置与大小 */this->setGeometry(0, 0, 800, 480);/* 查看本机可用的数据库驱动 */QStringList drivers = QSqlDatabase::drivers();foreach(QString driver, drivers) {qDebug()<<driver;}/* 以QSQLITE驱动方式打开或者创建数据库 */sqlDatabase = QSqlDatabase::addDatabase("QSQLITE");sqlDatabase.setDatabaseName("alarm.db");/* 以open的方式打开alarm.db数据库,则会创建一个alarm.db */if (!sqlDatabase.open())qDebug()<<"连接数据库错误"<<sqlDatabase.lastError()<<endl;elseqDebug()<<"连接数据库成功"<<endl;QSqlQuery query(sqlDatabase);/* 使用指令式创建表 */query.exec("create table alarm (id int primary key, time vchar(15), flag vchar(5))");/* 以指令的方式插入数据 *///query.exec("insert into alarm values(0, '06:00', 'false')");model = new QSqlTableModel(this, sqlDatabase);/* 模型设置表的名字,需要与数据库的表的名字相同 */model->setTable("alarm");/* 如果有修改则同步修改到数据库,* 注意这个规则需要与tabview这样的控件才生效,* 因为tabview可以直接编辑表里的内容 */model->setEditStrategy(QSqlTableModel::OnFieldChange);/* 成功则返回true,查看数据库里是否有alarm这个表格 */model->select();/* 如果数据表数据为空,则添加两个闹钟 */if (model->rowCount() == 0) {/* 插入一行 */model->insertRow(model->rowCount());/* 在该行插入数据 */model->setData(model->index(0, 0), 1);model->setData(model->index(0, 1), "06:00");model->setData(model->index(0, 2), "false");/* 插入数据后记得提交 */model->submit();/* 再插入一行 */model->insertRow(model->rowCount());model->setData(model->index(1, 0), 2);model->setData(model->index(1, 1), "18:00");model->setData(model->index(1, 2), "true");/* 提交 */model->submit();}hourPicker = new NumberPicker(this);hourPicker->setRange(0, 24);minutePicker = new NumberPicker(this);minutePicker->setRange(0, 60);/* 标签,用于显示时&分 */QLabel *label[3];label[0] = new QLabel();label[1] = new QLabel();label[2] = new QLabel();QFont font;font.setBold(true);font.setPixelSize(10);QPalette pal;pal.setBrush(QPalette::WindowText, QColor(0, 0, 0));label[0]->setFont(font);label[1]->setFont(font);label[2]->setFont(font);label[0]->setText(" ");label[1]->setText("时");label[2]->setText("分");/* 主布局初始化 */listWidget = new QListWidget();mainWidget = new QWidget();bottomWidget = new QWidget();alarmDialog = new QDialog(this);timeWidget = new QWidget();btWidget = new QWidget();addAlarm = new QPushButton();yesButton = new QPushButton();cancelButton = new QPushButton();vBoxLayout[0] = new QVBoxLayout();vBoxLayout[1] = new QVBoxLayout();hBoxLayout[0] = new QHBoxLayout();hBoxLayout[1] = new QHBoxLayout();hBoxLayout[2] = new QHBoxLayout();addAlarm->setMaximumSize(84, 84);addAlarm->setObjectName("addAlarm");addAlarm->setMinimumSize(84, 84);bottomWidget->setMinimumHeight(84);bottomWidget->setMaximumHeight(84);yesButton->setText("确认");cancelButton->setText("取消");yesButton->setMaximumSize(100, 50);yesButton->setMinimumSize(100, 50);cancelButton->setMinimumSize(100, 50);cancelButton->setMaximumSize(100, 50);btWidget->setMaximumHeight(70);btWidget->setMinimumHeight(70);alarmDialog->setMinimumSize(300, 300);alarmDialog->setMaximumSize(300, 300);alarmDialog->setModal(true);yesButton->setObjectName("yesButton");cancelButton->setObjectName("cancelButton");/* 主布局 */vBoxLayout[0]->addWidget(listWidget);vBoxLayout[0]->addWidget(bottomWidget);vBoxLayout[0]->setContentsMargins(0, 0, 0, 0);mainWidget->setLayout(vBoxLayout[0]);setCentralWidget(mainWidget);/* 底部按钮布局 */hBoxLayout[0]->addWidget(addAlarm);hBoxLayout[0]->setContentsMargins(0, 0, 0, 0);bottomWidget->setLayout(hBoxLayout[0]);/* 对话框布局 */vBoxLayout[1]->addWidget(timeWidget);vBoxLayout[1]->addWidget(btWidget);vBoxLayout[1]->setContentsMargins(0, 0, 0, 0);alarmDialog->setLayout(vBoxLayout[1]);hBoxLayout[1]->addWidget(label[0]);hBoxLayout[1]->addWidget(hourPicker);hBoxLayout[1]->addWidget(label[1]);hBoxLayout[1]->addWidget(minutePicker);hBoxLayout[1]->addWidget(label[2]);hBoxLayout[1]->setContentsMargins(0, 0, 0, 0);timeWidget->setLayout(hBoxLayout[1]);hBoxLayout[2]->addWidget(yesButton);hBoxLayout[2]->addWidget(cancelButton);btWidget->setLayout(hBoxLayout[2]);/* 打印出闹钟数据库里的信息 */for (int i = 0; i < model->rowCount(); i++) {for (int j = 0; j < 3; j++) {QModelIndex qindex = model->index(i, j);switch (j) {case 0:qDebug()<<"第"<<model->data(qindex).toInt()<<"行数据";break;case 1:listWidget->addItem(model->data(qindex).toString());qDebug()<<"闹钟时间为:"<<model->data(qindex).toString();break;case 2:qDebug()<<"闹钟状态为:"<<model->data(qindex).toString()<<endl;if (model->data(qindex).toString() != "true")listWidget->item(i)->setTextColor(QColor(22, 22, 22, 60));elselistWidget->item(i)->setTextColor(QColor(22, 22, 22, 225));break;default:break;}}}/* 在列表里添加闹钟开关 */for (int i = 0; i < model->rowCount(); i++) {ItemObjectInfo info;info.widget = new QWidget();info.switchButton = new SwitchButton();info.hBoxLayout = new QHBoxLayout();info.switchButton->setMaximumSize(55, 30);info.switchButton->setMinimumSize(55, 30);info.hBoxLayout->setContentsMargins(0, 0, 0, 0);info.hBoxLayout->setAlignment(Qt::AlignRight);info.hBoxLayout->addWidget(info.switchButton);info.widget->setLayout(info.hBoxLayout);listWidget->setItemWidget(listWidget->item(i),info.widget);itemObjectInfo.append(info);/* 连接信号槽 */connect(info.switchButton,SIGNAL(toggled(bool)),this,SLOT(switchButtonClicked(bool)));/* 获取数据库里的闹钟开关状态 */QModelIndex qindex = model->index(i, 2);if (model->data(qindex).toBool())/* 设置列表里的闹钟开关按钮状态 */info.switchButton->setToggle(true);}/* 按钮 */connect(addAlarm, SIGNAL(clicked()), this,SLOT(addAlarmClicked()));connect(yesButton, SIGNAL(clicked()), this,SLOT(yesButtonClicked()));connect(cancelButton, SIGNAL(clicked()), this,SLOT(cancelButtonClicked()));/* 列表 */connect(listWidget,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(listWidgetItemClicked(QListWidgetItem*)));
}MainWindow::~MainWindow()
{/* 关闭数据库 */sqlDatabase.close();
}void MainWindow::addAlarmClicked()
{/* 选择时间对话框里显示当前系统时间 */hourPicker->setValue(QTime::currentTime().hour());minutePicker->setValue(QTime::currentTime().minute());/* 取消按钮显示文本为"取消" */cancelButton->setText("取消");/* 如果是点击添加闹钟的按钮,则设置闹钟列表的索引index为-1 */listWidget->setCurrentRow(-1);/* 显示对话框 */alarmDialog->show();
}void MainWindow::listWidgetItemClicked(QListWidgetItem *item)
{/* 从被点击项里获取闹钟数据 */QStringList list =listWidget->item(listWidget->row(item))->text().split(":");/* 选择时间对话框里显示被选择项的时间 */hourPicker->setValue(list.at(0).toInt());minutePicker->setValue(list.at(1).toInt());/* 取消按钮显示文本为"删除" */cancelButton->setText("删除");/* 显示闹钟选择对话框 */alarmDialog->show();/* 作用使其失去选择 */listWidget->clearSelection();
}void MainWindow::yesButtonClicked()
{/* 获取数值选择值的数据,转为字符串 */QString hour;QString minute;if (hourPicker->readValue() < 10)hour = "0" + QString::number(hourPicker->readValue()) + ":";elsehour = QString::number(hourPicker->readValue()) + ":";if (minutePicker->readValue() < 10)minute = "0" + QString::number(minutePicker->readValue());elseminute = QString::number(minutePicker->readValue());/* 如果不是选中闹钟列表的数据 */if (listWidget->currentRow() == -1) {/* 插入一行数据,闹钟时间为选择的闹钟时间 */int row = model->rowCount();/* 插入数据到数据库 */model->insertRow(row);model->setData(model->index(row, 0), row + 1);model->setData(model->index(row, 1), hour + minute);model->setData(model->index(row, 2), "true");model->submit();/* 添加闹钟到列表 */listWidget->addItem(hour + minute);/* 添加到容器 */ItemObjectInfo info;info.widget = new QWidget();info.switchButton = new SwitchButton();info.hBoxLayout = new QHBoxLayout();info.switchButton->setMaximumSize(55, 30);info.switchButton->setMinimumSize(55, 30);info.hBoxLayout->setContentsMargins(0, 0, 0, 0);info.hBoxLayout->setAlignment(Qt::AlignRight);info.hBoxLayout->addWidget(info.switchButton);info.widget->setLayout(info.hBoxLayout);info.switchButton->setToggle(true);/* 连接信号槽 */connect(info.switchButton, SIGNAL(toggled(bool)), this,SLOT(switchButtonClicked(bool)));listWidget->setItemWidget(listWidget->item(listWidget->count() - 1),info.widget);itemObjectInfo.append(info);} else {/* 修改数据(更新闹钟数据) */int row =  listWidget->currentRow();model->setData(model->index(row, 0), row + 1);model->setData(model->index(row, 1), hour + minute);model->setData(model->index(row, 2), "true");model->submit();/* 设置当前项的闹钟文本 */listWidget->currentItem()->setText(hour + minute);}/* 再确保提交 */if (model->isDirty())model->submitAll();/* 关闭对话框 */alarmDialog->close();
}void MainWindow::cancelButtonClicked()
{if (cancelButton->text() == "删除") {/* 删除数据库整一行数据 */model->removeRow(listWidget->currentRow());model->submit();/* 执行上面语句 */model->select();itemObjectInfo.remove(listWidget->currentRow());listWidget->takeItem(listWidget->currentRow());}/* 再确保提交 */if (model->isDirty())model->submitAll();/* 关闭对话框 */alarmDialog->close();
}/* 当点击闹钟开关时,将闹钟开关状态同步更新到数据库里 */
void MainWindow::switchButtonClicked(bool checked)
{listWidget->clearSelection();SwitchButton *button = (SwitchButton *)sender();for (int i = 0; i < itemObjectInfo.count(); i++) {if (button == itemObjectInfo.at(i).switchButton) {if (checked) {model->setData(model->index(i, 2), "true");listWidget->item(i)->setTextColor(QColor(22, 22, 22, 225));} else {model->setData(model->index(i, 2), "false");listWidget->item(i)->setTextColor(QColor(22, 22, 22, 60));}model->submit();break;}}
}

numberpicker.h

#ifndef NUMBERPICKER_H
#define NUMBERPICKER_H#include <QMainWindow>
#include <QPropertyAnimation>class NumberPicker : public QWidget
{Q_OBJECTQ_PROPERTY(int deviation READ readDeviation WRITE setDeviation )
public:NumberPicker(QWidget *parent = nullptr);~NumberPicker();/* 设置最大值与最小值的范围 */void setRange(int min, int max);/* 读取当前值 */int readValue();protected:void mousePressEvent(QMouseEvent *);void mouseMoveEvent(QMouseEvent *);void mouseReleaseEvent(QMouseEvent *);void wheelEvent(QWheelEvent *);void paintEvent(QPaintEvent *);public:/* 描绘数字 */void paintNum(QPainter &painter, int num, int deviation);/* 使选中的数字回到屏幕中间 */void homing();/* 鼠标移动偏移量,默认为0 */int readDeviation();/* 设置偏移量 */void setDeviation(int n);/* 设置字体大小 */void setNumSize(int);/* 设置间隔大小 */void setInterval(int);/* 设置分格数量,一般设置为3、5、7... */void setDevide(int);/* 设置数字颜色,设置rgb的数值 */void setNumberColor(QRgb rgb);/* 设置当前值 */void setValue(int value);signals:void currentValueChanged(int value);void deviationChange(int deviation);private:/* 最小值 */int minRange;/* 最大值 */int maxRange;/* 当前选中的值 */int currentValue;/* 鼠标是否按下 */bool isDragging;/* 偏移量,记录鼠标按下后移动的垂直距离 */int deviation;/* 鼠标按下的垂直位置 */int mouseSrcPos;/* 数字大小 */int numSize;/* 动画 */QPropertyAnimation *homingAni;/* 间隔大小 */int interval;/* 分格数量 */int devide;/* 数字颜色 */QColor numberColor;
};
#endif // NUMBERPICKER_H

numberpicker.cpp

#include <QMouseEvent>
#include <QDebug>
#include "numberpicker.h"
#include <QPainter>NumberPicker::NumberPicker(QWidget *parent) :/* 最小值默认为0 */minRange(0),/* 最大值默认60 */maxRange(60),/* 当前值默认0 */currentValue(0),/* 按下标志位为假 */isDragging(false),/* 默认偏移量为0 */deviation(0),/* 数值越大 */numSize(15),/* 间隔为1 */interval(1),/* 默认分成3格 */devide(3),/* 默认颜色黑色 */numberColor(0, 0, 0)
{setParent(parent);setMinimumSize(50, 150);homingAni = new QPropertyAnimation(this, "deviation");homingAni->setDuration(300);homingAni->setEasingCurve(QEasingCurve::OutQuad);
}NumberPicker::~NumberPicker()
{}void NumberPicker::setRange(int min, int max)
{minRange = min;maxRange = max;if (currentValue < min) {currentValue = min;}if (currentValue > max) {currentValue = max;}repaint();
}int NumberPicker::readValue()
{return currentValue;
}void NumberPicker::mousePressEvent(QMouseEvent *e)
{homingAni->stop();isDragging = true;mouseSrcPos = e->pos().y();QWidget::mousePressEvent(e);
}void NumberPicker::mouseMoveEvent(QMouseEvent *e)
{if (isDragging){deviation = e->pos().y() - mouseSrcPos;/* 若移动速度过快,则进行限制 */if (deviation > (height() - 1) / devide) {deviation = (height() - 1) / devide;} else if (deviation < -(height() - 1) / devide) {deviation = -( height() - 1) / devide;}emit deviationChange(deviation / ((height() - 1) / devide));repaint();}
}void NumberPicker::mouseReleaseEvent(QMouseEvent *)
{if (isDragging) {isDragging = false;homing();}
}void NumberPicker::wheelEvent(QWheelEvent *e)
{if (e->delta() > 0) {deviation = (this->height() - 1) / devide;} else {deviation = -(this->height() - 1) / devide;}homing();repaint();
}void NumberPicker::paintEvent(QPaintEvent *)
{QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing, true);int Height = height() - 1;if (deviation >= Height / devide && currentValue > minRange ) {mouseSrcPos += Height / devide;deviation -= Height / devide;currentValue -= interval;/* 负数处理 */if (currentValue < 0)currentValue = maxRange + currentValue;}if (deviation <= -Height / devide && currentValue < maxRange ) {mouseSrcPos -= Height / devide;deviation += Height / devide;currentValue += interval;}if (qAbs(int(currentValue)) >= int(maxRange))currentValue = minRange;paintNum(painter, qAbs(int(currentValue + maxRange) % maxRange),deviation);paintNum(painter,qAbs((currentValue - interval + maxRange) % maxRange),deviation - Height / devide);paintNum(painter,qAbs((currentValue + interval + maxRange) % maxRange),deviation + Height / devide);for (int i = 2; i <= devide / 2; ++i) {if (qAbs(currentValue - interval * i) >= minRange) {paintNum(painter,qAbs((currentValue - interval * i + maxRange)% maxRange),deviation - Height / devide * i);}if (qAbs(currentValue + interval * i) <= maxRange) {paintNum(painter,qAbs((currentValue + interval * i + maxRange)% maxRange),deviation + Height / devide * i);}}
}void NumberPicker::paintNum(QPainter &painter, int num, int deviation)
{int Width = width() - 1;int Height = height() - 1;/* 偏移量越大,数字越小 *///int size = (Height - qAbs(deviation)) / numSize;int size = (Height - qAbs(deviation)) * numSize / 80;int transparency = 255 - 255 * qAbs(deviation) / Height;int height = Height / devide;int y = Height / 2 + deviation - height / 2;QFont font;font.setPixelSize(size);painter.setFont(font);painter.setPen(QColor(numberColor.red(),numberColor.green(),numberColor.blue(),transparency));if ( y >= 0 && y + height < Height) {//painter.drawRect(0, y, Width, height);if (num < 10)painter.drawText(QRectF(0, y, Width, height),Qt::AlignCenter,"0" + QString::number(num, 'f', 0));elsepainter.drawText(QRectF(0, y, Width, height),Qt::AlignCenter,QString::number(num, 'f', 0));}
}void NumberPicker::homing()
{if (deviation > height() / 10) {homingAni->setStartValue((height() - 1 ) / 8 - deviation);homingAni->setEndValue(0);currentValue -= interval;} else if (deviation > -height() / 10) {homingAni->setStartValue(deviation);homingAni->setEndValue(0);} else if (deviation < -height() / 10) {homingAni->setStartValue(-(height() - 1) / 8 - deviation);homingAni->setEndValue(0);currentValue += interval;}emit currentValueChanged(currentValue);homingAni->start();
}int NumberPicker::readDeviation()
{return deviation;
}void NumberPicker::setDeviation(int n)
{deviation = n;repaint();
}void NumberPicker::setNumSize(int size)
{numSize = size;repaint();
}void NumberPicker::setInterval(int n)
{interval = n;repaint();
}void NumberPicker::setDevide(int n)
{devide = n;repaint();
}void NumberPicker::setNumberColor(QRgb rgb)
{numberColor.setRgb(rgb);repaint();
}void NumberPicker::setValue(int value)
{if (value < minRange || value > maxRange) {qDebug()<<"数值设置必须在"<<minRange<<"和"<<maxRange<<"之间"<<endl;return;}currentValue = value;repaint();
}

二,数据库proC代码(与QT那个不是同一种情形和用法)

1,数据库服务器创建

/* Result Sets Interface */
#ifndef SQL_CRSR
#  define SQL_CRSRstruct sql_cursor{unsigned int curocn;void *ptr1;void *ptr2;unsigned int magic;};typedef struct sql_cursor sql_cursor;typedef struct sql_cursor SQL_CURSOR;
#endif /* SQL_CRSR *//* Thread Safety */
typedef void * sql_context;
typedef void * SQL_CONTEXT;/* Object support */
struct sqltvn
{unsigned char *tvnvsn; unsigned short tvnvsnl; unsigned char *tvnnm;unsigned short tvnnml; unsigned char *tvnsnm;unsigned short tvnsnml;
};typedef struct sqltvn sqltvn;struct sqladts
{unsigned int adtvsn; unsigned short adtmode; unsigned short adtnum;  sqltvn adttvn[1];       
};
typedef struct sqladts sqladts;static struct sqladts sqladt = {1,1,0,
};/* Binding to PL/SQL Records */
struct sqltdss
{unsigned int tdsvsn; unsigned short tdsnum; unsigned char *tdsval[1]; 
};
typedef struct sqltdss sqltdss;
static struct sqltdss sqltds =
{1,0,
};/* File name & Package Name */
struct sqlcxp
{unsigned short fillen;char  filnam[10];
};
static struct sqlcxp sqlfpn =
{9,"struct.pc"
};static unsigned int sqlctx = 42123;static struct sqlexd {unsigned int   sqlvsn;unsigned int   arrsiz;unsigned int   iters;unsigned int   offset;unsigned short selerr;unsigned short sqlety;unsigned int   occurs;short *cud;unsigned char  *sqlest;char  *stmt;sqladts *sqladtp;sqltdss *sqltdsp;void  **sqphsv;unsigned int   *sqphsl;int   *sqphss;void  **sqpind;int   *sqpins;unsigned int   *sqparm;unsigned int   **sqparc;unsigned short  *sqpadto;unsigned short  *sqptdso;unsigned int   sqlcmax;unsigned int   sqlcmin;unsigned int   sqlcincr;unsigned int   sqlctimeout;unsigned int   sqlcnowait;int   sqfoff;unsigned int   sqcmod;unsigned int   sqfmod;void  *sqhstv[4];unsigned int   sqhstl[4];int   sqhsts[4];void  *sqindv[4];int   sqinds[4];unsigned int   sqharm[4];unsigned int   *sqharc[4];unsigned short  sqadto[4];unsigned short  sqtdso[4];
} sqlstm = {12,4};/* SQLLIB Prototypes */
extern sqlcxt (/*_ void **, unsigned int *,struct sqlexd *, struct sqlcxp * _*/);
extern sqlcx2t(/*_ void **, unsigned int *,struct sqlexd *, struct sqlcxp * _*/);
extern sqlbuft(/*_ void **, char * _*/);
extern sqlgs2t(/*_ void **, char * _*/);
extern sqlorat(/*_ void **, unsigned int *, void * _*/);/* Forms Interface */
static int IAPSUCC = 0;
static int IAPFAIL = 1403;
static int IAPFTL  = 535;
extern void sqliem(/*_ char *, int * _*/);static char *sq0002 = 
"select id ,first_name ,salary  from s_emp            ";typedef struct { unsigned short len; unsigned char arr[1]; } VARCHAR;
typedef struct { unsigned short len; unsigned char arr[1]; } varchar;/* CUD (Compilation Unit Data) Array */
static short sqlcud0[] =
{12,4130,1,0,0,
5,0,0,1,0,0,27,15,0,0,4,4,0,1,0,1,97,0,0,1,10,0,0,1,10,0,0,1,10,0,0,
36,0,0,2,53,0,9,17,0,0,0,0,0,1,0,
51,0,0,2,0,0,13,21,0,0,3,0,0,1,0,2,3,0,0,2,97,0,0,2,4,0,0,
78,0,0,2,0,0,15,24,0,0,0,0,0,1,0,
93,0,0,3,0,0,30,25,0,0,0,0,0,1,0,
};#include<stdio.h>
/* exec sql include sqlca;*/ 
/** $Header: sqlca.h 24-apr-2003.12:50:58 mkandarp Exp $ sqlca.h *//* Copyright (c) 1985, 2003, Oracle Corporation.  All rights reserved.  *//*
NAMESQLCA : SQL Communications Area.
FUNCTIONContains no code. Oracle fills in the SQLCA with status infoduring the execution of a SQL stmt.
NOTES*****************************************************************                                                        ****** This file is SOSD.  Porters must change the data types ****** appropriately on their platform.  See notes/pcport.doc ****** for more information.                                  ******                                                        *****************************************************************If the symbol SQLCA_STORAGE_CLASS is defined, then the SQLCAwill be defined to have this storage class. For example:#define SQLCA_STORAGE_CLASS externwill define the SQLCA as an extern.If the symbol SQLCA_INIT is defined, then the SQLCA will bestatically initialized. Although this is not necessary in orderto use the SQLCA, it is a good pgming practice not to haveunitialized variables. However, some C compilers/OS's don'tallow automatic variables to be init'd in this manner. Therefore,if you are INCLUDE'ing the SQLCA in a place where it would bean automatic AND your C compiler/OS doesn't allow this styleof initialization, then SQLCA_INIT should be left undefined --all others can define SQLCA_INIT if they wish.If the symbol SQLCA_NONE is defined, then the SQLCA variable willnot be defined at all.  The symbol SQLCA_NONE should not be definedin source modules that have embedded SQL.  However, source modulesthat have no embedded SQL, but need to manipulate a sqlca structpassed in as a parameter, can set the SQLCA_NONE symbol to avoidcreation of an extraneous sqlca variable.MODIFIEDlvbcheng   07/31/98 -  long to intjbasu      12/12/94 -  Bug 217878: note this is an SOSD filelosborne   08/11/92 -  No sqlca var if SQLCA_NONE macro set Clare      12/06/84 - Ch SQLCA to not be an extern.Clare      10/21/85 - Add initialization.Bradbury   01/05/86 - Only initialize when SQLCA_INIT setClare      06/12/86 - Add SQLCA_STORAGE_CLASS option.
*/#ifndef SQLCA
#define SQLCA 1struct   sqlca{/* ub1 */ char    sqlcaid[8];/* b4  */ int     sqlabc;/* b4  */ int     sqlcode;struct{/* ub2 */ unsigned short sqlerrml;/* ub1 */ char           sqlerrmc[70];} sqlerrm;/* ub1 */ char    sqlerrp[8];/* b4  */ int     sqlerrd[6];/* ub1 */ char    sqlwarn[8];/* ub1 */ char    sqlext[8];};#ifndef SQLCA_NONE 
#ifdef   SQLCA_STORAGE_CLASS
SQLCA_STORAGE_CLASS struct sqlca sqlca
#elsestruct sqlca sqlca
#endif#ifdef  SQLCA_INIT= {{'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '},sizeof(struct sqlca),0,{ 0, {0}},{'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '},{0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 0, 0, 0, 0, 0}}
#endif;
#endif#endif/* end SQLCA */struct emp
{int id;char name[30];double salary;
};int main()
{/* exec sql begin declare section; */ char userpasswd[20]="openlab/open123";struct emp empx;/* exec sql end declare section; */ /* exec sql connect:userpasswd; */ {struct sqlexd sqlstm;sqlstm.sqlvsn = 12;sqlstm.arrsiz = 4;sqlstm.sqladtp = &sqladt;sqlstm.sqltdsp = &sqltds;sqlstm.iters = (unsigned int  )10;sqlstm.offset = (unsigned int  )5;sqlstm.cud = sqlcud0;sqlstm.sqlest = (unsigned char  *)&sqlca;sqlstm.sqlety = (unsigned short)256;sqlstm.occurs = (unsigned int  )0;sqlstm.sqhstv[0] = (         void  *)userpasswd;sqlstm.sqhstl[0] = (unsigned int  )20;sqlstm.sqhsts[0] = (         int  )20;sqlstm.sqindv[0] = (         void  *)0;sqlstm.sqinds[0] = (         int  )0;sqlstm.sqharm[0] = (unsigned int  )0;sqlstm.sqadto[0] = (unsigned short )0;sqlstm.sqtdso[0] = (unsigned short )0;sqlstm.sqphsv = sqlstm.sqhstv;sqlstm.sqphsl = sqlstm.sqhstl;sqlstm.sqphss = sqlstm.sqhsts;sqlstm.sqpind = sqlstm.sqindv;sqlstm.sqpins = sqlstm.sqinds;sqlstm.sqparm = sqlstm.sqharm;sqlstm.sqparc = sqlstm.sqharc;sqlstm.sqpadto = sqlstm.sqadto;sqlstm.sqptdso = sqlstm.sqtdso;sqlstm.sqlcmax = (unsigned int )100;sqlstm.sqlcmin = (unsigned int )2;sqlstm.sqlcincr = (unsigned int )1;sqlstm.sqlctimeout = (unsigned int )0;sqlstm.sqlcnowait = (unsigned int )0;sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}/* exec sql declare empcur cursor for select id,first_name,salary from s_emp; */ /* exec sql open empcur; */ {struct sqlexd sqlstm;sqlstm.sqlvsn = 12;sqlstm.arrsiz = 4;sqlstm.sqladtp = &sqladt;sqlstm.sqltdsp = &sqltds;sqlstm.stmt = sq0002;sqlstm.iters = (unsigned int  )1;sqlstm.offset = (unsigned int  )36;sqlstm.selerr = (unsigned short)1;sqlstm.cud = sqlcud0;sqlstm.sqlest = (unsigned char  *)&sqlca;sqlstm.sqlety = (unsigned short)256;sqlstm.occurs = (unsigned int  )0;sqlstm.sqcmod = (unsigned int )0;sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}/* exec sql whenever notfound do break; */ for(;;){/* exec sql fetch empcur into:empx; */ {struct sqlexd sqlstm;sqlstm.sqlvsn = 12;sqlstm.arrsiz = 4;sqlstm.sqladtp = &sqladt;sqlstm.sqltdsp = &sqltds;sqlstm.iters = (unsigned int  )1;sqlstm.offset = (unsigned int  )51;sqlstm.selerr = (unsigned short)1;sqlstm.cud = sqlcud0;sqlstm.sqlest = (unsigned char  *)&sqlca;sqlstm.sqlety = (unsigned short)256;sqlstm.occurs = (unsigned int  )0;sqlstm.sqfoff = (           int )0;sqlstm.sqfmod = (unsigned int )2;sqlstm.sqhstv[0] = (         void  *)&empx.id;sqlstm.sqhstl[0] = (unsigned int  )sizeof(int);sqlstm.sqhsts[0] = (         int  )0;sqlstm.sqindv[0] = (         void  *)0;sqlstm.sqinds[0] = (         int  )0;sqlstm.sqharm[0] = (unsigned int  )0;sqlstm.sqadto[0] = (unsigned short )0;sqlstm.sqtdso[0] = (unsigned short )0;sqlstm.sqhstv[1] = (         void  *)empx.name;sqlstm.sqhstl[1] = (unsigned int  )30;sqlstm.sqhsts[1] = (         int  )0;sqlstm.sqindv[1] = (         void  *)0;sqlstm.sqinds[1] = (         int  )0;sqlstm.sqharm[1] = (unsigned int  )0;sqlstm.sqadto[1] = (unsigned short )0;sqlstm.sqtdso[1] = (unsigned short )0;sqlstm.sqhstv[2] = (         void  *)&empx.salary;sqlstm.sqhstl[2] = (unsigned int  )sizeof(double);sqlstm.sqhsts[2] = (         int  )0;sqlstm.sqindv[2] = (         void  *)0;sqlstm.sqinds[2] = (         int  )0;sqlstm.sqharm[2] = (unsigned int  )0;sqlstm.sqadto[2] = (unsigned short )0;sqlstm.sqtdso[2] = (unsigned short )0;sqlstm.sqphsv = sqlstm.sqhstv;sqlstm.sqphsl = sqlstm.sqhstl;sqlstm.sqphss = sqlstm.sqhsts;sqlstm.sqpind = sqlstm.sqindv;sqlstm.sqpins = sqlstm.sqinds;sqlstm.sqparm = sqlstm.sqharm;sqlstm.sqparc = sqlstm.sqharc;sqlstm.sqpadto = sqlstm.sqadto;sqlstm.sqptdso = sqlstm.sqtdso;sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);if (sqlca.sqlcode == 1403) break;
}printf("%d,%s,%lf\n",empx.id,empx.name,empx.salary);}/* exec sql close empcur; */ {struct sqlexd sqlstm;sqlstm.sqlvsn = 12;sqlstm.arrsiz = 4;sqlstm.sqladtp = &sqladt;sqlstm.sqltdsp = &sqltds;sqlstm.iters = (unsigned int  )1;sqlstm.offset = (unsigned int  )78;sqlstm.cud = sqlcud0;sqlstm.sqlest = (unsigned char  *)&sqlca;sqlstm.sqlety = (unsigned short)256;sqlstm.occurs = (unsigned int  )0;sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}/* exec sql commit work release; */ 
{struct sqlexd sqlstm;sqlstm.sqlvsn = 12;sqlstm.arrsiz = 4;sqlstm.sqladtp = &sqladt;sqlstm.sqltdsp = &sqltds;sqlstm.iters = (unsigned int  )1;sqlstm.offset = (unsigned int  )93;sqlstm.cud = sqlcud0;sqlstm.sqlest = (unsigned char  *)&sqlca;sqlstm.sqlety = (unsigned short)256;sqlstm.occurs = (unsigned int  )0;sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn);
}
}

2,用户端访问 
 

#include<stdio.h>
#include<string.h>
exec sql include sqlca;
int main()
{exec sql begin declare section;char userpasswd[20]="openlab/open123";char sqlstr[100];exec sql end declare section;exec sql connect:userpasswd;strcpy(sqlstr,"create table"" wangjie36(id number(10)","name varchar2(20))");exec sql execute immediate :sqlstr;exec sql commit work release;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1319952.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

每日一题(leetcode169):多数元素-哈希、随机、分治

哈希&#xff1a; class Solution { public:int majorityElement(vector<int>& nums) {int lennums.size();unordered_map<int,int> map;for (int i0;i<len;i){if(map.find(nums[i])map.end()){map[nums[i]]1;}else{map[nums[i]];}}int seqlen/2;int ansnu…

FebHost:注册人工智能.AI域名的优势?

近年来,人工智能技术的飞速发展,让AI在各行各业扮演着愈发重要的角色。在这一背景下,.AI域名凭借其独特的优势,正成为越来越多AI从业者的首选。那么,.AI域名到底有哪些亮点,值得广大AI企业和个人关注呢?记者进行了深入探访。 专业形象加分 彰显技术实力 要说.AI域名的最大优势…

yolov5关键点检测-实现溺水检测与警报提示(代码+原理)

基于YOLOv5的关键点检测应用于溺水检测与警报提示是一种结合深度学习与计算机视觉技术的安全监控解决方案。该项目通常会利用YOLOv5强大的实时目标检测能力&#xff0c;并通过扩展或修改网络结构以支持人体关键点检测&#xff0c;来识别游泳池或其他水域中人们的行为姿态。 项…

为何网易游戏会选择引入OceanBase数据库

本文作者&#xff1a;田维繁&#xff0c;网易游戏关系型数据库小组负责人 作为中国游戏开发领域的佼佼者&#xff0c;网易游戏始终站在网络游戏自主研发的前沿。其产品及周边产品线丰富多样&#xff0c;因此&#xff0c;为满足各种业务场景的需求&#xff0c;需要多种不同的数据…

delphi获取windows右下角任务栏图标信息

今天在群里,看有人问怎么获取windows右下角任务栏图标信息 win7 x64 测试通过 unit Unit1;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs,commctrl, Vcl.StdCtr…

烂笔头笔记:Windows 11下照片查看器显示偏色问题修复

本文出处&#xff1a;http://blog.csdn.net/chaijunkun/article/details/137278931&#xff0c;转载请注明。由于本人不定期会整理相关博文&#xff0c;会对相应内容作出完善。因此强烈建议在原始出处查看此文。 最近在研究HDR视频的截图算法&#xff0c;目的就是生成色彩正确…

并发编程01-深入理解Java并发/线程等待/通知机制

为什么我们要学习并发编程&#xff1f; 最直白的原因&#xff0c;因为面试需要&#xff0c;我们来看看美团和阿里对 Java 岗位的 JD&#xff1a; 从上面两大互联网公司的招聘需求可以看到&#xff0c; 大厂的 Java 岗的并发编程能力属于标配。 而在非大厂的公司&#xff0c; 并…

如何在 Android 上恢复已删除的短信

在 Android 上恢复短信是一件棘手的事情。以下是限制、您的选择以及如何为未来做好准备。 丢失手机数据令人心碎。无论您不小心删除了某些内容&#xff0c;还是手机被盗或损坏&#xff0c;您不可替代的照片、亲人的消息等都可能会立即消失。 如果您需要从 Android 手机恢复一…

腾讯云最新活动及优惠券领取入口整理汇总

腾讯云作为国内领先的云服务提供商&#xff0c;始终致力于为广大用户提供更优质、更便捷的云服务体验。为了帮助用户更好地了解和参与腾讯云的活动&#xff0c;本文将对腾讯云的最新活动及优惠券领取入口进行整理汇总。 一、优惠券领取入口 领取入口&#xff1a;点此领取 领券…

OSPF实验1

1,配置IP地址 [R1]dis ip interface brief Interface IP Address/Mask Physical Protocol GigabitEthernet0/0/0 200.1.1.1/24 up up GigabitEthernet0/0/1 10.1.1.1/24 up …

Linux学习笔记————C 语言版 LED 灯实验

这里写目录标题 一、实验程序编写二、 汇编部分实验程序编写三、C 语言部分实验程序编写四、编译下载验证 汇编 LED 灯实验中&#xff0c;我们讲解了如何使用汇编来编写 LED 灯驱动&#xff0c;实际工作中是很少用到汇编去写嵌入式驱动的&#xff0c;毕竟汇编太难&#xff0c;而…

加密、签名、验签、证书、对称加密、非对称加密【部分知识点】

文章目录 前言如图一些概念区分不可逆加密可逆加密签名和验签 前言 总结一些涉及到OTA升级相关的数据加密知识点&#xff0c;仅作为笔记记录&#xff0c;仅部分总结&#xff0c;细节部分可以私聊我。 如图 一些概念区分 不可逆加密 哈希算法是一个统称&#xff0c;它分为MD…

HarmonyOS 应用开发之同应用跨设备数据同步

概述 场景介绍 跨设备数据同步功能&#xff08;即分布式功能&#xff09;&#xff0c;指将数据同步到一个组网环境中的其他设备。常用于用户应用程序数据内容在可信认证的不同设备间&#xff0c;进行自由同步、修改和查询。 例如&#xff1a;当设备1上的应用A在分布式数据库…

打印日志(JAVA)

1、通过导入包的形式 package com.example.demo;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; RequestMapping("/log&q…

信息系统项目管理师——第17章项目干系人管理

本章节内容属于10大管理知识领域&#xff0c;选择、案例、论文都会考。 选择题&#xff0c;稳定考1-2分左右&#xff0c;新教材基本考课本原话&#xff0c;这个分不能丢。 案例题&#xff0c;本期考的概率一般。 论文题&#xff0c;202205期考过。 1管理基础 管理的重要性 为…

北京可安可智能科技有限公司:创新巡检,智慧守护

在智能化的浪潮中&#xff0c;北京可安可智能科技有限公司以其创新的机器人巡检产品和方案&#xff0c;正引领安防巡检领域迈向一个新的里程碑。作为人工智能科技创新的先锋&#xff0c;我们专注于利用科技力量提升行业标准&#xff0c;降低用人成本&#xff0c;为安保场景提供…

番外篇 | 引入RepVGG重参数化模块 | 即插即用,实现有效涨点

前言:Hello大家好,我是小哥谈。重参数化模块(RepVGG)是一种深度学习模型,它借鉴了卷积神经网络(CNN)中的卷积操作,并对其进行了一些改进,以提高模型的性能和计算效率。RepVGG的核心思想是通过重参数化技术,对卷积操作中的权重进行重新配置,以适应不同的数据集和任务…

file_get_contents(‘php://input‘); 这个postman要如何传参

在 Postman 中传递参数给 file_get_contents(php://input); 是通过请求的 Body 部分来实现的。使用 Postman 进行 API 接口测试时&#xff0c;可以按照以下步骤来传递参数&#xff1a; 打开 Postman 并创建一个新的请求。在请求的 URL 地址栏输入你的 API 地址。选择请求方法为…

基于java的java二手手机回收平台系统

开发语言&#xff1a;Java 框架&#xff1a;ssm 技术&#xff1a;JSP JDK版本&#xff1a;JDK1.8 服务器&#xff1a;tomcat7 数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09; 数据库工具&#xff1a;Navicat11 开发软件&#xff1a;eclipse/myeclip…

【力扣】11. 盛最多水的容器

11. 盛最多水的容器 题目描述 给定一个长度为 n 的整数数组 height 。有 n 条垂线&#xff0c;第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。 找出其中的两条线&#xff0c;使得它们与 x 轴共同构成的容器可以容纳最多的水。 返回容器可以储存的最大水量。 说明&…