Results 1 to 9 of 9

Thread: SQLQuery assigning column

  1. #1
    Join Date
    Jul 2012
    Posts
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Platforms
    Windows

    Question SQLQuery assigning column

    Hi,
    Here's my code. Is this right?

    def sqlquery():
    query = QSqlQuery(db)
    query.prepare("Select itemcode, description, srp, vat from items Where itemcode=:itemcode;");
    if query.next():
    itemcode = query.value(0).toInt()
    description = query.value(1).toString()
    srp = query.value(2).toInt()
    vat = query.value(3).toInt()
    query.bindvalue(":itemcode", QtCore.QVariant(itemcode))
    query.exec_()
    return True

  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: SQLQuery assigning column

    No, it's wrong.
    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.


  3. #3
    Join Date
    Jul 2012
    Posts
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Platforms
    Windows

    Default Re: SQLQuery assigning column

    Man,your so fast. :-) What's the right way? Can you enlighten me? Thank you again wysota.

  4. #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: SQLQuery assigning column

    Currently you are trying to read data from the query before the query is made. You need to reorder your statements.
    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.


  5. #5
    Join Date
    Jul 2012
    Posts
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Platforms
    Windows

    Default Re: SQLQuery assigning column

    def sqlquery():
    query = QSqlQuery(db)
    query.prepare("Select itemcode, description, srp, vat from items Where itemcode=:itemcode;");
    query.exec_()
    if query.next():
    itemcode = query.value(0).toInt()
    description = query.value(1).toString()
    srp = query.value(2).toInt()
    vat = query.value(3).toInt()
    query.bindvalue(":itemcode", QtCore.QVariant(itemcode))
    return True

    How about this? Is this right now wysota?

  6. #6
    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: SQLQuery assigning column

    No. Do you understand what value() and bindValue() do? Did you read the fine docs on QSqlQuery? There are examples of doing queries there.
    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.


  7. #7
    Join Date
    Jul 2012
    Posts
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Platforms
    Windows

    Default Re: SQLQuery assigning column

    Man, from what I've read in that documentation, value is a field from my table. So using this code: itemcode = query.value(0).toInt() is wrong? Can you teach me the right way? I already read many documentations and examples, but man, I still can't get it.

  8. #8
    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: SQLQuery assigning column

    Quote Originally Posted by nethanjavier View Post
    So using this code: itemcode = query.value(0).toInt() is wrong?
    If you issue that call before the query is executed then what do you expect to receive in "itemcode"?
    Furthermore if you call bindValue() after you execute the query then what do you expect the value of ":itemcode" to be used in the query?
    If your query doesn't return any rows then what exactly do you expect your variables to contain?
    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.


  9. #9
    Join Date
    Jul 2012
    Posts
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Platforms
    Windows

    Exclamation Re: SQLQuery assigning column

    Man, that's how far I understand in PyQt. Using that code,I'm expecting a value that i could pass to my tableview. As what I have explained before, I want to put the values(itemcode,description,srp and vat) to my tableview if my lineedit will find any value in my items table in my database. By using that bindvalue, I could query to my table or finding a record to my table. If my query doesn't find any record, I want to show and error message. Hope you understand me wysota.

Similar Threads

  1. from sqlquery to qstringlist
    By ditsikts in forum Newbie
    Replies: 4
    Last Post: 4th February 2016, 19:18
  2. SQLQuery usage of RAM
    By Momergil in forum Qt Programming
    Replies: 5
    Last Post: 21st May 2012, 13:45
  3. Assigning new QStyle
    By seux in forum Newbie
    Replies: 1
    Last Post: 29th August 2011, 13:30
  4. Qt Creator Assigning different compiler
    By hibbity in forum Qt Tools
    Replies: 8
    Last Post: 24th May 2011, 18:45
  5. SQLQUERY - column value retrun problem
    By damodharan in forum Qt Programming
    Replies: 7
    Last Post: 27th May 2010, 08:09

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.