Results 1 to 8 of 8

Thread: how to save database

  1. #1
    Join Date
    Dec 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default how to save database

    hi
    how are you?
    i wan`t to ask how i can to save a database that i enter data in it and i use it in onther time like i make a database and i give it a name and i create tables in it but how i can save to use it data every time i enter to it .
    i look in alot of books but i just see how to make query but no body talk how to save it
    and if any body can give me a small example like address bokk or any small project.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to save database

    Take a look at database examples distributed with Qt. They mostly work on sqlite databases which are plain files on your disk so you don't need a database server.

  3. The following user says thank you to wysota for this useful post:

    GioFX (4th January 2009)

  4. #3
    Join Date
    Dec 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Re: how to save database

    ---------------(iam using sqlite)
    i did not found any document or any subjects about ( database examples distributed with Qt.).
    so i will be grateful if you give me a link or give any link about this subject.
    and if you have a little program that connect with adatabase i will be so grateful to you

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to save database

    Open up Qt Assistant and click on "Examples" and then SQL in Qt Reference Manual.

  6. #5
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: how to save database

    The example within QT Assistent are creating the same database content using the createConnection() function each time the program is launched.

    I suppose the initial post was to ask how the content of the SQLITE database can be stored when the application ends and how this content can be reloaded again when the application is started the next time so that changes made to the database are persistent across reboots.

    Do you have any example on this ?

  7. #6
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: how to save database

    I think the answer would be something like this:

    To store the database in memory only you can use something like:

    Qt Code:
    1. static bool createConnection()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    4. db.setDatabaseName(":memory:");
    5. if (!db.open()) {
    6. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    7. qApp->tr("Unable to establish the database connection.\n"
    8. "Click Cancel to exit."), QMessageBox::Cancel);
    9. return false;
    10. }
    11.  
    12. QSqlQuery query;
    13. query.exec("create table table1 (someIdentifier int, someNameAssociated varchar(30))");
    14. query.exec("create table table2 (someIdentifier int, someDateAssociated datetime)");
    15.  
    16. return true;
    17. }
    To copy to clipboard, switch view to plain text mode 


    While if you would like to make the database persistent accross reboots it should be saved in a file on the hard drive so you could use something only like:

    Qt Code:
    1. static bool createConnection()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    4. db.setDatabaseName("myDatabase");
    5. if (!db.open()) {
    6. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    7. qApp->tr("Unable to establish the database connection.\n"
    8. "Click Cancel to exit."), QMessageBox::Cancel);
    9. return false;
    10. }
    11.  
    12. QSqlQuery query;
    13. query.exec("create table table1 (someIdentifier int, someNameAssociated varchar(30))");
    14. query.exec("create table table2 (someIdentifier int, someDateAssociated datetime)");
    15.  
    16. return true;
    17. }
    To copy to clipboard, switch view to plain text mode 

    Note the difference between the 2 versions of createConnection:

    The first one is using db.setDatabaseName(":memory:");
    The second one is using db.setDatabaseName("myDatabase");

    Laurent

  8. #7
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to save database

    I am not sure this would help or not, a code snipp to connect to database


    Qt Code:
    1. database = QSqlDatabase::addDatabase("QSQLITE");
    2. if(database.isValid()) {
    3. QDir databasePath("path here");
    4. QString databaseFile(full path and file name here);
    5.  
    6. if(database.open()) {
    7. qDebug() << ;
    8. } else {
    9. qDebug() << ";
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 11th March 2009 at 10:32. Reason: missing [code] tags

  9. #8
    Join Date
    Mar 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to save database

    i did somewhat like scahall_i described above and it works. thanks.

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. Replies: 2
    Last Post: 8th August 2008, 01:54
  3. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  4. How can I save a QImage object into a SQLite3 database table?
    By danielperaza in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2008, 05:39
  5. Save images to database
    By jnk5y in forum Qt Programming
    Replies: 4
    Last Post: 8th May 2006, 19:56

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.