Results 1 to 16 of 16

Thread: Need help on QSQLITE

  1. #1
    Join Date
    Jul 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Need help on QSQLITE

    Hi,
    I'm still new with QT and I have a simple application that add, search and remove records using SQLITE. But everytime I run the application my database is empty. Please check the partial code I'm using.
    Qt Code:
    1. #include "phonebook.h"
    2. #include "ui_phonebook.h"
    3. #include <QMessageBox>
    4. //./phonebook.db
    5. PhoneBook::PhoneBook(QWidget *parent) :
    6. QWidget(parent),
    7. ui(new Ui::PhoneBook)
    8. {
    9. ui->setupUi(this);
    10.  
    11. database = new QSqlDatabase();
    12.  
    13. //set database driver to QSQLITE
    14. *database = QSqlDatabase::addDatabase("QSQLITE");
    15. database->setDatabaseName(":memory:");
    16. //database->setDatabaseName("./phonebook.db");
    17.  
    18. //can be removed
    19. database->setHostName("localhost");
    20. database->setUserName("");
    21. database->setPassword("");
    22.  
    23. if(!database->open())
    24. {
    25. QMessageBox::warning(0,"Error","Couldn't open database file.");
    26. }
    27.  
    28. QSqlQuery query;
    29. query.exec("CREATE TABLE IF NOT EXISTS Contacts (id int primary key, "
    30. "name varchar(20), mobile varchar(20),city varchar(20))");
    31. all_model = new QSqlTableModel(this, *database);
    32. updateTable();
    33.  
    34. search_model = new QSqlTableModel(this, *database);
    35. search_model->setTable("Contacts");
    36. }
    To copy to clipboard, switch view to plain text mode 

    regards,
    lam-ang

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help on QSQLITE

    Quote Originally Posted by lam-ang View Post
    Hi,
    I'm still new with QT and I have a simple application that add, search and remove records using SQLITE. But everytime I run the application my database is empty. Please check the partial code I'm using.
    The problem is at line 15
    Qt Code:
    1. database->setDatabaseName(":memory:");
    To copy to clipboard, switch view to plain text mode 

    you're using a "in memory" SQLITE database.
    Comment this line and uncomment the line 16
    Qt Code:
    1. database->setDatabaseName("./phonebook.db");
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jul 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Need help on QSQLITE

    Hi mcosta,
    I appreciate the help and the time...I did what you suggested but this give me the output.
    Qt Code:
    1. Couldn't open database file.
    To copy to clipboard, switch view to plain text mode 
    is this line means it's creating a database name phonebook or is it only identifying the location of the file?
    Qt Code:
    1. database->setDatabaseName("./phonebook.db");
    To copy to clipboard, switch view to plain text mode 

    regards,
    lam-ang
    Last edited by lam-ang; 10th June 2011 at 13:30.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help on QSQLITE

    When the database does'n exist, open create it.

    Probably you don't have permission to create file in the directory where the program are installed.
    Try using your DATA directory.

    Use QDesktopServices; for example
    Qt Code:
    1. QString dbName = QDesktopServices::storageLocation (QDesktopServices::DataLocation) + "/phonebook.db";
    2. database->setDatabaseName(dbName);
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Jul 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Need help on QSQLITE

    Hi,
    I tried what you suggested and I added QDesktopService header but I got the same result "Couldn't open database file".

    regards,
    lam-ang

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help on QSQLITE

    Print the Database Driver Message Please

    Qt Code:
    1. if(!database->open()) {
    2. QMessageBox::warning(0,"Error",tr("Couldn't open database file: %1").arg(database.lasteError().text()));
    3. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  7. #7
    Join Date
    Jul 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Need help on QSQLITE

    Quote Originally Posted by mcosta View Post
    Print the Database Driver Message Please

    Qt Code:
    1. if(!database->open()) {
    2. QMessageBox::warning(0,"Error",tr("Couldn't open database file: %1").arg(database.lasteError().text()));
    3. }
    To copy to clipboard, switch view to plain text mode 
    Hi,
    Thanks for the help and the time, but it seems I'm having trouble compiling.
    Qt Code:
    1. C:\Users\Severpc\Documents\QtFiles1.1\SQLite_example-build-simulator\..\SQLite_example\phonebook.cpp:29: error: request for member 'lasteError' in '((PhoneBook*)this)->PhoneBook::database', which is of non-class type 'QSqlDatabase*'
    To copy to clipboard, switch view to plain text mode 
    regards,
    picuser

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Need help on QSQLITE

    It's a typo. It has to be lastError().

  9. #9
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help on QSQLITE

    Quote Originally Posted by Lykurg View Post
    It's a typo. It has to be lastError().
    I'm sorry!!
    A camel can go 14 days without drink,
    I can't!!!

  10. #10
    Join Date
    Jul 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Need help on QSQLITE

    Hi, I still have errors and would not compile..
    Qt Code:
    1. :-1: warning: The Symbian tool chain does not handle special characters in the project name 'SQLite_example.pro' well.
    To copy to clipboard, switch view to plain text mode 
    I noticed if I remove this part of code ".arg(database.lasteError().text())" it compiles.

    regards,
    lam-ang

  11. #11
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help on QSQLITE

    The correct code is

    Qt Code:
    1. if(!database->open()) {
    2. QMessageBox::warning(0,"Error",tr("Couldn't open database file: %1").arg(database->lastError().text()));
    3. }
    To copy to clipboard, switch view to plain text mode 

    try it please
    A camel can go 14 days without drink,
    I can't!!!

  12. #12
    Join Date
    Jul 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Need help on QSQLITE

    Hi, I tried it and unfortunately it does nos compile...
    Qt Code:
    1. #include "phonebook.h"
    2. #include "ui_phonebook.h"
    3. #include <QMessageBox>
    4. #include <QDesktopservices.h>
    5. //./phonebook.db
    6. PhoneBook::PhoneBook(QWidget *parent) :
    7. QWidget(parent),
    8. ui(new Ui::PhoneBook)
    9. {
    10. ui->setupUi(this);
    11.  
    12. database = new QSqlDatabase();
    13.  
    14. //set database driver to QSQLITE
    15. *database = QSqlDatabase::addDatabase("QSQLITE");
    16. // database->setDatabaseName(":memory:");
    17. //database->setDatabaseName("./phonebook.db");
    18. QString dbName = QDesktopServices::storageLocation (QDesktopServices::DataLocation) + "/phonebook.db";
    19. database->setDatabaseName(dbName);
    20.  
    21. //can be removed
    22. //database->setHostName("localhost");
    23. //database->setUserName("");
    24. //database->setPassword("");
    25.  
    26. if(!database->open()) {
    27. QMessageBox::warning(0,"Error",tr("Couldn't open database file: %1").arg(database->lastError().text()));
    28. }
    29.  
    30. QSqlQuery query;
    31. query.exec("CREATE TABLE IF NOT EXISTS Contacts (id int primary key, "
    32. "name varchar(20), mobile varchar(20),city varchar(20))");
    33. all_model = new QSqlTableModel(this, *database);
    34. updateTable();
    35.  
    36. search_model = new QSqlTableModel(this, *database);
    37. search_model->setTable("Contacts");
    38. }
    To copy to clipboard, switch view to plain text mode 
    But if I comment this line like this..
    Qt Code:
    1. if(!database->open()) {
    2. QMessageBox::warning(0,"Error",tr("Couldn't open database file: %1"));//.arg(database->lastError().text()));
    3. }
    To copy to clipboard, switch view to plain text mode 
    and here is the result when compiled...
    Qt Code:
    1. Executable file: 15482 2011-06-13T15:12:58 C:\QtSDK\Symbian\SDKs\Symbian1Qt473\\epoc32\release\gcce\urel\SQLite_example.exe
    2. Starting application...
    3. Application running with pid 4355.
    4. [Qt Message] QSqlQuery::exec: database not open
    To copy to clipboard, switch view to plain text mode 

    thanks,
    lam-ang

  13. #13
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help on QSQLITE

    Quote Originally Posted by lam-ang View Post
    Hi, I tried it and unfortunately it does nos compile...
    What is the error?
    Probably you have to add some include in your source. Perhaps
    Qt Code:
    1. #include <QtSql/QSqlError>
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  14. #14
    Join Date
    Oct 2010
    Location
    India
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need help on QSQLITE

    Hi

    It seems you can run execute your application successfully. But DB is empty/volatile if you restart your qt application. am I right?

    if yes,

    Try this

    Qt Code:

    database->setDatabaseName("memory.db");

    and use CREATE TABLE command rather than CREATE TABLE IF NOT EXISTS Contacts.

    Thanks,
    Vishnu

  15. The following user says thank you to vishnu for this useful post:

    lam-ang (13th June 2011)

  16. #15
    Join Date
    Jul 2010
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Need help on QSQLITE [SOLVED]

    Hello there,
    Thanks for helping me out, I appreciate it very much...its running fine now.
    I hope it is okay if I have another question, how can I use another database like Mysql if my target is a pc..any idea or link?

    thanks and regards,
    lam-ang
    Last edited by lam-ang; 13th June 2011 at 16:20.

  17. #16
    Join Date
    Oct 2010
    Location
    India
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need help on QSQLITE [SOLVED]

    Hello,

    IF you want to know about the SQL support drivers,

    use the following debug code,
    Qt Code:
    1. debugLog("Driver supports name "<<db.drivers());
    To copy to clipboard, switch view to plain text mode 
    you can get the list supporting drivers name.

    I think PC will support the following the drivers ("QSQLITE", "QPSQL7", "QPSQL") by default

Similar Threads

  1. QSqlite issues
    By duave in forum Newbie
    Replies: 2
    Last Post: 3rd April 2011, 23:32
  2. QSqlite changes between 4.3 and 4.7
    By LKIM in forum Qt Programming
    Replies: 0
    Last Post: 22nd March 2011, 19:50
  3. qsqlite for winCE
    By giginjose in forum Newbie
    Replies: 0
    Last Post: 27th July 2010, 15:54
  4. QSQlite driver
    By praveen_g in forum Newbie
    Replies: 6
    Last Post: 18th November 2009, 08:58
  5. QSqlite in QT4
    By sophister in forum Qt Programming
    Replies: 26
    Last Post: 5th April 2009, 12:52

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.