PDA

View Full Version : Database Connection Problem



sai_3289
25th February 2013, 15:53
Hi,

METHOD1()
{
if(db.open())
{

QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/mnt/jffs2/Offlinedb.sqlite");
QSqlQuery query("select * from networkmode");
while (query.next()) {
QString country = query.value(0).toString();
QMessageBox::critical(0,"Success",country,QMessageBox::Ok);

}
}
else
{
QMessageBox::critical(0,"Error","Database Connection Problem",QMessageBox::Ok);
}
}
-------------------------

method2()
{
QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/mnt/jffs2/Offlinedb.sqlite");
QSqlQuery query;
if(db.open())
{
bool ch=query.exec("select * from networkmode");
qDebug()<<"REQUIRED CH______REGQISSSSSs"<<ch;
if(ch)
QMessageBox::critical(0,"Success","Database Conected",QMessageBox::Ok);
else
QMessageBox::critical(0,"Error",query.lastError().text(),QMessageBox::Ok);
}
else
{
QMessageBox::critical(0,"Error","Database Connection Problem",QMessageBox::Ok);
}

}


->The METHOD1 is executing with no issues but when i follow method2 procedure i am getting the error as "out of memory Unable to execute statement" from line13......Y is to so behaving odd ????
->i am calling those methods in constructor.

alizadeh91
25th February 2013, 19:05
in the first method you didn't open db and exec the query! and first of all before executing the query use query.prepare() then execute it. use examples in Qt for more information

wysota
25th February 2013, 20:11
Using prepare() is not required.

sai_3289
26th February 2013, 05:23
@ alizadeh91....Thankz a lot for reply ...now i got the diff between them....yeah i know the use of prepare statement but y its failing when i follow the method2()..?can u explain me ...?
@wysota ...thankz for reply...
In the same class i have 1 method which contains below code..this is called when button clicked signal is generated....

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/mnt/jffs2/Offlinedb.sqlite");
if (!db.open())
{
QMessageBox::critical(0, tr("Cannot open database"),
tr("Please Enter the Apmc"), QMessageBox::Ok);
}
else
{
QSqlQuery query,query_uid,query_yard,query_gate;
bool qu_ch;
bool checkquery=query.exec("create table login_details (id INTEGER PRIMARY KEY AUTOINCREMENT,datetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP,user_id integer, yard_id varchar(20),gate_id text)");
qDebug()<<"CReation of table query check-"<<checkquery<<query.lastError();

}
------
here the debug statement printing is
if there is no table====CReation of table query check- true QSqlError(-1, "", "")
if table exists === "CReation of table query check- false QSqlError(1, "Unable to execute statement", "table login_details already exists")"..
Here again its behaving normally....dono why its behaving odd when i write method2 code directly in constructor........