Results 1 to 14 of 14

Thread: Select data from database SQLite error - Help please!

  1. #1
    Join Date
    Apr 2012
    Posts
    39
    Thanks
    7

    Exclamation Select data from database SQLite error - Help please!

    Good afternoon.

    How do I do a SELECT to an SQLite database?

    I have the following:


    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2.  
    3. db.setDatabaseName("bd_agt");
    4.  
    5. if(db.open()){
    6. qDebug() << "Opening database, status: " << db.open();
    7. }
    8.  
    9. QSqlQuery query("SELECT * FROM tag");
    10. while (query.next()) {
    11. QString nome = query.value(1).toString();
    12. ui->label->setText(nome);
    13. }
    14.  
    15. qDebug() << query.size();
    To copy to clipboard, switch view to plain text mode 


    But in query.size () always returns -1, but I have values ​​in the database
    How can I fix this?

    Regards,
    Daniel Sousa

  2. #2
    Join Date
    Mar 2012
    Location
    Lesotho
    Posts
    33
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Select data from database SQLite error - Help please!

    check if the connection is opening.
    Is your code getting into the if statement?

  3. #3
    Join Date
    Apr 2012
    Posts
    39
    Thanks
    7

    Default Re: Select data from database SQLite error - Help please!

    He says that the database opens. Where do I have to file for the database? Within the project folder or in the debug folder?


    Regards,
    Daniel Sousa

  4. #4
    Join Date
    Mar 2012
    Location
    Lesotho
    Posts
    33
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Select data from database SQLite error - Help please!

    To see whether connection opens the statement at line 6 in your code should be executed.

  5. #5
    Join Date
    Apr 2012
    Posts
    39
    Thanks
    7

    Default Re: Select data from database SQLite error - Help please!

    It appears on the console: Opening database, status: true

  6. #6
    Join Date
    Mar 2012
    Location
    Lesotho
    Posts
    33
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Select data from database SQLite error - Help please!

    if that is the case then everything seems ok,try show output of nome,and declare it out side while statement.

  7. #7
    Join Date
    Apr 2012
    Posts
    39
    Thanks
    7

    Default Re: Select data from database SQLite error - Help please!

    I've seen the problem! I put subestitui db.setDatabaseName by line 3 ("/ Users / danielsousa / Qt / BaseDados / bd_agt");

    However I want to use only the name of the database that is to take on multiple operating systems. How do I do that?


    regards,
    Daniel Sousa

  8. #8
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Select data from database SQLite error - Help please!

    Try this code:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE", "bd_agt" );
    2. db.setDatabaseName( "full_path_to_db" );
    3. db.setUserName( "UserName" );
    4. db.setPassword( "UserPass" );
    5.  
    6. if (db.open())
    7. {
    8. QSqlQuery query( QSqlDatabase::database( "bd_agt" ) );
    9.  
    10. if (query.exec( "SELECT * FROM tag" ))
    11. {
    12. while (query.next())
    13. {
    14. QString nome = query.value(1).toString();
    15. ui->label->setText( nome );
    16. }
    17.  
    18. query.clear();
    19. }
    20. else
    21. qDebug() << "Error: " << query.lastError().text();
    22. }
    23. else
    24. qDebug() << "Error: " << db.lastError().databaseText();
    To copy to clipboard, switch view to plain text mode 
    Last edited by Jonny174; 19th April 2012 at 07:15.

  9. #9
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Select data from database SQLite error - Help please!

    ideally, the db file should be stored relative to your executable file. see QCoreApplication::applicationDirPath

  10. #10
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    85
    Thanks
    2
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Select data from database SQLite error - Help please!

    qDebug() << query.size() == -1 ? query.numRowsAffected() : query.size();

  11. #11
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Select data from database SQLite error - Help please!

    From documentation: QSqlQuery::size():
    Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes.
    From documentation: QSqlQuery::numRowsAffected():
    Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined.
    As per quotes above, if everything works but size is -1, then it can't be determined and that's all.

    When working with SQLite, numRowsAffected() nor size() never worked for me.

    As long as next() works, you have nothing to worry about.

  12. #12
    Join Date
    Apr 2012
    Posts
    39
    Thanks
    7

    Default Re: Select data from database SQLite error - Help please!

    Hello
    I'm trying to find a record in a database through the name. I have the following code:

    Qt Code:
    1. //Metodo que procura uma tag através do seu nome
    2. bool BaseDados::procurarTagPeloNome(QString nome){
    3. QSqlQuery query;
    4. query.prepare("SELECT * FROM tag WHERE nome=:nome ");
    5. query.bindValue(":nome", nome);
    6. query.exec();
    7.  
    8. qDebug() << query.size();
    9.  
    10. if(!query.isSelect()){
    11. return true;
    12. }else{
    13. return false;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    However, the size of me and I have always -1 there a record with the same name.
    Am I doing something wrong?

    Regards,
    Daniel Sousa

  13. #13
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Select data from database SQLite error - Help please!

    Please read the post immediately above yours. Only some database engines support reporting the number of rows returned in a query. For Sqlite:
    Qt Code:
    1. qDebug() << db.driver()->hasFeature(QSqlDriver::QuerySize);
    2. // Output
    3. false
    To copy to clipboard, switch view to plain text mode 


    If you only want to know if a record exists then:
    Qt Code:
    1. //Metodo que procura uma tag através do seu nome
    2. bool BaseDados::procurarTagPeloNome(const QString &nome){
    3. QSqlQuery query;
    4. query.prepare("SELECT 1 FROM tag WHERE nome=:nome ");
    5. query.bindValue(":nome", nome);
    6. return query.exec() && qry.next();
    7. // If the query succeeds and returns a row (or more) then returns true.
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 26th April 2012 at 02:08.

  14. The following user says thank you to ChrisW67 for this useful post:

    sousadaniel7 (26th April 2012)

  15. #14
    Join Date
    Apr 2012
    Posts
    39
    Thanks
    7

    Default Re: Select data from database SQLite error - Help please!

    Quote Originally Posted by ChrisW67 View Post
    Please read the post immediately above yours. Only some database engines support reporting the number of rows returned in a query. For Sqlite:
    Qt Code:
    1. qDebug() << db.driver()->hasFeature(QSqlDriver::QuerySize);
    2. // Output
    3. false
    To copy to clipboard, switch view to plain text mode 


    If you only want to know if a record exists then:
    Qt Code:
    1. //Metodo que procura uma tag através do seu nome
    2. bool BaseDados::procurarTagPeloNome(const QString &nome){
    3. QSqlQuery query;
    4. query.prepare("SELECT 1 FROM tag WHERE nome=:nome ");
    5. query.bindValue(":nome", nome);
    6. return query.exec() && qry.next();
    7. // If the query succeeds and returns a row (or more) then returns true.
    8. }
    To copy to clipboard, switch view to plain text mode 
    Thank longer works. I thought only query.size () is that eventually could not be supported.

Similar Threads

  1. SQLite database problem in some SO
    By anoraxis in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2012, 23:54
  2. Sqlite Database
    By sabbu in forum Qt Programming
    Replies: 5
    Last Post: 16th May 2011, 13:07
  3. How to insert row to SQLite database?
    By MIH1406 in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2010, 12:22
  4. Conect and do select in a database mysql
    By mmm286 in forum Newbie
    Replies: 1
    Last Post: 10th March 2010, 16:21
  5. [QT4][SQLITE] Database and query
    By agent007se in forum Newbie
    Replies: 10
    Last Post: 12th July 2006, 22:16

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.