Results 1 to 16 of 16

Thread: Need help on QSQLITE

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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!!!

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.