PDA

View Full Version : unable to run query



sachinmcajnu
11th March 2011, 09:27
i made a simple interface linked to mysql in fedora...................
it connectted to database...............but when i tried to query using
QSqlQuery object................
nothing was returned as output nor i was able to insert anything into database
plz help
me

unit
11th March 2011, 09:54
The magic ball is sleeping. Provide us code and errors

sachinmcajnu
11th March 2011, 10:10
no errrors were there.......................
i just printed the message "connected" on connection and while executing the query................
i looked into my database..............but database was still the same.....no change was there........

i used q.isactive() for checking the state of query object i created as QSqlQuery q.................
i used this after connection

QSqlquery q;
if(q.isactive())
cout<<"query complete"
but nothing was printed
plz help................


The magic ball is sleeping. Provide us code and errors

no errrors were there.......................
i just printed the message "connected" on connection and while executing the query................
i looked into my database..............but database was still the same.....no change was there........

i used q.isactive() for checking the state of query object i created as QSqlQuery q.................
i used this after connection

QSqlquery q;
if(q.isactive())
cout<<"query complete"
but nothing was printed
plz help................

Lesiok
11th March 2011, 10:13
Just RTFM. From QSqlQuery::isActive doc :
An active QSqlQuery is one that has been exec()'d successfully but not yet finished with.
Where is Yours query exec()'d ???

sachinmcajnu
11th March 2011, 10:36
Just RTFM. From QSqlQuery::isActive doc :
An active QSqlQuery is one that has been exec()'d successfully but not yet finished with.
Where is Yours query exec()'d ???
sorry i forgot..
i used this also
after the above code
q.exec(insert into student values(1,"sachin","m"));
but no record was added to the database

unit
11th March 2011, 11:00
qDebug(q.lastError().text().toAscii());

I think you have mistake in your query string

sachinmcajnu
11th March 2011, 12:14
qDebug(q.lastError().text().toAscii());

I think you have mistake in your query string

#include<iostream>
#include<QMessageBox>
#include<QApplication>
#include<QSqlDatabase>
#include<QtGui>
#include <QSqlQuery>
using namespace std;
struct QSqlError;
bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("jaisiyaram");
if (!db.open()) {
QMessageBox::warning(0, QObject::tr("Database Error"),"sachin");
return false;
}

return true;
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!createConnection())
cout<<"error";
else
{
cout<<"connected";
return 1;
}
QSqlQuery q;
q.exec("SELECT sno FROM student");

if (q.isActive())
QMessageBox::warning(0, "Database Error","SACHIN");


while (q.next()) {
int title = q.value(0).toInt();
//QString year = q.value(1).toString();
cout<<title << /*": " << qPrintable(year) */ endl;
}
//qDebug(q.lastError().text().toAscii());


/* if (!q.execBatch())
qDebug() << q.lastError();
*/
return app.exec();
}


it print nothing and on addition of above statemnnt it gave error:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:46:20: error: invalid use of incomplete type ‘struct QSqlError’
/usr/include/QtSql/qsqldatabase.h:57:7: error: forward declaration of ‘struct QSqlError’
make: *** [main.o] Error 1

Added after 28 minutes:

sorry i got the error,..........
in the else block
else
{
cout<<"connected";
return 1;
}

i was returning 1..........so further statements where not executing