Results 1 to 16 of 16

Thread: Load a new Sqlite database problem

  1. #1
    Join Date
    Jan 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Load a new Sqlite database problem

    Hi,

    I've a problem with SQLITE :

    My program use a Sqlite database which is set like this :
    Qt Code:
    1. m_db = QSqlDatabase::addDatabase( "QSQLITE" );
    2. m_db.setDatabaseName( "base1.bdd" );
    To copy to clipboard, switch view to plain text mode 

    m_db is create like this in the header file :
    Qt Code:
    1. private :
    To copy to clipboard, switch view to plain text mode 

    Then in a second function (in the same class), i execute SQL request :
    Qt Code:
    1. if ( m_db.open() )
    2. s_req->data = m_db.exec( "SELECT * FROM tab1 ORDER BY nom COLLATE NOCASE;" );
    To copy to clipboard, switch view to plain text mode 

    Until here, i've no problem with Sqlite.

    But in a third function, i want to load (and use) a new database (without restart program).

    To do this, i do :
    Qt Code:
    1. if ( m_db.isOpen() )
    2. m_db.close();
    3.  
    4. m_db.setDatabaseName( "base2.bdd" );
    To copy to clipboard, switch view to plain text mode 

    But, here, if i launch program it closes immediately as soon i've come on the if instruction.

    I don't know what is wrong.

    Thank you for your answer.[/quote]
    Last edited by beware; 18th January 2011 at 22:25.

  2. #2
    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: Load a new Sqlite database problem

    Quote Originally Posted by beware View Post
    Hi,
    Then in a second function (in the same class), i execute SQL request :
    Qt Code:
    1. if ( !m_db.open() )
    2. s_req->data = m_db.exec( "SELECT * FROM tab1 ORDER BY nom COLLATE NOCASE;" );
    To copy to clipboard, switch view to plain text mode 
    If the database is not open then try to execute a query... are you sure about that logic?

    Also, if you are interested in future proofing: QSqlDatabase::exec() is deprecated. Use QSqlQuery::exec() instead.

    Until here, i've no problem with Sqlite.

    But in a third function, i want to load (and use) a new database (without restart program).

    To do this, i do :
    Qt Code:
    1. if ( m_db.isOpen() )
    2. m_db.close();
    3.  
    4. m_db.setDatabaseName( "base2.bdd" );
    To copy to clipboard, switch view to plain text mode 

    But, here, if i launch program it closes immediately as soon i've come on the if instruction.
    I don't know what is wrong.
    Have you tried a clean build?
    Have you tried using a debugger to work out the problem?
    What have you tried to debug the problem?
    Is there any error message when the program "closes immediately"?
    Are there any warnings or error messages issued before the program "closes immediately"?
    Has m_db been set when you execute this code, or is it still in the default constructed invalid state? What is the return of m_db.isValid()?
    Last edited by ChrisW67; 18th January 2011 at 22:28.

  3. #3
    Join Date
    Jan 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Load a new Sqlite database problem

    Hi,

    yes, you're rigth i made a mistake in my post, the condition is of course
    Qt Code:
    1. if ( m_db.open )
    To copy to clipboard, switch view to plain text mode 

    Then, m_db is set in the constructor of the class (m_db is a private member). And i use it in the function of this class.
    So when i do m_db.exec in my function, m_db is already set by the constructor. Well i think.

    Edit:
    Well i've just add this at the end of my main function (which executes sql query):
    Qt Code:
    1. m_db.close();
    2. qDebug() << m_db.lastError();
    To copy to clipboard, switch view to plain text mode 

    Then i ve got this :
    Qt Code:
    1. QSqlError(-1, "Error on ending file", "unable to close due to unfinalised statements")
    To copy to clipboard, switch view to plain text mode 

    But i don't know what is it.
    Last edited by beware; 18th January 2011 at 22:41.

  4. #4
    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: Load a new Sqlite database problem

    Have you started any transactions on that connection that you have yet to commit or rollback?

  5. #5
    Join Date
    Jan 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Load a new Sqlite database problem

    The only SQL function i've used are :
    open()
    close()
    exec()

    I don't know if it's important but i don't use a QqslTable

  6. #6
    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: Load a new Sqlite database problem

    Dont store the QSqlDatabse object as private member. Use it on the fly by QSqlDatabase::database().

  7. #7
    Join Date
    Jan 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Load a new Sqlite database problem

    Well if i don't set QSqlDatabase as a member, and if i use it in my function like this :
    Qt Code:
    1. QSqlDatabase m_db = QSqlDatabase::addDatabase( "QSQLITE" );
    2. m_db.setDatabaseName( db_name );
    To copy to clipboard, switch view to plain text mode 

    And then i can use it. But, at launch i call this function 5 times. Well the first call is correct, but i ve this error on the second one :
    Qt Code:
    1. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: Load a new Sqlite database problem

    read my post again. addDatabase() is called only first time. After that use database().

    http://doc.qt.nokia.com/latest/qsqld....html#database

  9. #9
    Join Date
    Jan 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Load a new Sqlite database problem

    If use QSqlQuerry in local, how i send data to others functions?

    Edit :
    For now, i store all my data in a QStringList that i return to functions.
    Last edited by beware; 19th January 2011 at 14:45.

  10. #10
    Join Date
    Jan 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Load a new Sqlite database problem

    Hi,

    i've a problem to open my database when i'm using database. My code is:
    Qt Code:
    1. if ( first_call )
    2. {
    3. qDebug() << "first";
    4. m_db = QSqlDatabase::addDatabase( "QSQLITE" );
    5. m_db.isValid() ? qDebug() << "valid" : qDebug() << "not valid";
    6. first_call = false;
    7. }
    8. else
    9. {
    10. qDebug() << "others";
    11. m_db = QSqlDatabase::database();
    12. m_db.isValid() ? qDebug() << "valid" : qDebug() << "not valid";
    13. }
    14. m_db.setDatabaseName( m_database );
    15.  
    16. /** Display an error if database can not be opened **/
    17. if ( !m_db.open() )
    18. {
    19. QSqlQuery q( m_db );
    20. q.exec( "SELECT count(*) FROM films;" ) ? qDebug() << "query ok" : qDebug() << "query ko";
    21. while ( q.next() )
    22. {
    23. s_stats->nb_total = q.value(0).toString();
    24. }
    25. }
    26. q.finish();
    27. m_db.close();
    To copy to clipboard, switch view to plain text mode 

    Then i got this with qdebug:
    Qt Code:
    1. first
    2. valid
    3. query ok
    4. QSqlError(-1, "", "unable to close due to unfinalised statements")
    To copy to clipboard, switch view to plain text mode 

    I get all my data from database, but it 's not clean.

  11. #11
    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: Load a new Sqlite database problem

    Do you really mean to query a database that failed to open() at lines 20-25?
    Line 27 should not compile given that the object it is referring to is out-of-scope.

    It is helpful to produce a small, complete example that compiles and demonstrates the problem.

    Suggestions:
    If this is part of a class then put line 5 in the constructor and dispense with the first_call flag business.

  12. #12
    Join Date
    Jan 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Load a new Sqlite database problem

    Qt Code:
    1. #include "sql_functions.hpp"
    2.  
    3. sql_functions::sql_functions( const QString database )
    4. {
    5. /**
    6.   * Create structure :
    7.   * - to store data for SQL request
    8.   * - to store data for statistics
    9.   **/
    10. s_req = new Struct_Request;
    11. s_stats = new Struct_Statistic;
    12.  
    13. first_call = true;
    14. m_database = database;
    15. }
    16.  
    17. sql_functions::~sql_functions()
    18. {
    19. delete s_req;
    20. delete s_stats;
    21. }
    22.  
    23. void sql_functions::exec( const int request_type, QString movie_title, QString type, QString borrower_name,
    24. QString Id_movie_update )
    25. {
    26. /** Clear data field in request structure **/
    27. s_req->data.clear();
    28.  
    29. if ( first_call )
    30. {
    31. m_db = QSqlDatabase::addDatabase( "QSQLITE" );
    32. first_call = false;
    33. }
    34. else
    35. {
    36. m_db = QSqlDatabase::database();
    37. }
    38. m_db.setDatabaseName( m_database );
    39.  
    40. /** If no error occured, SQL request is executed **/
    41. if ( m_db.open() )
    42. {
    43. s_req->data = QSqlQuery( m_db );
    44. switch( request_type )
    45. {
    46. case RequestSQL::DISPLAY_ALL :
    47. s_req->data.exec( "SELECT type,titre,dvd_en_pret,bluray_en_pret,dvd_prete_a,bluray_prete_a,est_combo "
    48. "FROM films ORDER BY titre COLLATE NOCASE;" );
    49. break;
    50.  
    51. case RequestSQL::DISPLAY_DVD :
    52. s_req->data.exec( "SELECT type,titre,dvd_en_pret,bluray_en_pret,dvd_prete_a,bluray_prete_a,est_combo
    53. "FROM films WHERE type IN ('DVD') ORDER BY titre COLLATE NOCASE;" );
    54. break;
    55.  
    56. case RequestSQL::DISPLAY_BLURAY :
    57. s_req->data.exec( "SELECT type,titre,dvd_en_pret,bluray_en_pret,dvd_prete_a,bluray_prete_a,est_combo "
    58. "FROM films WHERE type IN ('BLURAY') ORDER BY titre COLLATE NOCASE;" );
    59. break;
    60.  
    61. default:
    62. break;
    63. }
    64.  
    65. /** close database **/
    66. m_db.close();
    67. }
    68. }
    To copy to clipboard, switch view to plain text mode 

    Variable s_req->data (QSqlQuerry) is used to share data with others functions in my program.

  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

    Unhappy Re: Load a new Sqlite database problem

    You cannot use the QSqlQuery to pass data back to other parts of the program if you insist on closing the database connection (even if it let you). It probably won't let you close the database connection because the QSqlQuery you create and execute is still active.

    Why do you want to keep opening and closing the connection rather than keeping a persistent connection for the life of your sql_functions instance?

    How about something like (untested - not at my machine):
    Qt Code:
    1. #include "sql_functions.hpp"
    2.  
    3. sql_functions::sql_functions( const QString database ):
    4. m_database(database)
    5. {
    6. // Establish the database connection
    7. QSQLDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    8. db.setDatabaseName( m_database );
    9. db.open();
    10.  
    11. /**
    12.   * Create structure :
    13.   * - to store data for SQL request
    14.   * - to store data for statistics
    15.   **/
    16. s_req = new Struct_Request;
    17. s_stats = new Struct_Statistic;
    18. }
    19.  
    20. sql_functions::~sql_functions()
    21. {
    22. delete s_req;
    23. delete s_stats;
    24.  
    25. QString connectionName;
    26. { // scope the db instance so that removeDatabase does not complain
    27. // about active connections.
    28. QSqlDatabase db = QSqlDatabase::database();
    29. connectionname = db.connectionName();
    30. if (db.isOpen())
    31. db.close();
    32. }
    33. QSqlDatabase::removeDatabase(connectionName);
    34.  
    35. }
    36.  
    37. void sql_functions::exec( const int request_type, QString movie_title,
    38. QString type, QString borrower_name,
    39. QString Id_movie_update )
    40. {
    41. /** Clear data field in request structure **/
    42. s_req->data.clear();
    43.  
    44. QSqlDatabase db = QSqlDatabase::database();
    45.  
    46. /** If no error occured, SQL request is executed **/
    47. if ( db.open() )
    48. {
    49. s_req->data = QSqlQuery( db );
    50. switch( request_type )
    51. {
    52. case RequestSQL::DISPLAY_ALL :
    53. s_req->data.exec( "SELECT type,titre,dvd_en_pret,bluray_en_pret,dvd_prete_a,bluray_prete_a,est_combo "
    54. "FROM films ORDER BY titre COLLATE NOCASE;" );
    55. break;
    56.  
    57. case RequestSQL::DISPLAY_DVD :
    58. s_req->data.exec( "SELECT type,titre,dvd_en_pret,bluray_en_pret,dvd_prete_a,bluray_prete_a,est_combo
    59. "FROM films WHERE type IN ('DVD') ORDER BY titre COLLATE NOCASE;" );
    60. break;
    61.  
    62. case RequestSQL::DISPLAY_BLURAY :
    63. s_req->data.exec( "SELECT type,titre,dvd_en_pret,bluray_en_pret,dvd_prete_a,bluray_prete_a,est_combo "
    64. "FROM films WHERE type IN ('BLURAY') ORDER BY titre COLLATE NOCASE;" );
    65. break;
    66.  
    67. default:
    68. break;
    69. }
    70.  
    71. }
    72. }
    To copy to clipboard, switch view to plain text mode 
    no m_db, no first_call, no constant open/close.

  14. #14
    Join Date
    Jan 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Load a new Sqlite database problem

    Thanks for your answer. I update my code.

    No compilation problem, but i have an error when i closing my program, here :
    Qt Code:
    1. QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    To copy to clipboard, switch view to plain text mode 

  15. #15
    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: Load a new Sqlite database problem

    Where else are you creating queries against the database? Are you creating copies of the query created by this class elsewhere in the program? Are they being cleared or deleted before this point?

  16. #16
    Join Date
    Jan 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Load a new Sqlite database problem

    Well,
    All my SQL querries are create in function
    Qt Code:
    1. void sql_functions::exec
    To copy to clipboard, switch view to plain text mode 

    Querries result are store in a structure, and others function can used this value to get back request values with
    Qt Code:
    1. QString result;
    2. while s_req->data.next()
    3. result = s_req->data.value(0).toString();
    To copy to clipboard, switch view to plain text mode 

    I only clear the querry in the begining of the function.

Similar Threads

  1. Load in memory the content of a database
    By franco.amato in forum Qt Programming
    Replies: 24
    Last Post: 3rd January 2011, 17:21
  2. How to insert row to SQLite database?
    By MIH1406 in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2010, 12:22
  3. QT & Sqlite problem: "database is locked"
    By xfurrier in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2009, 07:06
  4. Cannot load SQLITE driver on clean target
    By ForestDweller in forum Installation and Deployment
    Replies: 1
    Last Post: 15th December 2008, 00:49
  5. Load menu from database
    By daica2003 in forum Qt Programming
    Replies: 5
    Last Post: 11th May 2008, 18:18

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.