Results 1 to 9 of 9

Thread: SQLITE ATTACH database

  1. #1
    Join Date
    Apr 2008
    Location
    Pittsburgh,PA,USA
    Posts
    48
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default SQLITE ATTACH database

    Is it possible to attach an SQLITE database and still use the Qt database classes?
    John

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: SQLITE ATTACH database

    Well, you could use the sql statement
    sql Code:
    1. ATTACH DATABASE filename AS databasename
    To copy to clipboard, switch view to plain text mode 
    then it is pure SQL and has nothing to do with Qt. => You can still use Qt's classes.

    Or you can simply use
    Qt Code:
    1. QSqlDatabase::addDatabase("path/to/file", "mySecondDatabase");
    2. //...
    3. QSqlQuery q(QSqlDatabase::database("mySecondDatabase"));
    To copy to clipboard, switch view to plain text mode 

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

    drescherjm (1st November 2009)

  4. #3
    Join Date
    Apr 2008
    Location
    Pittsburgh,PA,USA
    Posts
    48
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLITE ATTACH database

    Quote Originally Posted by Lykurg View Post
    Well, you could use the sql statement
    sql Code:
    1. ATTACH DATABASE filename AS databasename
    To copy to clipboard, switch view to plain text mode 
    then it is pure SQL and has nothing to do with Qt. => You can still use Qt's classes.
    Thanks. I was not sure if that method would work.

    Or you can simply use
    Qt Code:
    1. QSqlDatabase::addDatabase("path/to/file", "mySecondDatabase");
    2. //...
    3. QSqlQuery q(QSqlDatabase::database("mySecondDatabase"));
    To copy to clipboard, switch view to plain text mode 
    Now that I expected to work.


    Now the question is mainly can I use 2 databases in the same QSqlQuery? I assume with the ATTACH database sql statement I can do this.
    Last edited by drescherjm; 1st November 2009 at 08:50.
    John

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

    Default Re: SQLITE ATTACH database

    Hello all!

    Regarding the same subject.

    It is possible to attach a database that resides into a QTemporaryFile?

    Thank you!

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: SQLITE ATTACH database

    Quote Originally Posted by tavioman View Post
    It is possible to attach a database that resides into a QTemporaryFile?
    Have you tried? Why shouldn't it working? It's a "normal" file. Just use QTemporaryFile::fileName() to work with the file.

  7. #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: SQLITE ATTACH database

    Quote Originally Posted by drescherjm View Post
    Now the question is mainly can I use 2 databases in the same QSqlQuery? I assume with the ATTACH database sql statement I can do this.
    with Qt classes you need add two databases using QSqlDatabase::addDatabase, but you need specify different connectionName:
    Qt Code:
    1. QSqlDatabase db1 = QSqlDatabase::addDatabase("QSQLITE", "db1");
    2. db1.setDatabaseName("path/to/db1");
    3. QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE", "db2");
    4. db2.setDatabaseName("path/to/db2");
    5. //opening databases and etc.
    To copy to clipboard, switch view to plain text mode 

    then you can get data from these databases like this:
    Qt Code:
    1. ...
    2. QSqlDatabase db1 = QSqlDatabase::database("db1");
    3. QSqlDatabase db2 = QSqlDatabase::database("db2");
    4.  
    5. QSqlQuery query1(db1);
    6. //preparing query and execution
    7. ...
    8. QSqlQuery query2(db2);
    9. //preparing query and execution
    10. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #7
    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: SQLITE ATTACH database

    Quote Originally Posted by Lykurg View Post
    Or you can simply use
    Qt Code:
    1. QSqlDatabase::addDatabase("path/to/file", "mySecondDatabase");
    2. //...
    3. QSqlQuery q(QSqlDatabase::database("mySecondDatabase"));
    To copy to clipboard, switch view to plain text mode 
    are you sure that your code will work?
    the first parameter of QSqlDatabase::addDatabase should be a name of a driver? i.e. should be:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "mySecondDatabase");
    2. db.setDatabaseName("path/to/file");
    3.  
    4. //...
    5. QSqlQuery q(QSqlDatabase::database("mySecondDatabase"));
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: SQLITE ATTACH database

    Quote Originally Posted by spirit View Post
    are you sure that your code will work?
    Of course not, it was a typo, but since drescherjm figured it out himself...

    And I just wonder, why people often ask questions like "Is fooBar working?" without trying it themself before asking. Then they would realize, that it is working.

  10. #9
    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: SQLITE ATTACH database

    Quote Originally Posted by Lykurg View Post
    And I just wonder, why people often ask questions like "Is fooBar working?" without trying it themself before asking. Then they would realize, that it is working.
    agree w you.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. SQLite sometimes doens't INSERT into database
    By cevou in forum Qt Programming
    Replies: 5
    Last Post: 30th October 2009, 08:10
  2. How can I send a SQLite :memory: database connection ?
    By georgep in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2009, 12:07
  3. Replies: 1
    Last Post: 26th March 2009, 15:25
  4. Replies: 2
    Last Post: 23rd February 2008, 01:58
  5. SQLITE database problems
    By phoenix in forum Newbie
    Replies: 3
    Last Post: 30th April 2007, 21:38

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.