PDA

View Full Version : Qt connection with SQL Server 2008 (Query not being processed)



antweb
11th March 2015, 10:47
I have created a database table in SQL Server 2008. I am successfully able to connect but not able to process my query.
Following is my program.


#include "db.h"
#include <QtGui/QApplication>
#include <QCoreApplication>
#include <QApplication>
#include <QtSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QDebug>
#include <QtSql/QSqlError>

bool createConnection();

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!createConnection()){

qDebug() << "Not connected!";
return 1;
}
else{

qDebug() << "Connected!";
QSqlQuery query;
query.exec("SELECT user_id FROM login_details");

while (query.next()) {
QString name = query.value(0).toString();
qDebug() << "user_id:" << name;
}
return 0;
}

return app.exec();
}

bool createConnection(){
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("Driver={SQL Server};Server=ANANTH-PC\SQLEXPRESS;Database=test;");
if(!db.open()){
qDebug()<<"ERROR: "<<QSqlError(db.lastError()).text();
return false;
}
else {
qDebug()<<"Ok";
return true;
}
}


What can be the possible problem?

^NyAw^
11th March 2015, 11:41
Hi,

What does "exec" returns?
You can also check database error and query error to know what is happening.

antweb
11th March 2015, 11:50
I put my commands out of the while and it gives me this
QSqlQuery::value: not positioned on a valid record
exec is returning false. Why?

But my record already exists. Is my syntax alright?

Lesiok
11th March 2015, 12:02
Why You are using SQLite driver for connecting to Microsoft SQL Server ?
Line 39 should be :
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");

antweb
11th March 2015, 12:14
On doing that,

ERROR: "[Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). QODBC3: Unable to connect"

^NyAw^
11th March 2015, 12:20
Hi,

You need to create a ODBC windows connection.

antweb
12th March 2015, 05:35
I created an ODBC connection. It is getting connected now. But my exec statement is still returning false. Please Help!
updated code:


#include "db.h"
#include <QtGui/QApplication>
#include <QCoreApplication>
#include <QApplication>
#include <QtSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QDebug>
#include <QtSql/QSqlError>
#include <QMessageBox>

bool createConnection();

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!createConnection()){

qDebug() << "Not connected!";
return 1;
}
else{
}
return app.exec();
}

bool createConnection(){
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={SQL Server};Server=ANANTH-PC;Database=demo;");
if(!db.open()){
qDebug()<<"ERROR: "<<QSqlError(db.lastError()).text();
return false;
}
else {
qDebug()<<"Ok";
qDebug() << "Connected!";
QSqlQuery query(db);
query.prepare("INSERT INTO login_details VALUES('A002','ABCD')");
if(query.exec())
QMessageBox::critical(0, "DB Status","DATABASE UPDATED", QMessageBox::Ok);
else
QMessageBox::critical(0, "DB Status","DATABASE NOT UPDATED", QMessageBox::Ok);
return true;
}
}

Lesiok
12th March 2015, 07:07
What is db.lastError and query.lastError ?

antweb
12th March 2015, 07:23
Made it work. Absolutely my bad. Was choosing the wrong table. Dumb mistake.
The above code I posted works perfectly.

antweb
13th March 2015, 07:19
I executed a query and did some processing based on it's data. Now that I am executing another query after that it still contains the data of the previous query. Any idea why?

^NyAw^
13th March 2015, 09:11
Hi,

Show us the relevant code.