标题说明了大部分内容。我可以确认我已连接到数据库,并且在尝试进行查询之前,我确实打开了连接。问题是,每次我通过exec(查询)发送查询时,我都会收到一条错误,指出我的QODBC驱动程序没有加载。如果情况确实如此,那我怎么连接开始呢?更重要的是,我如何重新编写代码或配置某些内容以便加载我的驱动程序?
填补空白的一些事情:
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) )
当我启动sendData()函数时,我得到了db.isOpen()和db.isValid()的真值,但是一旦我排除了他们正在检查的错误,我就将它们取出。
我的应用程序目录中的几个文件夹中都有qsqlodbc.dll和qsqlodbcd.dll,但最重要的是
C:\path\to\built\project\debug\sqldrivers
很抱歉墙上的代码,但我想确保你们都拥有所需的一切。
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 established\n" + 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参数。即使我正确地使用它,我的问题仍然存在,所以那里没有太多的进展,只是一些方便的花花公子学习。
答案 0 :(得分:0)
我明白了! VALUES中的内容需要包含在单引号中
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')
这对我来说似乎有点傻,但我确信这有一个经过深思熟虑的原因(如果你知道的话,请务必启发我!)。感谢那些帮助的人,它让我到这里,所以就我而言你回答了这个问题:)
旁注:错误报告对此是彻头彻尾的垃圾。驱动程序和数据库报告“驱动程序未加载”甚至都没有接近发生的情况。