Noob question about example code



  • In many of the examples provided by QT, that use access to a database, there is a run time test to see if the drivers have been loaded. My question: If I #include QtSql in my code, do I really need this test? If I make a call to sql without the include, I'll get a compile error. How can a program's source have an include and not have the drivers accessible? Why is this sample code included in the examples?


  • Moderators

    @bart.hollis

    Yes. You test that the actual driver could be loaded. If you deploy somewhere your application and you forgot to deploy the appropriate driver or something happened to it, you will love the test!


  • Lifetime Qt Champion

    Hi,

    To add to @koahnig, each driver is a plugin so when you open a database connection the appropriate plugin gets loaded unless theres a problem (usually missing dependencies). That's what you are seeing.



  • @bart-hollis think of the drivers as two part things: one is the definition (header file you use with #include) and then the implementation (driver), the actual code accessing the database.
    So not using #include gives you a compile-time error, since the compiler cannot see the "description" of operations/methods provided by a particular driver.
    Not having the driver available when you actually run your database-driven application gives you a runtime error, since something is missing at "run time". So the runtime check allows you to create a robust application that refuses to go on if some required component (in this case the driver!) is not available at that specific time.



  • What a wonderful resource this forum is! Thank you all for your rapid and thorough responses. I only hope that some day I'll know enough to be able to return to the community at least a part of what I have received!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.