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

  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

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

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

  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

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

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

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

    lam-ang (13th June 2011)

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

  8. #7
    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
  •  
Qt is a trademark of The Qt Company.