Results 1 to 2 of 2

Thread: QSqlQuery can "insert" data to mssql 2005 enterprise, but can not "select".

  1. #1
    Join Date
    Mar 2007
    Posts
    7
    Thanks
    1

    Default QSqlQuery can "insert" data to mssql 2005 enterprise, but can not "select".

    Qt Code:
    1. QSqlQuery query = QSqlQuery(db);
    2.  
    3. bool result = query.exec("SELECT Rno FROM tbl_phone WHERE Tel=\'158123456\'");
    4.  
    5. bool active = query.isActive();
    6. bool sel = query.isSelect();
    7. bool valid = query.isValid();
    8.  
    9. int rno = query.value(0).toInt();
    To copy to clipboard, switch view to plain text mode 
    the rno should be 3. but it is zero always. the active, sel, valid are all false, while result is true.

    when debugging, i find an error in QSqlResult:
    " [Microsoft][ODBC SQL Server Driver][SQL Server]对象名 'tbl_phone' 无效。 [Microsoft][ODBC SQL Server Driver][SQL Server]该游标未声明。"

    [Microsoft][ODBC SQL Server Driver][SQL Server] invalid object name of "tbl_phone", .... this occur is not decleared.

    thx.

  2. #2
    Join Date
    Apr 2010
    Location
    United States
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery can "insert" data to mssql 2005 enterprise, but can not "select".

    Most likely the database pointed to within your DSN that you connected to with the QSqlDatabase object db for your QSqlQuery does not contain a table by the name of tbl_phone judging by your sql error.

    However once that is resolved you will also want to advance your query record position to a valid record before retrieving the result with value() method.

    QVariant QSqlQuery::value ( int index ) const
    Returns the value of field index in the current record.

    The fields are numbered from left to right using the text of the SELECT statement, e.g. in

    SELECT forename, surname FROM people;
    field 0 is forename and field 1 is surname. Using SELECT * is not recommended because the order of the fields in the query is undefined.

    An invalid QVariant is returned if field index does not exist, if the query is inactive, or if the query is positioned on an invalid record.

    See also previous(), next(), first(), last(), seek(), isActive(), and isValid().
    Example:
    Qt Code:
    1. QSqlQuery query(db);
    2. if (query.exec("SELECT Rno FROM tbl_phone WHERE Tel=158123456"))
    3. {
    4. query.first(); // or query.next() etc
    5. rno = query.value(0).toInt();
    6. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 15th February 2010, 17:27
  2. Replies: 3
    Last Post: 25th August 2009, 13:03
  3. Replies: 3
    Last Post: 8th July 2008, 19:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

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.