QODBC连接到SQL Server,但无法执行查询

QODBC connects to SQL server but can not execute queries

本文关键字:执行 查询 连接 SQL Server QODBC      更新时间:2023-10-16

标题说的大部分发生了。我可以确认我要连接到数据库,并且在尝试查询之前,我肯定会打开连接。问题是,每当我通过Exec(查询)发送查询时,我会遇到一个错误,说明我的QODBC驱动程序没有加载。如果确实如此,那么我什至如何开始连接?更重要的是,如何重写代码或配置某些内容,以使我的驱动程序被加载?

一些要填补空白的东西:

  • popupwindows.h是控制AlertWindow,ErrorWindow和Success窗口的原因,根本不会影响任何事情。
  • 我得到的所有常数和数据都很好,所以我如何将SQL查询的方式放在一起,示例:

插入Regtable(Project_ID,Project_Name,Cycle,Number,b1,b2,b3,b3,byt,vert,偏斜)值(117.001,ZT400-300DPI,0,1,1,1,1,2,2,2,2,2,2,2,2,3,4,4,5,5,5)

  • 当我启动senddata()函数时,我将获得db.isopen()和db.isvalid()的真实值,尽管一旦我用他们检查的内容排除了错误,我就将它们取出。

  • 我在我的应用程序目录中都有QSQLODBC.DLL和QSQLODBCD.dll,但最重要的是

    C:pathtobuiltprojectdebugsqldrivers
    

对不起壁编码,但我想确保大家拥有所需的一切。

databaseconnection.cpp:

#include <QFile>
#include <QTextStream>
#include <QSqlError>
#include <QSqlQuery>
#include "databaseconnection.h"
#include "popupwindows.h"
DatabaseConnection::DatabaseConnection(QWidget *parent, QString type) {
    this->setParent(parent);
    log = new EventLog("DatabaseConnection");
    connected = false;
    db = QSqlDatabase::addDatabase("QODBC");        //note that this is a QString, not a QSqlDriver
    loadParams();
    connectDB(type);
}
DatabaseConnection::~DatabaseConnection() {
    db.close();
}
bool DatabaseConnection::connectDB( QString driverString) {
    if (!db.isOpen()) {
        if (db.isValid() && db.isDriverAvailable(driverString)) {
            QString connParams = QString("Driver={SQL SERVER};DSN=ODBCDriverForLocal;SERVER=" + server +";DATABASE="
                                     + database + ";Uid=" + username +";Pwd=" + password);
        db.setDatabaseName(connParams);
        log->print("Attempting to connect to database: " + connParams);
        if(!db.open()) {
            log->printerr("Could not connect to database: " + db.lastError().text());
            connected = false;
            ErrorWindow(0, "Database connection could not be establishedn" + db.lastError().text());
               return false;
            } else {
                log->print("Database connection established.");
                connected = true;
                SuccessWindow(0, "Database connection established");
                return true;
            }
        } else {
            ErrorWindow(0, "Driver not available or valid.");
        }
    } return true;
}
void DatabaseConnection::disconnect() {
    if (db.isOpen()) {
        db.close();
    }
}
void DatabaseConnection::loadParams() {
    QFile settings("dbSettings.txt");
    if (!settings.open(QIODevice::ReadOnly | QIODevice::Text)) {
        server = QString("Server String");
        database = QString("Database String");
        username = QString("Username");
        password = QString("Password");
    } else {
        QTextStream in(&settings);
        server = in.readLine();
        database = in.readLine();
        username = in.readLine();
        password = in.readLine();
    }
}
void DatabaseConnection::updateParams(QString params) {
    disconnect();
    QStringList parameters = params.split(";");
    server = parameters[0];
    database = parameters[1];
    username = parameters[2];
    password = parameters[3];
}
void DatabaseConnection::sendData(QString mode, QVector<QString> *data) {
    log->print("Prepping Data for Sending...");
    int cols;
    if(connectDB()) {
        db.exec(QString("USE " + database));
        QVector<QString> *dataQueue = new QVector<QString>;
        //collect all the data
        if (mode == QString("DK")) {
            extractData(mode, 15, 31, dataQueue, data);
            cols = 15;
        } else if (mode == QString("PQ")) {
            extractData(mode, 17, 200, dataQueue, data);
            cols = 17;
        } else {
            extractData(mode, 6, 200, dataQueue, data);
            cols = 6;
        }
        //create a query for each entry
        for (int i =0; i < (dataQueue->size() / cols); i++) {
            QString query("INSERT INTO ");
            emit requestConsts();
            if (mode == QString("Reg")) {
                 query += "RegTable(Project_ID, Project_Name, Cycle, Number, B1, B2, B3, Horiz, Vert, Skew) VALUES (";
            } else if (mode == QString("PQ")) {
                query += "PQTable(Project_ID, Project_Name, Cycle, Number, B1, B2, B3, [3DOTCD128_ScoreA], [3DOTCD128_GradeA],"
                     " [3DOTCD128_ScoreB], [3DOTCD128_GradeB], [10CR3DT128_Score], [10CR3DT128_Grade], [12CHAR3DOT39_Score],"
                     " [12CHAR3DOT39_Grade], [12CHAR4DOT39_Score], [12CHAR4DOT39_Grade], [2DOTDATAMATRIX_Score],"
                     " [2DOTDATAMATRIX_Grade], [3DOTDATAMATRIX_Score], [3DOTDATAMATRIX_Grade]) VALUES (";
            } else {
                 query += "DKTable(Project_ID, Project_Name, Cycle, Darkness, [3DOTCD128_ScoreA], [3DOTCD128_GradeA],"
                     " [3DOTCD128_ScoreB], [3DOTCD128_GradeB], [10CR3DT128_Score], [10CR3DT128_Grade], [12CHAR3DOT39_Score],"
                     " [12CHAR3DOT39_Grade], [12CHAR4DOT39_Score], [12CHAR4DOT39_Grade], [2DOTDATAMATRIX_Score],"
                     " [2DOTDATAMATRIX_Grade], [3DOTDATAMATRIX_Score], [3DOTDATAMATRIX_Grade]) VALUES (";
            }
            query += "" + constants;
            for (int j = 0; j < cols; j++) {
                int index = (i * cols) + j;
                query += ", " + dataQueue->at(index);
            }
            query += (")");
            log->print(query);
            QSqlQuery postedQuery = QSqlQuery(db);
            if (!postedQuery.exec(query)) {
                AlertWindow(0, this->lastError().text());
            }
        }
        //push the queries into action if not already being performed
        db.exec(QString("GO"));
        log->print("Data Sent.");
        SuccessWindow(0, "Data (theoretically) Sent!");
    }
}
void DatabaseConnection::getConsts(QString consts) {
    constants = consts;
}
void DatabaseConnection::extractData(QString mode, int cols, int rows, QVector<QString> *queue, QVector<QString> *data) {
    log->print("Extracting Data...");
    log->print(mode + " Mode");
    for (int row = 0; row < rows; row++) {
        QVector<QString> thisLine;
        for (int col = 0; col < cols; col++) {
            int index = (row * cols) + col;
            thisLine.append(data->at(index));
        }
        if (mode == QString("DK")) {
            if (!thisLine.at(0).isEmpty() && (!thisLine.at(1).isEmpty() || !thisLine.at(3).isEmpty() || !thisLine.at(5).isEmpty() || !thisLine.at(7).isEmpty() || !thisLine.at(9).isEmpty() || !thisLine.at(11).isEmpty() || !thisLine.at(13).isEmpty())) {
                for (int i = 0; i < cols; i++) {
                    queue->append(thisLine.at(i));
                    //log->print(thisLine.at(i));
                }
            }
        } else if (mode == QString("PQ")) {
            if ((!thisLine.at(0).isEmpty() && !thisLine.at(1).isEmpty() && !thisLine.at(2).isEmpty()) || (!thisLine.at(5).isEmpty() || !thisLine.at(7).isEmpty() || !thisLine.at(9).isEmpty() || !thisLine.at(11).isEmpty() || !thisLine.at(13).isEmpty() || !thisLine.at(15).isEmpty())) {
                for (int i = 0; i < cols; i++) {
                    queue->append(thisLine.at(i));
                    //log->print(thisLine.at(i));
                }
            }
        } else {
            if ((!thisLine.at(0).isEmpty() && !thisLine.at(1).isEmpty() && !thisLine.at(2).isEmpty()) || (!thisLine.at(3).isEmpty() || !thisLine.at(4).isEmpty() || !thisLine.at(5).isEmpty())) {
                for (int i = 0; i < cols; i++) {
                    queue->append(thisLine.at(i));
                    //log->print(thisLine.at(i));
                }
            }
        }
    }
}

编辑6/21/16-我做了更多研究,发现我正在错误地使用DSN参数。即使我正确使用它,我的问题仍然存在,所以那里没有太多进展,只有一些方便的花花公子学习。

我弄清楚了!值中需要包含的东西

INSERT INTO RegTable(Project_ID, Project_Name, Cycle, Number, B1, B2, B3, Horiz, Vert, Skew) VALUES ('117.001', 'ZT400-300DPI', '0', '1', '1', '2', '3', '4', '5', '6')

对我来说似乎有点愚蠢,但是我敢肯定,这是一个充分考虑的原因(如果您知道的话,一定会启发我!)。感谢您提供的帮助,这让我到了这里,就我而言,您回答了这个问题:)

旁注:错误报告是完全垃圾的。驱动程序和数据库报告"未加载驱动程序"甚至都不接近发生的事情。