Results 1 to 20 of 61

Thread: using an isntance of QSqlDatabase for connection defiinition

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    jfinn88 Guest

    Default Re: using an isntance of QSqlDatabase for connection defiinition

    Quote Originally Posted by Lesiok View Post
    conn1 is the name of db connection not C++ variable. Variable name is m_insertDataBase - C++ ABC.
    And line 4
    Qt Code:
    1. m_insertDataBase = QSqlDatabase::database("conn1");
    To copy to clipboard, switch view to plain text mode 
    is unnecessary.
    It didn't seem to work when passing the driver and the connection name with the line of code above this one. So I tried setting the connection name again but kept getting "conn1 was not declared in this scope". I know conn1 is the db connection name I declare it in the code above and in this line and I declare the instance variable in my header file. I only used the above code seeing if setting the db connection name would work like this I realize its unnecessary, I was just trying something to get it to work. I'm not sure if I need to declare the instance variable under public or not in the header file?


    Added after 4 minutes:


    Quote Originally Posted by anda_skoa View Post
    You also don't want to do the addDatabase() and setDatabaseName() calls every time, just once, when "m_insertDataBase" is not valid yet.
    See QSqlDatabase:::isValid().

    Also, as someone had already pointed out in another thread, don't concatenated an input string into a SQL Query, always use prepare() and bound values for proper escaping.

    Cheers,
    _
    You are talking about binding my Msg variable correct? the parameter I'm using in my sql statement?

    I was using a prepare statement before but took it out to try to short my code and it only takes one parameter wasn’t sure how to tell if its using the right connection. I will go back to using the prepare statement as suggested to allow proper escaping. How do I set the conn1 connection since The prepare() fcn only takes one argument can't pass it the connection name with sql statement do I just set the connection by

    Qt Code:
    1. m_selectDataBase = QSqlDatabase::database("conn2");
    To copy to clipboard, switch view to plain text mode 

    I will look at documentation on isValid(). How do I only call those methods once when the instance is not valid? little explanation on how this works be great, you got a example?
    Last edited by jfinn88; 24th August 2016 at 16:29.

Similar Threads

  1. Replies: 16
    Last Post: 4th September 2013, 00:49
  2. How to set x509 on a QSqlDatabase Connection?
    By m3rlin in forum Qt Programming
    Replies: 24
    Last Post: 21st February 2012, 04:04
  3. Windows OCI QSqlDatabase connection
    By hollyberry in forum Newbie
    Replies: 10
    Last Post: 13th February 2012, 22:13
  4. QSqlDatabase Connection Close on Destruction
    By Sanuden in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2011, 15:32
  5. QSqlDatabase connection timeout?
    By joseprl89 in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2011, 01:43

Tags for this Thread

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.