Is it possible to define query globally
-
Hi and welcome to devnet,
You'll have to be more specific, what query are you talking about ? What do you want to program ?
Did you took a look at Qt's documentation/tutorials/examples and demos ?
-
This is our sample code for login which we have made.
Here in header file we have made function for opening and closing database connection and that function we have used in the cpp file.
Now our issue is that we want to know that is it possible to write a function which has query like "select * from tablename" in header file and use this function in cpp file.?login.h
namespace Ui {
class login;
}class login : public QMainWindow
{
Q_OBJECTpublic:
QSqlDatabase mydb;void connClose() { mydb.close(); mydb.removeDatabase(QSqlDatabase::defaultConnection); } bool connOpen() { mydb=QSqlDatabase::addDatabase("QSQLITE"); mydb.setDatabaseName("/home/ubuntu/db1.db"); if(!mydb.open()) { qDebug()<<("Not Connected...."); return false; } else { qDebug()<<("Connected...."); return true; } }
login.cpp
void login::on_pushButton_clicked()
{
QString username,password;
username=ui->lineEdit_uname->text();
password=ui->lineEdit_pass->text();if(!connOpen())
{
qDebug()<<"Failed to open the database";
return;
}connOpen();
QSqlQuery qry;
qry.prepare("select * from login where username='"+username+"' and password='"+password+"'");if(qry.exec())
{
int count=0;
while(qry.next())
{
count++;} if(count==1){ ui->label->setText("username and password is correct"); if(count<1) ui->label->setText("username and password is incorrect"); if(count>1) ui->label->setText("duplicate username and password");
}
} -
Is it possible to define query globally
Yes .it is possible.[quote author="trupti" date="1410952271"]Hello I am new in Qt Development.
Can anyone plz suggest how to define a query globally??
Also is it possible to use query in header file??[/quote][quote author="trupti" date="1411020931"]This is our sample code for login which we have made.
Here in header file we have made function for opening and closing database connection and that function we have used in the cpp file.
Now our issue is that we want to know that is it possible to write a function which has query like "select * from tablename" in header file and use this function in cpp file.?
[/quote]
you can take reference from this link..
http://www.cplusplus.com/forum/beginner/16886/
[quote author="trupti" date="1411020931"]
@
login.hnamespace Ui {
class login;
}class login : public QMainWindow
{
Q_OBJECTpublic:
QSqlDatabase mydb;void connClose() { mydb.close(); mydb.removeDatabase(QSqlDatabase::defaultConnection); } bool connOpen() { mydb=QSqlDatabase::addDatabase("QSQLITE"); mydb.setDatabaseName("/home/ubuntu/db1.db"); if(!mydb.open()) { qDebug()<<("Not Connected...."); return false; } else { qDebug()<<("Connected...."); return true; } }
login.cpp
void login::on_pushButton_clicked()
{
QString username,password;
username=ui->lineEdit_uname->text();
password=ui->lineEdit_pass->text();if(!connOpen())
{
qDebug()<<"Failed to open the database";
return;
}connOpen();
QSqlQuery qry;
qry.prepare("select * from login where username='"+username+"' and password='"+password+"'");if(qry.exec())
{
int count=0;
while(qry.next())
{
count++;} if(count==1){ ui->label->setText("username and password is correct"); if(count<1) ui->label->setText("username and password is incorrect"); if(count>1) ui->label->setText("duplicate username and password");
}
}
@
[/quote]i see there is no error in you code;
hope it helps.
-
@ trupti based on your questions, it looks like you are new to C++ and Qt. I'd recommend getting a good book on C++ first before going further