Results 1 to 12 of 12

Thread: Sqlite3 window problem

  1. #1
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Sqlite3 window problem

    There are differences between QSQLITE in windows and in linux?
    In linux, if the database doesnot exist Qt create a new file with the databasename.
    In windows it does not, although I can use the database.
    Perhaps, the sqlite database has been creating in memory, or what?
    G

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sqlite3 window problem

    on windows Qt also creates file with databasename in app directory.
    but if you this like this
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(":memory:");
    To copy to clipboard, switch view to plain text mode 
    then the database will be allocated in memory.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite3 window problem

    I did not.

    Qt Code:
    1. QSqlDatabase d = QSqlDatabase::database();
    2. d.setDatabaseName(databaseName);
    To copy to clipboard, switch view to plain text mode 

    Still, I cannot see the file in the any folder, but I can do sql query!
    How is it possible?
    even if I close the application the database file is not present....

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sqlite3 window problem

    did you change work directory (dest dir) in pro-file?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite3 window problem

    I don't know what you are talking about....
    Please, shine me!

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sqlite3 window problem

    ok, build an troll's example. go to QTDIR/examples/sql and in connection.h change the next lines
    Qt Code:
    1. // db.setDatabaseName(":memory:");
    2. db.setDatabaseName("test.db");
    To copy to clipboard, switch view to plain text mode 
    then go (for example) to cachedtable dir, build and run this example. the "test.db" file should be with executable file.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite3 window problem

    I still do not understand.
    My exec is in debug folder.
    If I try to create a database e.g. d.setDatabaseName("pinco.db"), the database FILE is correctly created in the debug folder.
    BUT: If i try to create d.setDatabaseName("C:/Docuemnts and Settings/giuse") the file IS NOT CREATED, but I can make queries!
    G

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sqlite3 window problem

    I've tried to connect to a database with name wich you specified and a file was created.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite3 window problem

    You are under windows?
    The same code under linux works (the file is created). But in windows not.
    I am desperate.
    Anyway, a solution is to close the connection, reopen it with addDatabase.
    In this case the file is created.
    G

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sqlite3 window problem

    I'm under windos now and a file creates fine. which version of Qt do you use? maybe this is a bug. I use 4.3.5.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #11
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite3 window problem

    I did not want to post the code, but I am really confused.
    Actually, the error is present in linux too.
    In my mainwindow constructor I did

    Qt Code:
    1. mainWindow::manWindow(
    2. ....
    3. db = QSqlDatabase::addDatabase("QSQLITE");
    4. if (createConnection())
    5. {
    6. initializeModel();
    7. createView();
    8. updateDetails(QModelIndex());
    9. ....
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. bool mainWindow::createConnection()
    2. {
    3.  
    4.  
    5. /* If the database do not exist
    6.   I create one anew*/
    7.  
    8. QFile check;
    9. check.setFileName(databaseName);
    10. QDir::setCurrent(QCoreApplication::applicationDirPath());
    11. db.setDatabaseName(databaseName);
    12.  
    13. if (!check.exists()){
    14.  
    15. #ifdef DEBUG
    16. qDebug()<<databaseName<<"do not exist. However, is open="<<db.open();
    17. #endif
    18. currentDatabase = databaseName ;
    19. createTable();
    20. statusLabel->setText(currentDatabase);
    21. }
    22. if (!db.open()) {
    23.  
    24. #ifdef DEBUG
    25. qDebug()<<db.lastError()<<db.open();
    26. #endif
    27. removeConnection();
    28. return false;
    29. }
    To copy to clipboard, switch view to plain text mode 

    When I wanna add a new datatabase (which in SQLITE means a new file!) I do

    Qt Code:
    1. void mainWindow::newDB()
    2. {
    3. /* Create a new archive*/
    4.  
    5. QString fileName = QFileDialog::getSaveFileName(this, tr("Nuovo archivio"),QDir::homePath (),tr("Rubic database (*.db)"));
    6. QFile f(fileName);
    7.  
    8.  
    9. if (fileName != currentDatabase)
    10. {
    11. db.setDatabaseName(fileName);
    12. #ifdef DEBUG
    13. qDebug()<<"newDB, opening"<<db.lastError()<<"is open="<<db.isOpen()<<fileName<<db.isValid();
    14. #endif
    15. if (db.isValid()){
    16. currentDatabase = fileName;
    17. createTable();
    18. initializeModel();
    19. createView();
    20. updateDetails(QModelIndex());
    21. statusLabel->setText(currentDatabase);
    22. }
    23. }
    24. }
    25.  
    26. currentDatabase = databaseName;
    27. return true;
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 
    The funny think is
    1) After the start up, If I create a newDB, the debug messages say that the database has been created. IN fact I can use the queries but I cannot see any database file in the choosen dir
    2) If I quit the application, since a write the currentDatabase var. inside a QSettings, the debu message says that the database does not exist and it will create it.
    3) The only way I found to let it work is to addDatabase also in the newDB function.

    Any suggestion appreciated

  12. #12
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite3 window problem

    I found a solution but I do not understand why it is working.
    First I create a static member function in mainWindow:

    Qt Code:
    1. bool mainWindow::createConnStatic()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 
    which is declared static in the header file.

    Now, at the startup I create a connection by calling this function. db is a member variable.
    When I wann change database, I do
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database();
    2. db.setDatabaseName(s);
    To copy to clipboard, switch view to plain text mode 
    This work pretty well, but is "impossible" to got there by reading the Qt Doc.

Similar Threads

  1. Problem with main window
    By M.A.M in forum Qt Programming
    Replies: 2
    Last Post: 2nd June 2008, 13:55
  2. QMain Window Problem
    By maveric in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2008, 15:51
  3. Problem hiding main window on minimize
    By bpetty in forum Newbie
    Replies: 5
    Last Post: 18th September 2007, 17:41
  4. New to QT, problem displaying main window...
    By McCall in forum Qt Programming
    Replies: 4
    Last Post: 15th June 2007, 14:27

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.