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