Results 1 to 4 of 4

Thread: Database Connection Problem

  1. #1
    Join Date
    Jul 2012
    Posts
    40
    Qt products
    Qt4 Qt/Embedded

    Default Database Connection Problem

    Hi,
    Qt Code:
    1. METHOD1()
    2. {
    3. if(db.open())
    4. {
    5.  
    6. QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
    7. db.setDatabaseName("/mnt/jffs2/Offlinedb.sqlite");
    8. QSqlQuery query("select * from networkmode");
    9. while (query.next()) {
    10. QString country = query.value(0).toString();
    11. QMessageBox::critical(0,"Success",country,QMessageBox::Ok);
    12.  
    13. }
    14. }
    15. else
    16. {
    17. QMessageBox::critical(0,"Error","Database Connection Problem",QMessageBox::Ok);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    -------------------------
    Qt Code:
    1. method2()
    2. {
    3. QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
    4. db.setDatabaseName("/mnt/jffs2/Offlinedb.sqlite");
    5. QSqlQuery query;
    6. if(db.open())
    7. {
    8. bool ch=query.exec("select * from networkmode");
    9. qDebug()<<"REQUIRED CH______REGQISSSSSs"<<ch;
    10. if(ch)
    11. QMessageBox::critical(0,"Success","Database Conected",QMessageBox::Ok);
    12. else
    13. QMessageBox::critical(0,"Error",query.lastError().text(),QMessageBox::Ok);
    14. }
    15. else
    16. {
    17. QMessageBox::critical(0,"Error","Database Connection Problem",QMessageBox::Ok);
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 


    ->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.

  2. #2
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Database Connection Problem

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Database Connection Problem

    Using prepare() is not required.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jul 2012
    Posts
    40
    Qt products
    Qt4 Qt/Embedded

    Default Re: Database Connection Problem

    @ 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....
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("/mnt/jffs2/Offlinedb.sqlite");
    3. if (!db.open())
    4. {
    5. QMessageBox::critical(0, tr("Cannot open database"),
    6. tr("Please Enter the Apmc"), QMessageBox::Ok);
    7. }
    8. else
    9. {
    10. QSqlQuery query,query_uid,query_yard,query_gate;
    11. bool qu_ch;
    12. 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)");
    13. qDebug()<<"CReation of table query check-"<<checkquery<<query.lastError();
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 
    ------
    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........
    Last edited by sai_3289; 26th February 2013 at 06:47.

Similar Threads

  1. Replies: 1
    Last Post: 24th January 2013, 20:42
  2. database connection
    By mak_user in forum Newbie
    Replies: 1
    Last Post: 9th February 2011, 09:23
  3. Connection With database
    By sudheer168 in forum Qt Programming
    Replies: 4
    Last Post: 22nd December 2010, 09:18
  4. Database connection
    By poporacer in forum Newbie
    Replies: 7
    Last Post: 7th September 2010, 07:49
  5. Database connection problem
    By MarkoSan in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2008, 20:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.