Results 1 to 20 of 20

Thread: QSqlDatabase and "CREATE DATABASE ..." ??

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlDatabase and "CREATE DATABASE ..." ??

    Quote Originally Posted by Lesiok View Post
    Sorry JohannesMunk but Yours solution is not working on PostgreSQL (and on another DB too I think). Before executing some query You must call QSqlDatabase::open() method. This method connecting to the real database not to the server.
    There is always a real database called "postgresql" on each postgresql server. Maybe you don't have privileges to connect to it? And I'm not convinced you have to open any databases to be able to create other databases. Maybe that's a permission problem as well? Connecting to the server itself should be enough.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase and "CREATE DATABASE ..." ??

    Quote Originally Posted by wysota View Post
    There is always a real database called "postgresql" on each postgresql server. Maybe you don't have privileges to connect to it? And I'm not convinced you have to open any databases to be able to create other databases. Maybe that's a permission problem as well? Connecting to the server itself should be enough.
    1. Please read my first post in this thread...
    2. I'm trying to connect to server on account with full privileges.

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

    Default Re: QSqlDatabase and "CREATE DATABASE ..." ??

    From what I see postgresql by default tries to connect to the database called the same as the database user (so if you connect on behalf of user "xyz", you will be connected to database "xyz"). At least that's what the default client does. Probably if you don't specify the database name, you'll end up trying to connect to the default database which fails because no such db exists. I don't know if Qt behaves the same way (it's easy to check in the source code) but I assume it delegates it to psql client library which could behave this way if the default client behaves that way as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: QSqlDatabase and "CREATE DATABASE ..." ??

    Just out of curiosity: How would you programatically create a database in postgresql then?

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase and "CREATE DATABASE ..." ??

    Quote Originally Posted by JohannesMunk View Post
    Just out of curiosity: How would you programatically create a database in postgresql then?
    As I say in my first post in this thread. On PostgreSQL server allways exist "service database" called postgres. In this database is saved dictionary of databases, tables, function, users etc.
    With this query
    Qt Code:
    1. SELECT datname FROM pg_catalog.pg_database
    To copy to clipboard, switch view to plain text mode 
    You retrieve list of all databases.
    Very simple and effective mechanism.

  6. The following user says thank you to Lesiok for this useful post:

    JohannesMunk (7th April 2009)

  7. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase and "CREATE DATABASE ..." ??

    Quote Originally Posted by wysota View Post
    From what I see postgresql by default tries to connect to the database called the same as the database user (so if you connect on behalf of user "xyz", you will be connected to database "xyz"). At least that's what the default client does. Probably if you don't specify the database name, you'll end up trying to connect to the default database which fails because no such db exists. I don't know if Qt behaves the same way (it's easy to check in the source code) but I assume it delegates it to psql client library which could behave this way if the default client behaves that way as well.
    Yes. QPSQLDriver::open method do nothing with parameters. If dbName is empty than dbName parameter for PostgreSQL driver is empty too. And farther all is DB dependet.

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
  •  
Qt is a trademark of The Qt Company.